// JDraw - Rect class
// Written by Jim Calciano, Melanie Lear, and PJ Waskiewicz
// November 18, 1998
// Class to draw a rectangle from our abstract class DrawObject

import java.awt.*;

public class SRect extends Rect{ 
  private int w=6;
  private int width,height,tempx,tempy;
  private Color color;
  private boolean itsMe=true;
  private boolean object=false;
  private boolean handle=false;
  private int pickedHandle;
  // Constructor for when the rectangle is drawn
  public SRect(int x1, int y1, int x2, int y2, Color color){
    super(x1,y1,x2,y2,color);
    tempx = x1;
    width = x2;
    tempy = y1;
    height = y2;
    this.color = color;
  }
  // Method that returns the attributes of the rectangle to be written
  // to a file
  public String returnString(){
    return (new String("rect" + " " + tempx + " " + tempy + " " + 
                       width + " " + height + " " + color.getRed() + "," +
                       color.getGreen() + "," + color.getBlue()));
  }
  public void paint (Graphics g){
    g.setColor(color);
    g.drawRect(tempx,tempy,width,height);
    g.setColor(Color.black);
    g.fillRect(tempx-(w/2),tempy-(w/2),w,w);
    g.fillRect(tempx+width-(w/2),tempy-(w/2),w,w);
    g.fillRect(tempx-(w/2),tempy+height-(w/2),w,w);
    g.fillRect(tempx+width-(w/2),tempy+height-(w/2),w,w);
  } 
  public void setFirstPoint(int x,int y){
    tempx = x;
    tempy = y;
  }
  public void setSecondPoint(int x, int y){
    width=x;
    height=y;
  }
  public Point firstSelPoint(){
    return(new Point(tempx,tempy));
  }
  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 ulp = new Point(tempx, tempy);
    Point blp = new Point(tempx, tempy+height);
    Point urp = new Point(tempx+width, tempy);
    Point brp = new Point(tempx+width, tempy+height);
    if(((ulp.x <= x)&&(urp.x >= x)) || ((blp.x <= x)&&(brp.x >= x)))
      object=(((ulp.y-4 <= y)&&(urp.y+4 >= y)) || ((blp.y+4 >= y)&&(brp.y-4 <= y)));
    else if(((ulp.y <= y)&&(blp.y >= y)) || ((urp.y <= y)&&(brp.y >= y)))
      object=(((ulp.x-4 <= x)&&(blp.x+4 >= x)) || ((urp.x+4 >= x)&&(brp.x-4 <= x)));
    if ((x>=(tempx-(w/2)))&&(x<=(tempx-(w/2)+w)) && (y>=(tempy-(w/2)))
        && (y<=(tempy-(w/2)+w))){
      handle=true;
      pickedHandle=1;
    }
    else if ((x>=(tempx+(width)-(w/2)))&&(x<=(tempx+(width)-(w/2)+w))
             && (y>=(tempy-(w/2))) && (y<=(tempy-(w/2)+w))){
      handle=true;
      pickedHandle=3;
    }
    else if ((x>=(tempx-(w/2)))&&(x<=(tempx-(w/2)+w)) && (y>=(tempy+(height)-(w/2)))
             && (y<=(tempy+(height)-(w/2)+w))){
      handle=true;
      pickedHandle=6;
    }
    else if ((x>=(tempx+(width)-(w/2)))&&(x<=(tempx+(width)-(w/2)+w))
             && (y>=(tempy+(height)-(w/2))) && (y<=(tempy+(height)-(w/2)+w))){
      handle=true;
      pickedHandle=8;
    }
    if(handle) object=false;
    if(handle || object) 
      return true;
    else return false;
  }
}