package test;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Ball ball;
//コンストラクタはやっつけ
public BasicApplet(){
this.setSize(120,120);
this.
setBackground(Color.
black); ball = new Ball(10);
new javax.
swing.
Timer(50,
this).
start(); }
ball.draw(g);
}
//タイマーイベントで、ballの座標を更新する
ball.move(rect);
repaint();
}
//飛び回るボールのクラス
public class Ball {
private double velocity, arg; //速度,x軸となす角
private final double RAD
= Math.
PI / 180; private float hue;//ボールの色を変えるため
private int x, y, size, sizeOffset;//ボールの位置とサイズ
private Color color
;//色変え用
public Ball(int size) {
this.size = size;
sizeOffset = size / 2;
x = 10;
y = 10;
velocity = 10.0;
arg = 33.0;
hue = 0.f;
color
= new Color(0xffffff
); }
/**
* @param rect ボールの移動範囲となる四角いエリア
* */
int xdist, ydist;
xdist
= (int) (velocity
* Math.
cos(arg
* RAD
)); ydist
= (int) (velocity
* Math.
sin(arg
* RAD
));
next
= new Point(x
+ xdist, y
+ ydist
);
// x range check
if (next.x < rect.x) {
next.x = -next.x;
arg = 180 - arg;
} else if (rect.width < next.x) {
next.x = rect.width - (next.x - rect.width);
arg = 180 - arg;
}
// y range check
if (next.y < rect.y) {
next.y = -next.y;
arg = 360 - arg;
} else if (rect.height < next.y) {
next.y = rect.height - (next.y - rect.height);
arg = 360 - arg;
}
x = next.x;
y = next.y;
}
//ボールを描画する
if (1.0 <= (hue += 0.05f)) {
hue = 0;
}
g.
setColor(new Color(color.
HSBtoRGB(hue,
1.
f,
1.
f)));//色を付ける g.fillOval(x - sizeOffset, y - sizeOffset, size, size);
}
}
}
最終更新:2011年06月30日 00:02