-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTriangle.java
More file actions
180 lines (162 loc) · 4.45 KB
/
Copy pathTriangle.java
File metadata and controls
180 lines (162 loc) · 4.45 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.awt.Graphics;
import java.awt.Color;
/**
* A triangle 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 Triangle
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private Color color;
private boolean isVisible;
/**
* Create a new triangle at default position with default color.
*/
public Triangle()
{
height = 60;
width = 70;
xPosition = 210;
yPosition = 140;
color = Canvas.getColor("green");
isVisible = false;
}
/**
* Make this triangle visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
if (!isVisible)
{
isVisible = true;
add();
}
}
/**
* Make this triangle invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
if (isVisible)
{
remove();
isVisible = false;
}
}
/**
* Move the triangle a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the triangle a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the triangle a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the triangle a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the triangle horizontally by 'distance' pixels.
* @param distance the distance to move the triangle along the x axis,
* positive to the right
*/
public void moveHorizontal(int distance)
{
xPosition += distance;
}
/**
* Move the triangle vertically by 'distance' pixels.
* @param distance the distance to move the triangle along the y axis,
* positive down
*/
public void moveVertical(int distance)
{
yPosition += distance;
}
/**
* Change the size of the triangle (in pixels).
* @param newHeight the new height of the triangle, positive points up, negative points down
* @param newWidth the new width of the triangle, must be > 0
*/
public void changeSize(int newHeight, int newWidth)
{
height = newHeight;
width = newWidth;
}
/**
* 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 string naming the new color for the triangle
*/
public void changeColor(String newColor)
{
color = Canvas.getColor(newColor);
}
/**
* Draw a triangle with current specifications on the screen.
*/
private void add()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.add(this, (g) -> {int[] xpoints = {xPosition,
xPosition + (width/2),
xPosition - (width/2)};
int[] ypoints = {yPosition,
yPosition + height,
yPosition + height};
g.setColor(color);
g.fillPolygon(xpoints, ypoints, 3);});
}
}
/**
* Remove the triangle from the screen.
*/
private void remove()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.remove(this);
}
}
/**
* Get a text description of the triangle.
*/
public String toString() {
String visibility;
if (isVisible) {
visibility = "Visible";
}
else {
visibility = "Invisible";
}
return visibility + " Triangle" +
" with width " + this.width +
" height " + this.height +
" at (" + this.xPosition + ", " + this.yPosition + ")" +
" with color " + this.color;
}
}