-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCircle.java
More file actions
164 lines (147 loc) · 3.7 KB
/
Copy pathCircle.java
File metadata and controls
164 lines (147 loc) · 3.7 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.awt.Graphics;
import java.awt.Color;
/**
* A circle that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes and Brian Dahlem
* @version 2018.11.26
*/
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private Color color;
private boolean isVisible;
/**
* Create a new circle at default position with default color.
*/
public Circle()
{
diameter = 68;
xPosition = 230;
yPosition = 90;
color = Canvas.getColor("blue");
}
/**
* Make this circle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
if (!isVisible) {
isVisible = true;
add();
}
}
/**
* Make this circle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
if (isVisible){
remove();
isVisible = false;
}
}
/**
* Move the circle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the circle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the circle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the circle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the circle horizontally by 'distance' pixels.
* @param distance the distance to move the circle along the x axis,
* positive to the right
*/
public void moveHorizontal(int distance)
{
xPosition += distance;
}
/**
* Move the circle vertically by 'distance' pixels.
* @param distance the distance to move the circle along the y axis,
* positive down
*/
public void moveVertical(int distance)
{
yPosition += distance;
}
/**
* Change the size to the new size (in pixels). Size must be >= 0.
* @param newDiameter the diameter of the circle
*/
public void changeSize(int newDiameter)
{
diameter = newDiameter;
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta", "cyan", "brown", "white", and "black", or rgb hex strings
* "#rrggbb" where rr, gg, bb are 2-hexit values for red, green, and
* blue levels.
* @param newColor a lower case string naming the color to change to
*/
public void changeColor(String newColor)
{
color = Canvas.getColor(newColor);
}
/**
* Add the circle to the screen.
*/
private void add()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.add(this,(g) -> {g.setColor(color);
g.fillOval(xPosition, yPosition, diameter, diameter);});
}
}
/**
* Remove the circle from the screen.
*/
private void remove()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.remove(this);
}
}
/**
* Get a text description of the circle.
*/
public String toString() {
String visibility;
if (isVisible) {
visibility = "Visible";
}
else {
visibility = "Invisible";
}
return visibility + " Circle of diameter " + this.diameter +
" at (" + this.xPosition + ", " + this.yPosition + ")" +
" with color " + this.color;
}
}