-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
83 lines (69 loc) · 2.21 KB
/
Ball.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Ball -- represents one ball in the Ball World application
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ball implements ActionListener
{
// the Ball's properties
private int canvasWidth, canvasHeight;
private int xpos,ypos;
protected double xvelocity,yvelocity;
protected int diameter;
protected Color color;
// the Ball constructor
// parameters are the width and height of the canvas
public Ball(int width, int height){
canvasWidth = width;
canvasHeight = height;
// the diameter is a random int between 50 and 100
diameter = (int)(50*Math.random())+50;
// the velocity components
xvelocity = ((int)(4*Math.random())+3)*((int)(Math.random()*2))*2-1;
yvelocity = ((int)(4*Math.random())+3)*((int)(Math.random()*2))*2-1;
// the initial (x,y) position of the ball
xpos=(int)(canvasWidth*Math.random());
ypos=(int)(canvasHeight*Math.random());
// pick a random color
color=new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
}
// how the ball moves
public void move(){
int xmax = canvasWidth;
int ymax = canvasHeight;
// update the x position
xpos+=xvelocity;
// if past the right boundary, bounce back to the left
if(xpos+diameter > xmax){
xpos = xpos - 2*(xpos+diameter-xmax);
xvelocity = -xvelocity;
}
// if past the left boundary, bounce back to the right
if(xpos < 0){
xpos = -xpos;
xvelocity = -xvelocity;
}
// update the y position
ypos+=yvelocity;
// if past the bottom boundary, bounce back up
if(ypos+diameter > ymax){
ypos = ypos - 2*(ypos+diameter-ymax);
yvelocity = -yvelocity;
}
// if past the upper boundary, bounce back down
if(ypos < 0){
ypos = -ypos;
yvelocity = -yvelocity;
}
}
// how to draw this ball
public void draw(Graphics g){
g.setColor(color);
g.fillOval(xpos,ypos,diameter,diameter);
}
// how the ball responds to a timer tick
public void actionPerformed(ActionEvent e){
move();
}
}