ラインアート
| 5本の直線を,ランダム位置に描く。0.2秒間隔で再描画する(repaint(long
tm)メソッドを使う) |
|
ソースコード
import java.applet.*;
import java.awt.*;
public class ja201 extends Applet
{
int w,h; //Appletの幅と高さ
final int n = 5; //一度に表示する直線の数
ja201Line jline[] = new ja201Line[n]; //ja201lineクラスのインスタンス配列
public void init(){
Dimension d = size(); //Appletの幅と高さを取得
w = d.width;
h = d.height;
setBackground(Color.black); //Appletの背景色を黒色に設定
int i;
for(i=0;i<n;i+=1){
jline[i] = new ja201Line(i); //ja201Lineのインスタンス配列を生成する
}
}
public void paint(Graphics g){
int i;
for(i=0;i<n;i+=1){ //n個数の直線を描画
jline[i].lineXY(w,h);
jline[i].paint(g);
}
repaint(200); //0.2秒間隔で再描画する
}
}
class ja201Line
{
double x1,y1,x2,y2; //直線の開始位置と終了位置のxy座標
Color c;
Color linec[] ={Color.pink,Color.orange,Color.yellow,Color.green,Color.magenta};
public ja201Line(int i){ //コンストラクタ
c = linec[i]; //色配列から色を取得する
}
void lineXY(int w,int h){ //直線の開始位置と終了位置のxy座標をランダムに設定
x1 = (double)w * Math.random();
y1 = (double)h * Math.random();
x2 = (double)w * Math.random();
y2 = (double)h * Math.random();
}
void paint(Graphics g){
g.setColor(c);
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
}
}
末尾