Ex13_1
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class Ex13_1 extends Applet implements Runnable {
int x, y, wid, hei; // 図形(楕円)の (x,y)座標, 幅wid, 高さhei
int rr, gg, bb; // 色設定用変数 RGB
Color col; // 色設定用
Random rnd;
Thread thd = null; // スレッド
public void init() { // 初期設定
this.setSize(300, 300); // 描画領域指定(300 x 300)
x = 10; y = 10; // 初期位置(x,y)
wid = 50; hei = 50; // 初期幅 wid, 高さ hei
rr = 255; gg = 0; bb = 0; // 初期色RGB(rr, gg, bb)
col = new Color(rr,gg,bb);
rnd = new Random();
thd = new Thread(this);
thd.start(); // スレッド開始
}
/* public void update(Graphics g) { // update:上書描画(画面に上書きしたい場合はこの3行追加)
paint(g);
}*/
public void paint(Graphics g) {
g.setColor(col); // 画面に色指定
g.fillOval(x,y,wid,hei); // 楕円描画
}
public void run() {
while (true) { // 無限ループ
x = rnd.nextInt(200); // x座標: 0,1,...,199 の乱数
y = rnd.nextInt(200); // y座標: 0,1,...,199 の乱数
wid = rnd.nextInt(50)+50; // 幅: 50,51,...,99 の乱数
hei = rnd.nextInt(50)+50; // 高さ:50,51,...,99 の乱数
rr = rnd.nextInt(256); // 赤色:0,1,...,255 の乱数
gg = rnd.nextInt(256); // 緑色:0,1,...,255 の乱数
bb = rnd.nextInt(256); // 青色:0,1,...,255 の乱数
col = new Color(rr,gg,bb); // 色設定
repaint(); // 再描画:paint()メソッドを呼び出し
try {
thd.sleep(250); // スレッド一時停止:停止時間=250ミリ秒
} catch(InterruptedException e) {
}
}
}
}