/*
 * Created on Apr 20, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.figures;

import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.List;

import jdraw.framework.*;
import jdraw.std.StdHandle;

/**
 * @author Christoph Denzler
 */
public class Ellipse extends AbstractFigure {
	
	private Ellipse2D ellipse;
	
	public Ellipse(int cx, int cy, int radius) {
		ellipse = new Ellipse2D.Float(cx-radius, cy-radius, 2*radius, 2*radius);
	}

	public Ellipse(Point p1, Point p2) {
		ellipse = new Ellipse2D.Float(Math.min(p1.x, p2.x),
																  Math.min(p1.y, p2.y),
																	Math.abs(p1.x - p2.x),
																	Math.abs(p1.y - p2.y));
	}
	
	/**
	 * Tests whether the mouse coordinates are contained in the ellipse.
	 * contains is called when the mouse is pressed in the grafic in
	 * order to decide which figure has to be selected.
	 * 
	 * @param x x-coordinate of mouse position
	 * @param y y-coordinate of mouse position
	 * @return <tt>true</tt>, if coordinates are contained in the figure, 
	 *         <tt>false</tt> otherwise
	 */
	public boolean contains(int x, int y) {
		return ellipse.contains(x, y);
	}

	/**
	 * Setting the bounding box of an ellipse also resizes the ellipse
	 * itself.
	 * @see jdraw.framework.Figure#setBounds(Point, Point)
	 */
	public void setBounds(Point origin, Point corner) {
		ellipse.setFrame(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 Ellipse
	 * @see jdraw.framework.Figure#draw(Graphics)
	 */
	public void draw(Graphics g) {
		Color tmp = g.getColor();
		g.drawOval((int)ellipse.getX(), (int)ellipse.getY(),
							 (int)ellipse.getWidth(), (int)ellipse.getHeight());
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Figure#move(int, int)
	 */
	public void move(int dx, int dy) {
		ellipse.setFrame(ellipse.getX()+dx,
										 ellipse.getY()+dy,
										 ellipse.getWidth(),
										 ellipse.getHeight());
		notifyListeners(new FigureEvent(this));
	}

	/* (non-Javadoc)
	 * @see jdraw.framework.Figure#getBounds()
	 */
	public java.awt.Rectangle getBounds() {
		return new java.awt.Rectangle((int)ellipse.getX(),
																	(int)ellipse.getY(),
																	(int)ellipse.getWidth(),
																	(int)ellipse.getHeight());
	}
	
	public int getWidth() {
		return (int)ellipse.getWidth();
	}

	public int getHeight() {
		return (int)ellipse.getHeight();
	}

	/**
	 * Returns a list of 8 handles for this Ellipse
	 * @see jdraw.framework.Figure#getHandles()
	 */	
	public List getHandles() {
		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;
	}

	/**
	 * Copies the internals of Rectangle.
	 * 
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		return null;
	}

}
