
import java.awt.*;
import java.io.*;
import java.util.*;

class Oval extends Figure {
  int _width;
  int _height;

    Oval( String color, int x0, int y0, int width, int height ) {
	super( color, x0, y0 );
	
	_width = width;
	_height = height;
    }

    public void resize( int x0, int y0, int x1, int y1 ) {
	if ( x0 > x1 ) { int tmp = x0; x0 = x1; x1 = tmp; }
	if ( y0 > y1 ) { int tmp = y0; y0 = y1; y1 = tmp; }
	_x0 = x0;
	_y0 = y0;
	_width = x1 - x0;
	_height = y1 - y0;
    }

    public String toString() {
	String s = "Oval|";
	s += super.toString();
	s += _width + "|" + _height +"\n";
	return s;
    }

    Oval( StringTokenizer t ) {
	super( t );

	Integer i = new Integer( t.nextToken() );
	_width = i.intValue();
	
	i = new Integer( t.nextToken() );
	_height = i.intValue();
    }

    public void paint( Graphics g ) {
	g.setColor( getColor() );      
	g.fillOval( getX0(), getY0(), _width, _height );
    }

    public boolean isSelected( int x, int y ) {
	double xc = _x0 + _width/2;
	double yc = _y0 + _height/2;
	double dx1 = x-xc;
	double dy1 = y-yc;
	double t = Math.atan2( dy1, dx1 );
	double dx2 = _width*Math.cos( t )/2;
	double dy2 = _height*Math.sin( t )/2;
	
	if (  dx1*dx1 + dy1*dy1 <= dx2*dx2 + dy2*dy2 ) {
	    return true;
	}
	else {
	    return false;
	}
    }

    // Return true if figure is selected using these coordinates
    public boolean isInBox( int x0, int y0, int width, int height ) {
	if ( x0 <= _x0 && x0 + width >= _x0 + _width && 
	     y0 <= _y0 && y0 + height >= _y0 + _height ) {
	    return true;
	}
	else {
	    return false;
	}
    }

    public void paintSelected( Graphics g ) {
	g.setColor( Color.blue.brighter() );
	g.drawRect( _x0-2, _y0-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0+_height-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0-2, 4, 4 );
	g.drawRect( _x0-2, _y0+_height-2, 4, 4 );
    }

    public int getMinX() {
	return _x0;
    }

    public int getMaxX() {
	return _x0 + _width;
    }

    public int getMinY() {
	return _y0;
    }

    public int getMaxY() {
	return _y0 + _height;
    }

}


