-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.java
55 lines (42 loc) · 914 Bytes
/
Animation.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
package animation;
import java.awt.*;
class MyFrame extends Frame implements Runnable
{
int x,y,tx,ty;
MyFrame()
{
x=100;
y=100;
tx=ty=1;
Thread t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,50,50);
}
public void run()
{
while(true)
{
x+=tx;
y+=ty;
if(x<0 || x>450)
tx=tx*-1;
if(y<20 || y>350)
ty=ty*-1;
repaint();
try{Thread.sleep(5);}catch(Exception e){}
}
}
}
public class Animation
{
public static void main(String[] args)
{
MyFrame f=new MyFrame();
f.setSize(500,400);
f.setVisible(true);
}
}