-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBouncingBall.java
More file actions
executable file
·78 lines (68 loc) · 1.43 KB
/
BouncingBall.java
File metadata and controls
executable file
·78 lines (68 loc) · 1.43 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 BouncingBall extends Applet implements Runnable{
Ball redBall,blackBall;
public void init()
{
redBall=new Ball(80,80,20,2,4,Color.red);
blackBall=new Ball(40,70,20,4,2,Color.black);
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);
g.setColor(blackBall.BallColor);
g.fillOval(blackBall.x, blackBall.y, blackBall.radius, blackBall.radius);
}
public void run()
{
while(true)
{
try
{
displacementOperation(redBall);
displacementOperation(blackBall);
Thread.sleep(100);
repaint();
}
catch(Exception e){}
}
}
//This method checks the boundary condition of ball movement
public void displacementOperation(Ball ball)
{
if(ball.y >= 200 || ball.y <= 0)
{
ball.dy=-ball.dy;
}
if(ball.x >= 200 || ball.x <= 0)
{
ball.dx=-ball.dx;
}
ball.y=ball.y-ball.dy;
ball.x=ball.x-ball.dx;
}
}
/*
<applet code = "BouncingBall.class"
height = 300
width = 300>
</applet>
*/