-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicshapes.java
49 lines (42 loc) · 1.2 KB
/
basicshapes.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
import java.awt.*;
import java.applet.*;
public class basicshapes extends Applet implements Runnable {
private Thread animationThread;
private int x = 0;
private int y = 150;
private int shapeWidth = 50;
private int shapeHeight = 50;
private int moveStep = 5;
public void init() {
setBackground(Color.white);
}
public void start() {
if (animationThread == null) {
animationThread = new Thread(this);
animationThread.start();
}
}
public void stop() {
if (animationThread != null) {
animationThread = null;
}
}
public void run() {
while (true) {
x += moveStep;
if (x > getWidth()) {
x = -shapeWidth;
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRect(x, y, shapeWidth, shapeHeight);
}
}