// 
// Copy this file in its entirety to a file named Turtle.java
// Compile it before trying to compile any program that uses Turtles


import java.awt.*;
import javax.swing.JFrame;
import java.awt.geom.*;
import java.awt.event.*;

public class Turtle extends Object
{	public static final double DEGREE = Math.PI / 180; 
	public static final int WIDTH = 760, HEIGHT = 600;
	public static final Color RED = Color.red, BLUE = Color.blue,
	               BLACK = Color.black,      GRAY = Color.gray, 
	               YELLOW = Color.yellow,    PINK = Color.pink,
	               ORANGE = Color.orange,    GREEN = Color.green,
	               MAGENTA = Color.magenta,  WHITE = Color.white;
	private static Graphics2D page = null;     // for drawing 
	//////////////////////////////////
	private double heading = 0;         // heading initially east
	private double xcor = WIDTH / 2;    // centered horizontally
	private double ycor = HEIGHT / 2;   // centered vertically too

	/** Set the drawing Color to the given value.  */

	public void switchTo (Color given)
	{	page.setColor (given);
	}	//======================

	/** Write words without changing the Turtle's state.  */

	public void say (String message)
	{	page.drawString (message, (int) xcor, (int) ycor);
	}	//======================

	/** Pause the animation for wait milliseconds.  */

	public void sleep (int wait)
	{	long timeToQuit = System.currentTimeMillis() + wait;
		while (System.currentTimeMillis() < timeToQuit)
			;   // take no action
	}	//======================

	/** Make a circle of the given radius, Turtle at center. */

	public void swingAround (double radius)
	{	if (radius > 0.0)
			page.draw (new Ellipse2D.Double (xcor - radius, 
			            ycor - radius, 2 * radius, 2 * radius));
	}	//======================


// the Turtle class, completed

	/** Rotate left by left degrees; MOVE for forward steps.*/

	public Turtle move (double left, double forward)
	{	heading += left * DEGREE;
		xcor += forward * Math.cos (heading);
		ycor -= forward * Math.sin (heading);
		return this;
	}	//======================

	/** Rotate left by left degrees; PAINT for forward steps. */

	public Turtle paint (double left, double forward)
	{	heading += left * DEGREE;
		double x = xcor + forward * Math.cos (heading);
		double y = ycor - forward * Math.sin (heading);
		page.draw (new Line2D.Double (xcor, ycor, x, y));
		xcor = x;
		ycor = y;
		return this;
	}	//======================

	/** Fill a circle of the given radius, Turtle at center. */

	public void fillCircle (double radius)
	{	page.fill (new Ellipse2D.Double (xcor - radius, 
		            ycor - radius, 2 * radius, 2 * radius));
	}	//======================

	/** Fill a rectangle of the given width and height, Turtle at center. */

	public void fillBox (double width, double height)
	{	page.fill (new Rectangle2D.Double (xcor - width / 2,
		           ycor - height / 2, width, height));
	}	//======================

	public Turtle (Graphics g)
	{	page = (Graphics2D) g;
	}	//======================

	public Turtle()
	{	if (page == null)
		{	JFrame area = new JFrame ("Turtle drawings");
			area.addWindowListener (new Closer());  // in Chapter 10
			area.setSize (WIDTH, HEIGHT);
			area.setVisible (true);
			page = (Graphics2D) area.getGraphics();
			area.paint (page);
			page.setColor (WHITE);
			page.fillRect (0, 0, WIDTH, HEIGHT);
			page.setColor (BLACK);
		}
	}	//======================


	private static class Closer implements WindowListener 
	{
	   /** Enable the closer icon to close the window. */

		public void windowClosing (WindowEvent e) 
		{	System.exit (0);
		}	//======================

		public void windowActivated (WindowEvent e)     { }
		public void windowDeactivated (WindowEvent e)   { }
		public void windowIconified (WindowEvent e)     { }
		public void windowDeiconified (WindowEvent e)   { }
		public void windowOpened (WindowEvent e)        { }
		public void windowClosed (WindowEvent e)        { }
	}     
}

// 
