-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllipse.java
145 lines (130 loc) · 3.19 KB
/
Ellipse.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
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
//HIDE
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class Ellipse implements Shape
{
private Color color = Color.BLACK;
private boolean filled = false;
private double x;
private double y;
private double width;
private double height;
/**
* Constructs an ellipse.
* @param x the leftmost x-coordinate
* @param y the topmost y-coordinate
* @param width the width of the bounding box
* @param height the height of the bounding box
*/
public Ellipse(double x, double y, double width, double height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* Gets the leftmost x-position of this ellipse.
* @return the leftmost x-position
*/
public int getX()
{
return (int) Math.round(x);
}
/**
* Gets the topmost y-position of this ellipse.
* @return the topmost y-position
*/
public int getY()
{
return (int) Math.round(y);
}
/**
* Gets the width of the bounding box.
* @return the width
*/
public int getWidth()
{
return (int) Math.round(width);
}
/**
* Gets the height of the bounding box.
* @return the height
*/
public int getHeight()
{
return (int) Math.round(height);
}
/**
* Moves this ellipse by a given amount.
* @param dx the amount by which to move in x-direction
* @param dy the amount by which to move in y-direction
*/
public void translate(double dx, double dy)
{
x += dx;
y += dy;
Canvas.getInstance().repaint();
}
public void translateTo(double dx, double dy)
{
x = dx;
y = dy;
}
/**
* Resizes this ellipse both horizontally and vertically.
* @param dw the amount by which to resize the width on each side
* @param dw the amount by which to resize the height on each side
*/
public void grow(double dw, double dh)
{
width += 2 * dw;
height += 2 * dh;
x -= dw;
y -= dh;
Canvas.getInstance().repaint();
}
/**
* Sets the color of this ellipse.
* @param newColor the new color
*/
public void setColor(Color newColor)
{
color = newColor;
Canvas.getInstance().repaint();
}
/**
* Draws this ellipse.
*/
public void draw()
{
filled = false;
Canvas.getInstance().show(this);
}
/**
* Fills this ellipse.
*/
public void fill()
{
filled = true;
Canvas.getInstance().show(this);
}
public String toString()
{
return "Ellipse[x=" + getX() + ",y=" + getY() + ",width=" + getWidth() + ",height=" + getHeight() + "]";
}
public void paintShape(Graphics2D g2)
{
Ellipse2D.Double ellipse = new Ellipse2D.Double(getX(), getY(),
getWidth(), getHeight());
g2.setColor(new Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue()));
if (filled)
{
g2.fill(ellipse);
}
else
{
g2.draw(ellipse);
}
}
}