// Simple turtle graphics package // Create by Lyn on 3/14/97 // Updated on 11/3/97 // Simplified by Randy for CS111 presentation 4/5/05 // Safari bug fixed. See TurtleWorld.java. -Mark 6 Dec 2005 import java.awt.*; // Import Abstract Window Toolkit public class Turtle { private double x; // x position of turtle private double y; // y position of turtle private double heading; // heading of turtle, in degrees private boolean pendown; // is turtle in drawing mode? private Color color; // Red is the default private Graphics gfx; // graphics with which turtle draws private Rectangle bounds; // bounds of drawing surface public Turtle() { color = Color.red; this.x = 0.0; this.y = 0.0; this.heading = 0.0; this.pendown = true; ensureGraphics(); } // Forward public void fd(double dist) { double rads = degreesToRadians(heading); this.moveTo(x + (dist * Math.cos(rads)), y + (dist * Math.sin(rads))); } public void fd (int dist) { fd((double) dist); } // Backward public void bd(double dist) { this.fd(- dist); } public void bd (int dist) { bd((double) dist); } // Left public void lt(double degrees) { this.heading = heading + degrees; } public void lt (int degrees) { lt((double) degrees); } // Right public void rt(double angle) { this.lt(- angle); } public void rt (int degrees) { rt((double) degrees); } // Pen Up public void pu() { this.pendown = false; } // Pen Down public void pd() { this.pendown = true; } // Position public Point getPosition () { return new Point ((int) x, (int) y); } public void setPosition (Point p) { x = (double) p.x; y = (double) p.y; } public void setPosition (int ix, int iy) { x = (double) ix; y = (double) iy; } public void setPosition (double dx, double dy) { x = dx; y = dy; } // Heading public double getHeading () { return heading; } public void setHeading (double degrees) { heading = degrees; } public void setHeading (int degrees) { heading = (double) degrees; } // Color public Color getColor () { return color; } public void setColor (Color c) { color = c; } // Other public void moveTo(double newx, double newy) { // For testing purposes: /* System.println("Move: heading = " + heading + "; x = " + x + "; y = " + y + "; newx = " + newx + "; newy = " + newy); */ if (pendown) { this.gfx.setColor(color); this.gfx.drawLine(screenX(x), screenY(y), screenX(newx), screenY(newy)); } x = newx; y = newy; } private void ensureGraphics () { this.gfx = TurtleWorld.currentWorld.getTurtleGraphics(); //System.err.println("My graphics context is: " + gfx); this.bounds = gfx.getClipBounds(); } private int screenX(double xcoord) { if (bounds == null) throw new RuntimeException("bounds is null!"); return (int) Math.round(bounds.x + (bounds.width / 2) + xcoord); } private int screenY(double ycoord) { return (int) Math.round(bounds.y + (bounds.height / 2) - ycoord); } private static double degreesToRadians (double degrees) { return 2 * Math.PI * (degrees / 360); } }