-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
74 lines (61 loc) · 1.13 KB
/
Ball.java
File metadata and controls
74 lines (61 loc) · 1.13 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
import java.awt.*;
//
public class Ball
{
public static final int RADIUS = 10;
public static final int SPEED = 10;
private int x, y;
private double dx, dy;
public Ball(int x, int y)
{
this(x,y,0,0);
}
public Ball(int x, int y, double dx, double dy)
{
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
public Ball moveLeft()
{
return new Ball(x-SPEED, y, dx, dy);
}
public Ball moveRight()
{
return new Ball(x+SPEED, y, dx, dy);
}
public Ball accelerate(double ax, double ay)
{
return new Ball(x, y, dx+ax, dy+ay);
}
public Ball setVelocity(double dx, double dy)
{
return new Ball(x, y, dx, dy);
}
public Ball setPosition(int x, int y)
{
return new Ball(x, y, 0, 0);
}
public Ball move(int dx, int dy)
{
return new Ball(x+dx, y+dy, 0, 0);
}
public Ball move()
{
return new Ball(x+(int)dx, y+(int)dy, dx, dy);
}
public Rectangle getBounds()
{
return new Rectangle(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2);
}
public Point getLocation()
{
return new Point(x,y);
}
public void draw(Graphics g)
{
g.setColor(Color.BLACK);
g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2);
}
}