pip15.gif (3154 bytes)

Altri esempi:

Un'applet con thread e parametro:

import java.awt.*;
import java.applet.*;

public class SpottA extends Applet implements Runnable {

int t = (int)(Math.random()*6);
int x0 = (int)(Math.random()*300);
int y0 = (int)(Math.random()*150);
int c = 0;
String pStr;
int s = 5;
Color colore;
Thread runner;
Image miaIm;
Graphics miosch;
// init

public void init() {
    setBackground(Color.black);
    colore = new Color(0,0,0);
    pStr = getParameter("sleep");
    s = Integer.parseInt(pStr);
    miaIm = createImage(getSize().width, getSize().height);
    miosch = miaIm.getGraphics();
}
//start
public void start() {
if(runner == null) {
    runner = new Thread(this);
    runner.start();
    }
}
//stop
public void stop() {
    runner = null;
}
//run
public void run() {
    Thread thisTh = Thread.currentThread();
    while(runner == thisTh) {
        int r = (int)(Math.random()*10);
        x0 += r;
        y0 += r;
        c += 1;
        if((x0 > 300) ||(y0 > 150) || (c > t)){
            x0 = (int)(Math.random()*300);
            y0 = (int)(Math.random()*150);
            t = (int)(Math.random()*6);
            colore = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
            repaint();
        }
        repaint();
        try {
    Thread.sleep(s);
} catch(InterruptedException e) { }
}
}
public void update(Graphics screen) {
    paint(screen);
}
public void paint(Graphics screen) {
    miosch.setColor(colore);
    miosch.fillOval(x0,y0,10,10);
    screen.drawImage(miaIm,0,0,this);
}
public void destroy(){
    miosch.dispose();
}
}
   

Va inserito nella pagina web con il parametro:

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<APPLET CODE="SpottA.class" width=300 height=150>
<param name="sleep" value="5">
</APPLET>
</BODY>
</HTML>

Il risultato è:

(per vedere l'applet dall'inizio aggiornare il browser)

Con le qualche nozioni che abbiamo visto finora e un pò di immaginazione è possibile fare delle cose divertenti. Tutte le applet di questo sito (tranne quelle di Java Games, of course) non richiedono (quasi) altre conoscenze. Per esempi di applicazioni alla didattica, vedere la sezione teaching on line. Certo si può fare molto di più, per questo rimandiamo ai tutorials online e ai siti professionisti (cf links).

import java.awt.*;
import java.applet.*;

public class Fine extends Applet implements Runnable {

int a = 15;
int r = 40;
int s = 5;
int re, g, b;
String sString;
String rBgc, gBgc, bBgc;
Color colore;
Color bgCol;
Thread runner;
Image miaIm;
Graphics miosch;


//init
public void init() {
    rBgc = getParameter("R");
    re = Integer.parseInt(rBgc);
    gBgc = getParameter("G");
    g = Integer.parseInt(gBgc);
    bBgc = getParameter("B");
    b = Integer.parseInt(bBgc);
    bgCol = new Color(re, g, b);   
    setBackground(bgCol);
    colore = new Color(0,0,0);
    sString = getParameter("sleep");
    s = Integer.parseInt(sString);
    miaIm = createImage(getSize().width, getSize().height);
    miosch = miaIm.getGraphics();
    }
public final void paint (Graphics screen) {
    miosch.setColor(colore);
    miosch.fillRect(10,10,(6*a)/360,r);
    miosch.fillRect(16,10,(9*a)/360,5);
    miosch.fillRect(16,25,4,(5*a)/360);
    miosch.fillRect(45,25,6,(a*25)/360);
    miosch.fillArc(45,10,6,6,0,a);
    miosch.fillRect(71,10,6,(a*r)/360);
    miosch.fillRect(77,12,(5*a)/360,3);
    miosch.fillRect(82,10,a/60,r);
    miosch.fillRect(108,10,6,(a*r)/360);
    miosch.fillRect(114,10,(10*a)/360,5);
    miosch.fillRect(114,45,10,a/72);
    miosch.fillRect(114,28,a/72,4);
    screen.drawImage(miaIm, 0, 0, this);
    }
//start
public void start() {
if(runner == null) {
    runner = new Thread(this);
    runner.start();
    }
}
//run
public void run() {
Thread thisThread = Thread.currentThread();
while(runner == thisThread) {
    if(a < 360) {
    a = a+10;
    repaint();
        }
    else {
    colore = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
    repaint();
    a = 15;
    }
try {
    Thread.sleep(s);
} catch(InterruptedException e) { }
}
}
//stop
public void stop() {
if(runner != null) {
    runner = null;
    }
}
public void update(Graphics screen) {
    paint(screen);
    }
}

NB: questa applet ha come parametro il colore dello sfondo:

<APPLET CODE="Fine.class" width=150 height=70>
<param name="sleep" value="10">
<param name="R" value="115">
<param name="G" value="200">
<param name="B" value="174">
</APPLET>

Sembra che ogni lettera si muova per conto suo (quindi che ci siano più threads) ma è un'illusione ottica... Questa applet è semplicemente una variante di ArcoB.java

fleche_retour.gif (1377 bytes)