/*
 * @(#)ShapeGraphicsDrawer.java	1.0 11-Apr-07
 *
 * IIT Bombay, 
 * Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.
 */

package classes.shapes.commands;

import interfaces.IShapeDrawer;

import java.awt.Color;
import java.awt.Graphics;

import types.Command;

/**
 * Draws the shapes based on the <tt>Command</tt> objects
 * using <tt>Graphics</tt> package
 *
 * @author  Prathab, Mukesh
 * @see interfaces.IShapeCommand
 * @see interfaces.IShapeDrawer
 * @see java.awt.Graphics
 * @version 1.0, 22-Mar-07
 */
public class ShapeGraphicsDrawer implements IShapeDrawer {
	private Graphics g = null;
	private Command command = null;
	
	
	/**
	 * Sets the Graphics object
	 * @param g <tt>Grahpics</tt> object to be used to draw shapes
	 */
	public void setGraphicsObject(Graphics g) {
		this.g = g;
	}
	
	/**
	 * Sets the <tt>Command</tt> object
	 * @param command <tt>Command</tt> object to be set
	 */
	public void setCommandObject(Command command) {
		this.command = command;
	}
	
	/**
	 * Draws the shape using <tt>Graphics</tt> package
	 * @see interfaces.IShapeDrawer#drawShape()
	 */
	public void drawShape() {
		if(command.methodName.equals("drawRect")) {
			g.drawRect(Integer.parseInt(command.args[0]),Integer.parseInt(command.args[1]),
					Integer.parseInt(command.args[2]),Integer.parseInt(command.args[3]));
		}
		else if(command.methodName.equals("drawCircle")) {
			g.drawOval(Integer.parseInt(command.args[0]),Integer.parseInt(command.args[1]),
					Integer.parseInt(command.args[2]),Integer.parseInt(command.args[3]));
		}

		else if(command.methodName.equals("fillRect")) {
			Color color = (Color) command.object;
			g.setColor(color);
			g.fillRect(Integer.parseInt(command.args[0]),Integer.parseInt(command.args[1]),
					Integer.parseInt(command.args[2]),Integer.parseInt(command.args[3]));
			g.setColor(Color.black);
		}
		else if(command.methodName.equals("fillCircle")) {
			Color color = (Color) command.object;
			g.setColor(color);
			g.fillOval(Integer.parseInt(command.args[0]),Integer.parseInt(command.args[1]),
					Integer.parseInt(command.args[2]),Integer.parseInt(command.args[3]));
			g.setColor(Color.black);
		}
		else if(command.methodName.equals("fillTriangle")) {
			Color color = (Color) command.object;
			g.setColor(color);
			int x = Integer.parseInt(command.args[0]);
			int y = Integer.parseInt(command.args[1]);
			int h = Integer.parseInt(command.args[2]);
			int w = Integer.parseInt(command.args[3]);
			int[] xPoints = new int[3];
			xPoints[0] = x; xPoints[1]= x+w; xPoints[2] = x + w/2;
			int[] yPoints = new int[3];
			yPoints[0] = y+h; yPoints[1]= y+h; yPoints[2] = y;
			g.fillPolygon(xPoints, yPoints,3);
		}
		else if(command.methodName.equals("drawTriangle")) {
			int x = Integer.parseInt(command.args[0]);
			int y = Integer.parseInt(command.args[1]);
			int h = Integer.parseInt(command.args[2]);
			int w = Integer.parseInt(command.args[3]);
			g.drawLine(x,y+h,x+w,y+h);
			g.drawLine(x+w,y+h,x + w/2,y);
			g.drawLine(x + w/2,y,x,y+h);
		}
		else if(command.methodName.equals("MessageBoard")) {
			g.setColor(Color.blue); 
	    	g.drawString(command.args[0], 20, 300);
	    	g.setColor(Color.black);
		}
		else if(command.methodName.equals("ErrorBoard")) {
			g.setColor(Color.red); 
	    	g.drawString(command.args[0], 20, 300);
	    	g.setColor(Color.black);
		}
	}

}
