/*
 * Created on 01.06.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package jdraw.figures;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

import jdraw.framework.FigureEvent;
import jdraw.std.StdHandle;

public class Rectangle extends AbstractFigure {
	java.awt.Rectangle boundingBox;
	
	public Rectangle(int x, int y, int w, int h) {
		boundingBox = new java.awt.Rectangle(x, y, w, h);
	}
	
	/**
	 * Changes the bounds of the figure. The figure has to adjust its size
	 * and position when this method is called, and registered figure listeners
	 * have to be notified.
	 * @param origin the new origin
	 * @param corner the new corner
	 * @see #addFigureListener
	 */
	public void setBounds(Point origin, Point corner) {
		boundingBox.setBounds(Math.min(origin.x, corner.x),
													Math.min(origin.y, corner.y),
													Math.abs(origin.x - corner.x),
													Math.abs(origin.y - corner.y));
		notifyListeners(new FigureEvent(this));
	}

	/**
	 * draw is called when the figure has to be drawn.
	 * @param g Graphics object on which figure has to be drawn.
	 * @see java.awt.Graphics
	 */
	public void draw(Graphics g) {
		g.drawRect(boundingBox.x, boundingBox.y, 
							 boundingBox.width, boundingBox.height);
	}

	/**
	 * Moves the figure. The figure has to adjust its coordinates 
	 * when this method is called, and registered figure listeners
	 * have to be notified.
	 * @param dx move distance in x direction (argument in pixels)
	 * @param dy move distance in y direction (argument in pixels)
	 * @see #addFigureListener
	 */
	public void move(int dx, int dy) {
		boundingBox.setLocation(boundingBox.x + dx,	boundingBox.y + dy);
		notifyListeners(new FigureEvent(this));
	}

	/**
	 * Returns the bounds of a figure. The bounds of a figure  is
	 * a rectangle which contains the figure.
	 * @return bounds of the figure
	 */
	public java.awt.Rectangle getBounds() {
		return new java.awt.Rectangle(boundingBox);
	}

	/**
	 * Returns a list of 8 handles for this Rectangle
	 * @see jdraw.framework.Figure#getHandles()
	 */	
	public java.util.List getHandles() {
		java.util.List handles = new ArrayList(8);
		handles.add(new StdHandle.North(this));
		handles.add(new StdHandle.NorthWest(this));
		handles.add(new StdHandle.West(this));
		handles.add(new StdHandle.SouthWest(this));
		handles.add(new StdHandle.South(this));
		handles.add(new StdHandle.SouthEast(this));
		handles.add(new StdHandle.East(this));
		handles.add(new StdHandle.NorthEast(this));
		return handles;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		return null;
	}

}
