-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleAnimation.java
47 lines (35 loc) · 1.02 KB
/
SimpleAnimation.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
import java.awt.*;
import javax.swing.*;
public class SimpleAnimation {
int x = 70;
int y = 70;
public static void main(String[] args){
SimpleAnimation gui = new SimpleAnimation();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
for(int i=0; i<100; i++){
x++;
y++;
drawPanel.repaint();
try {
Thread.sleep(50);
} catch(Exception ex){
};
}
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.GREEN);
g.fillOval(x, y, 40, 40);
}
}
}