// 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 Rect extends DrawObject{ 
  private int bigx, bigy, smallx, smally;
  private int x1,x2,y1,y2,width,height,tempx,tempy;
  private Color color;
  // Constructor for when the rectangle is drawn
  public Rect(int x1, int y1, int x2, int y2, Color color){
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
    // Make sure the rectangle is being drawn in the right direction
    if (x2 > x1){
      bigx = x2;
      tempx = x1;
      smallx = x1;
    }
    else {
      bigx = x1;
      tempx = x2;
      smallx = x2;
    }
    if (y2>y1) {
      bigy=y2;
      tempy = y1;
      smally=y1;
    }
    else{
      bigy=y1;
      tempy = y2;
      smally=y2;
    }
    width = bigx - smallx;
    height = bigy - smally;
    this.color = color;
  }
  // Constructor for when the rectangle is created from opening a file
  public Rect(int x1, int y1, int x2, int y2, Color color, String s){
    tempx = x1;
    tempy = y1;
    this.color = color;
    width = x2;
    height = y2;
  }
  // 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);
  } 
  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){}
  public boolean areYouSelected(){
    return false;
  }
  public void youAreSelected(boolean b){
  }
  public boolean getIfObjectClicked(){
    return false;
  }
  public boolean getIfHandleClicked(){
    return false;
  }
  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))){
      if(((ulp.y-4 <= y)&&(urp.y+4 >= y)) || ((blp.y+4 >= y)&&(brp.y-4 <= y)))
        return true;
    }
    if(((ulp.y <= y)&&(blp.y >= y)) || ((urp.y <= y)&&(brp.y >= y)))
      return (((ulp.x-4 <= x)&&(blp.x+4 >= x)) || ((urp.x+4 >= x)&&(brp.x-4 <= x)));
    return false;
  }
}