package gizmoball.ui.r2;

import gizmoball.game.AbstractGizmo;
import gizmoball.game.FlipperGizmo;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;

import physics.Circle;
import physics.Vect;

/**
 * A drawer for AbstractGizmosWithPolygonalGeometry.  This drawer
 * can handle SquareGizmos, TriangleGizmos, Absorbers, OuterWalls,
 * and all other imaginable polygonal gizmos. <p>
 * 
 * @see R2DrawerFactory
 * @see gizmoball.game.AbstractGizmoWithPolygonalGeometry
 * @author Albert Leung
 * @version $Id: R2FlipperDrawer.java,v 1.4 2004/04/28 02:58:07 amthrax Exp $
 *  
 */
public class R2FlipperDrawer implements R2GizmoDrawer {

	/**
	 * The singleton R2FlipperDrawer returned by the R2DrawerFactory.
	 * 
	 */
	public static final R2FlipperDrawer DRAWER = new R2FlipperDrawer();

	/**
	 * Draws a flipper gizmo.
	 * 
	 * @param g the Graphics context on which to draw
	 * @param gizmo the gizmo from which to extract properties for
	 * representation
	 * @modifies g
	 * 
	 */
	public void drawGizmo(Graphics2D g, AbstractGizmo gizmo) {
		Stroke st =g.getStroke();
		// TODO: make flipper thickness more general
		g.setStroke(new BasicStroke(L_IN_PIXELS/2, BasicStroke.CAP_ROUND, 
			BasicStroke.JOIN_ROUND));
		g.setColor(Color.RED);
		FlipperGizmo flip = (FlipperGizmo)gizmo;
		Circle c1 = flip.getEndpoint1();
		Circle c2 = flip.getEndpoint2();
		Vect p1 = c1.getCenter();
		Vect p2 = c2.getCenter();
		int x1 = (int)Math.round(p1.x()*L_IN_PIXELS);
		int x2 = (int)Math.round(p2.x()*L_IN_PIXELS);
		int y1 = (int)Math.round(p1.y()*L_IN_PIXELS);
		int y2 = (int)Math.round(p2.y()*L_IN_PIXELS);
		g.drawLine(x1,y1,x2,y2);
		g.setColor(Color.YELLOW);
		g.fillOval(x1-1, y1-1, 2, 2);
		g.setStroke(st);
	}

	/**
	 * Returns the singleton instance.
	 * 
	 * @return the only instance of an R2FlipperDrawer
	 * 
	 */
	public static R2GizmoDrawer getInstance() {
		return DRAWER;
	}

}
