// JDraw - Oval class
// Written by Jim Calciano, Melanie Lear, and PJ Waskiewicz
// November 18, 1998
// Class to draw an oval from the abstract class DrawObject

import java.awt.*;

public class SOval extends Oval{
  private int w=6;
  private int x1,y1, height, width;
  private Color color;
  private boolean itsMe=true;
  private boolean object=false;
  private boolean handle=false;
  private int pickedHandle;
  // Constructor for when the oval is being drawn
  public SOval(int x1, int y1, int x2, int y2, Color color){
    super(x1,y1,2*x2,2*y2,color);
    this.x1 = x1;
    width = x2;
    this.y1 = y1;
    height = y2;
    this.color = color;
  }
  // Method to return the attributes of the oval to be written to a file
  public String returnString(){
    return (new String("oval" + " " + x1 + " " + y1 + " " + width + " " + 
                       height + " "  + color.getRed() + "," + color.getGreen()
                       + "," + color.getBlue()));
  }
  public void setFirstPoint(int x, int y){
    x1=x;
    y1=y;
  }
  public void setSecondPoint(int x, int y){
    width=x;
    height=y;
  }
  public void paint (Graphics g){
    g.setColor(color);
    g.drawOval(x1-width,y1-height,2*width,2*height);
    g.setColor(Color.black);
    g.fillRect(x1-width-w/2,y1-w/2,w,w);
    g.fillRect(x1+width-w/2,y1-w/2,w,w);
    g.fillRect(x1-w/2,y1+height-w/2,w,w);
    g.fillRect(x1-w/2,y1-height-w/2,w,w);
  }
  public Point firstSelPoint(){
    return(new Point(x1,y1));
  }
  public Point secondSelPoint(){
    return(new Point(width,height));
  }
  public Color selColor(){
    return color;
  }
  public void setSelColor(Color c){
    color=c;
  }
  public boolean areYouSelected(){
    return itsMe;
  }
  public void youAreSelected(boolean b){
    itsMe=b;
  }
  public boolean getIfObjectClicked(){
    return object;
  }
  public boolean getIfHandleClicked(){
    return handle;
  }
  public int getPickedHandle(){
    return pickedHandle;
  }
  public boolean intersects(int x, int y){
    Point center = new Point(x1, y1);
    double answer1, answer2, answer;
    answer1 = ((Math.pow(x-center.x,2))/((Math.pow(width,2))));
    answer2 = ((Math.pow(y-center.y,2))/((Math.pow(height,2))));
    answer = answer1+answer2;
    object=(answer >= .85 && answer <= 1.15);
    if((x>=(x1-width-(w/2)))&&(x<=(x1-width-(w/2)+w))&&
       (y>=(y1-(w/2))&&(y<=(y1-(w/2)+w)))){
      handle=true;
      pickedHandle=1;
    }
    else if((x>=(x1+width-(w/2)))&&(x<=(x1+width-(w/2)+w))&&
       (y>=(y1-(w/2))&&(y<=(y1-(w/2)+w)))){
      handle=true;
      pickedHandle=2;
    }
    else if((x>=(x1-(w/2)))&&(x<=(x1-(w/2)+w))&&
       (y>=(y1-height-(w/2))&&(y<=(y1-height-(w/2)+w)))){
      handle=true;
      pickedHandle=3;
    }
    else if((x>=(x1-(w/2)))&&(x<=(x1-(w/2)+w))&&
       (y>=(y1+height-(w/2))&&(y<=(y1+height-(w/2)+w)))){
      handle=true;
      pickedHandle=4;
    }
    if(handle) object=false;
    if(handle || object) 
      return true;
    else return false;
  }
}