-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnim.java
58 lines (44 loc) · 1.28 KB
/
Anim.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
56
57
58
package animation;
import javax.swing.*;
import java.awt.*;
public class Anim extends JFrame implements Runnable {
int position = 10, inc = 3;
int time = 40;
Thread t;
MyPanel panel;
public Anim() {
super("Animation");
setSize(200, 230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new MyPanel();
add(panel);
setVisible(true);
if (t == null) {
t = new Thread(this);
t.start();
}
}
public static void main(String[] args) {
Anim application = new Anim();
}
class MyPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(position + 10 , position + 10, 30, 30);
}
}
public void run() {
while (true) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
position += inc;
if (position > 100 || position < 10)
inc = -inc;
panel.repaint();
}
}
}