-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle.java
117 lines (93 loc) · 2.88 KB
/
Circle.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Represents a circle
import java.awt.*;
import java.util.Random;
public class Circle{
private int centerX, centerY, radius;
private Color color;
private int direction, speed;
private boolean filled;
private Random gen;
//public Circle(int x, int y, int r, Color c){
public Circle(int x, int y, int r){
centerX = x;
centerY = y;
radius = r;
color = Color.black;
direction = 0;
speed = 0;
filled = false;
gen = new Random();
}
public void draw(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
// Translates circle's center to rectangle's origin for drawing.
if (filled)
g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
else
g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public void fill(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
// Translates circle's center to rectangle's origin for drawing.
g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y){
int xSquared = (x - centerX) * (x - centerX);
int ySquared = (y - centerY) * (y - centerY);
int radiusSquared = radius * radius;
return xSquared + ySquared - radiusSquared <= 0;
}
public void move(int xAmount, int yAmount){
centerX = centerX + xAmount;
centerY = centerY + yAmount;
}
public void setColor(){
int red = gen.nextInt(256);
int green = gen.nextInt(256);
int blue = gen.nextInt(256);
color = new Color(red, green, blue);
}
public int getRadius(){
return radius;
}
public void setRadius(){
//set random radius here at 10 min
radius = gen.nextInt(31) + 10;
}
public int getX(){
return centerX;
}
public int getY(){
return centerY;
}
public void setY(){
centerY = 0 - radius;
}
public int getDirection(){
return direction;
}
public void setSpeed(){
speed = gen.nextInt(6) + 5;
//speed = 1; //for testing
}
public void setDirection(int d){
direction = d % 360;
}
public void turn(int degrees){
direction = (direction + degrees) % 360;
}
//Moves the circle in the current direction using its current speed
public void move(){
move((int)(speed * Math.cos(Math.toRadians(direction))), (int)(speed * Math.sin(Math.toRadians(direction))));
}
public void moveScreen(){
move((int)(speed * Math.cos(Math.toRadians(direction))), (int)(-1 * speed * Math.sin(Math.toRadians(direction))));
}
public void setFilled(boolean b){
filled = b;
}
}