-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtle.java
More file actions
266 lines (228 loc) · 6.3 KB
/
Turtle.java
File metadata and controls
266 lines (228 loc) · 6.3 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Simple Turtle library
* Version 1.03 - 2016.11.01
* @author Davide Fossati <davide@fossati.us>
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class Turtle {
public static final int WINDOW_WIDTH = 1000;
public static final int WINDOW_HEIGHT = 1000;
public static final int WORLD_WIDTH = 5000;
public static final int WORLD_HEIGHT = 5000;
public Turtle() {
if (world == null) {
initWorld();
}
if (allTurtles == null) {
allTurtles = new ArrayList<Turtle>();
}
allTurtles.add(this);
x = WINDOW_WIDTH / 2;
y = WINDOW_HEIGHT / 2;
dir = 0;
turtleColor = Color.BLACK;
delay = 100;
turtleVisible = true;
penDown = true;
}
private void initWorld() {
JFrame worldFrame = new JFrame("Turtle");
worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
world = new PaintArea();
worldFrame.add(world);
worldFrame.pack();
worldFrame.setVisible(true);
}
/*
* Moves the turtle forward.
* @param steps the distance traveled by the turtle (1 step = 1 pixel)
*/
public void forward(double steps) {
double x0 = x;
double y0 = y;
x += steps * Math.cos(dir);
y -= steps * Math.sin(dir);
if (penDown) {
world.drawLine(x0, y0, x, y, turtleColor);
}
if (delay > 0) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
world.repaint();
}
/*
* Moves the turtle backward.
* @param steps the distance traveled by the turtle (1 step = 1 pixel)
*/
public void backward(double steps) {
forward(-steps);
}
/*
* Rotates the turtle counter-clockwise.
* @param degrees the rotation angle (in degrees)
*/
public void left(double degrees) {
dir += degrees * Math.PI / 180;
double pi2 = 2 * Math.PI;
while (dir >= pi2) {
dir -= pi2;
}
while (dir < 0) {
dir += pi2;
}
if (delay > 0) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
world.repaint();
}
/*
* Rotates the turtle clockwise.
* @param degrees the rotation angle (in degrees)
*/
public void right(double degrees) {
left(-degrees);
}
/*
* Raises the turtle's pen. When the pen is up, the turtle does not draw lines on the screen.
*/
public void penup() {
penDown = false;
}
/*
* Lowers the turtle's pen. When the pen is down, the turtle draws lines on the screen.
*/
public void pendown() {
penDown = true;
}
/*
* Sets the turtle's delay between moves. The lower the delay, the faster the turtle. Default: 100 ms
* @param delay the amount of delay (in milliseconds)
*/
public void delay(int delay) {
this.delay = delay;
}
/*
* Hides the turtle so it is not visible on the screen.
*/
public void hideturtle() {
turtleVisible = false;
world.repaint();
}
/*
* Shows the turtle so it is visible on the screen.
*/
public void showturtle() {
turtleVisible = true;
world.repaint();
}
/*
* Sets the color of the turtle.
* @param color a string representation of the color. Examples: "red", "green", "blue", "yellow", etc.
*/
public void color(String color) {
try {
turtleColor = (Color)Class.forName("java.awt.Color").getField(color).get(null);
} catch (Exception e) {
e.printStackTrace();
}
world.repaint();
}
/*
* Sets the color of the turtle.
* @param red the red component of the color (0 <= red <= 255)
* @param green the blue component of the color (0 <= green <= 255)
* @param blue the blue component of the color (0 <= blue <= 255)
*/
public void color(int red, int green, int blue) {
turtleColor = new Color(red, green, blue);
world.repaint();
}
/*
* Flood-fills the area under the turtle with the turtle's current color.
*/
public void fill() {
world.fill(x, y, turtleColor);
world.repaint();
}
private static PaintArea world;
private static ArrayList<Turtle> allTurtles;
private double x; // x position of the turtle
private double y; // y position of the turtle
private double dir; // direction of the turtle in radians
private Color turtleColor; // color of the turtle
private long delay; // delay of the turtle
private boolean turtleVisible; // true if the turtle is visible
private boolean penDown; // true if the pen is down
class PaintArea extends JPanel {
private static final long serialVersionUID = 1L;
public PaintArea() {
this.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
drawingBuf = new BufferedImage(WORLD_WIDTH, WORLD_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = drawingBuf.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WORLD_WIDTH, WORLD_HEIGHT);
}
public void drawLine(double x1, double y1, double x2, double y2, Color c) {
Graphics2D g = drawingBuf.createGraphics();
g.setColor(c);
g.drawLine((int)Math.round(x1), (int)Math.round(y1), (int)Math.round(x2), (int)Math.round(y2));
}
public void fill(double x0, double y0, Color foreground) {
int ix0 = (int)Math.round(x0);
int iy0 = (int)Math.round(y0);
int fgc = foreground.getRGB();
int bgc = drawingBuf.getRGB(ix0, iy0);
if (fgc == bgc) {
return;
}
Queue<Point> q = new ArrayDeque<Point>();
q.add(new Point(ix0, iy0));
while (!q.isEmpty()) {
Point p = q.remove();
if (p.x >=0 && p.x < drawingBuf.getWidth()
&& p.y >=0 && p.y < drawingBuf.getHeight()
&& drawingBuf.getRGB(p.x, p.y) == bgc) {
drawingBuf.setRGB(p.x, p.y, fgc);
q.add(new Point(p.x-1, p.y));
q.add(new Point(p.x+1, p.y));
q.add(new Point(p.x, p.y-1));
q.add(new Point(p.x, p.y+1));
}
}
}
public void paint(Graphics g) {
g.drawImage(drawingBuf, 0, 0, Color.WHITE, null);
for (Turtle t : allTurtles) {
if (t.turtleVisible) {
// draw the turtle
g.setColor(t.turtleColor);
double tw = 7;
double th = 10;
double sind = Math.sin(t.dir);
double cosd = Math.cos(t.dir);
int tx1 = (int)(t.x + th * cosd);
int ty1 = (int)(t.y - th * sind);
int tx2 = (int)(t.x - th * cosd - tw * sind);
int ty2 = (int)(t.y + th * sind - tw * cosd);
int tx3 = (int)(t.x - th * cosd + tw * sind);
int ty3 = (int)(t.y + th * sind + tw * cosd);
g.drawLine(tx1, ty1, tx2, ty2);
g.drawLine(tx1, ty1, tx3, ty3);
g.drawLine(tx2, ty2, tx3, ty3);
}
}
}
private BufferedImage drawingBuf;
}
}