-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmyApplet.java
55 lines (48 loc) · 1.42 KB
/
myApplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
class Ball {
int x, y, radius, dx, dy;
Color BallColor;
public Ball(int x, int y, int radius, int dx, int dy, Color bColor) {
this.x = x;
this.y = y;
this.radius = radius;
this.dx = dx;
this.dy = dy;
BallColor = bColor;
}
}
public class myApplet extends Applet implements Runnable {
Ball redBall;
public void init() {
redBall = new Ball(150, 0, 20, 2, 5, Color.red);
Thread t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
g.setColor(redBall.BallColor);
g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);
}
public void run() {
try {
Color c = redBall.BallColor;
redBall.BallColor = new Color(c.getRed(), c.getBlue() + 20, c.getGreen());
redBall.y = redBall.y + redBall.dy;
} catch (Exception e) {
}
while (redBall.BallColor.getBlue() != 255) {
try {
displacementOperation(redBall);
Thread.sleep(1000);
repaint();
} catch (Exception e) {
}
}
}
public void displacementOperation(Ball ball) {
Color c = ball.BallColor;
ball.BallColor = new Color(c.getRed(), c.getBlue() + 20, c.getGreen());
ball.y = ball.y + ball.dy;
}
}