import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;


@SuppressWarnings("serial")
public abstract class TurtlePanel extends JPanel {
	protected Graphics2D g;
	protected boolean isPenUp;
	protected int width, height;
	protected double x, y, angle;
	
	public void up() {
		isPenUp = true;
	}
	
	public void down() {
		isPenUp = false;
	}
	
	public void color(Color color) {
		g.setColor(color);
	}
	
	public void hue(double hue) {
		color(new Color(Color.HSBtoRGB((float) hue, 1, 1)));

	}
	
	private void drawTo(double newX, double newY) {
		if (!isPenUp)
			g.draw(new Line2D.Double(x, y, newX, newY));
		x = newX; 
		y = newY;
	}
	
	public void forward(double pixels) {
		drawTo(x + Math.cos(angle) * pixels, y + Math.sin(angle) * pixels);
	}
	
	public void backward(double pixels) {
		drawTo(x - Math.cos(angle) * pixels, y - Math.sin(angle) * pixels);
	}
	
	public void left(double degrees) {
		angle -= degrees * Math.PI / 180;
	}
	
	public void right(double degrees) {
		angle += degrees * Math.PI / 180;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		this.g = (Graphics2D) g;
		this.g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
				RenderingHints.VALUE_ANTIALIAS_ON);
		color(Color.WHITE);
		width = getWidth();
		height = getHeight();
		this.g.fill(new Rectangle2D.Double(0, 0, width, height));
		x = width / 2.0;
		y = height / 2.0;
		angle = 0;
		color(Color.BLACK);
		turtleMain();
	}
	
	public abstract void turtleMain();
}
