Version 2.0 of Turtle Graphics is similar to version 1.0 except a new command is added:
-> color RED GREEN BLUE
Where
0 <= RED, GREEN, BLUE < 256
This command sets the color of the turtle's pen. As the turtle moves, he draws a line the color of his pen.
In this example we ask the turtle to draw a flag:
Here is the command sequence. First the flag:
-> turn E
done
-> color 255 0 0
done
-> move 100
done
-> turn S
done
-> color 0 255 0
done
-> move 50
done
-> turn W
done
-> color 0 0 255
done
-> move 100
done
-> turn N
done
-> color 255 0 255
done
-> move 50
done
Next the pole:
-> turn S
done
-> color 0 0 0
done
-> move 200
done
And now the turtle moves away:
-> color 255 255 255
done
-> turn E
done
-> move 100
done
->
Here's a shot of the console window where the commands are entered:
Exactly the same as version 1.0.
Also the same as version 1.0 except we add a new field:
private Color color = Color.BLACK;
It might be a good idea to add setter and getter methods for this field and getter methods for the xc and yc fields.
Each time the turtle moves, it creates a colored point object (aka a turtle turd) and adds it to an array of turds:
class TurtleTurd { // aka ColoredPoint
private Color color;
private int xc, yc;
public TurtleTurd(Turtle t) {
xc = t.getXC();
yc = t.getYC();
color
= t.getColor();
}
public int getXC() { return xc; }
public int getYC() { return yc; }
public Color getColor() { return color;
}
}
The new parts of the TurtleComponent class are shown in boldface. We add an array of turds. The constructor drops a single turd in the array marking the turtle's initial position and color.
The paintComponent method asks the turtle to draw himself, then traverses the turds array connecting each turd in the array to the next by a line segment.
Finally, the control loop adds the code for the new color command as well as a line to the move command that adds a new turd to the turds array after each move:
class TurtleComponent extends JComponent {
private Turtle turtle = new Turtle();
private Scanner kbd = new
Scanner(System.in);
private
int cap = 500;
private
TurtleTurd[] turds = new TurtleTurd[cap];
private int size = 0;
public TurtleComponent() {
turds[size++] = new
TurtleTurd(turtle);
setBackground(Color.WHITE);
}
public void paintComponent(Graphics gc)
{
Graphics2D gc2d = (Graphics2D)gc;
turtle.draw(gc2d);
for(int i = 1; i < size; i++) {
TurtleTurd last = turds[i - 1];
TurtleTurd current = turds[i];
// draw a colored line from last to current
}
}
public void controlLoop() {
while(true) {
System.out.print("->
");
String token = kbd.next();
if
(token.equals("quit")) {
System.out.println("bye");
System.exit(0);
} else if
(token.equals("move")) {
int steps = kbd.nextInt();
turtle.move(steps);
// drop a new turd in the turds array
repaint();
System.out.println("done");
} else if
(token.equals("turn")) {
String dir = kbd.next();
turtle.setHeading(dir.charAt(0));
repaint();
System.out.println("done");
} else if
(token.equals("color")) {
// read amount of red green and blue from kbd
// set turtle color
System.out.println("done");
} else {
System.err.println("unrecognized
command: " + token);
}
}
}
}