package wheels.users;

import wheels.Scalable;
import java.awt.Color;
import java.awt.Dimension;

/**
 * A Square is a kind of Rectangle in which the 
 * width and height are always equal.
 * 
 * @author Zach Tomaszewski
 * @version 12 Oct 2007
 */
public class Square extends Rectangle implements Scalable {

  
  /**
   * Constructs a new Square with Rectangle's defaults,
   * except that the height is always equal to the width.
   */
  public Square() {
    super();
    //ensure that a new square always has width == height
    int width = this.getWidth();
    this.setSize(width, width);
  }
  
  /**
   * Creates a new Square with the given details.
   * 
   * @param x           Horizontal position
   * @param y           Vertical position
   * @param sideLength  Equals both width and height
   * @param c           The color of the new square
   */
  public Square(int x, int y, int sideLength, Color c) {
    super(x, y); //let Rectangle set location of shape
    this.setColor(c);
    this.setSize(sideLength, sideLength);
  }
  
  
  /**
   * Sets this square's width and height to the smaller
   * of the two given values.
   */
  public void setSize(int w, int h) {
    int smaller = Math.min(w, h);
    /*XXX: Calling the super's setSize method that actually does the work. */
    super.setSize(new Dimension(smaller, smaller));
  }
  
  /**
   * Sets this square's width and height to the smaller
   * of the two given values.
   */  
  public void setSize(Dimension d) {
    int width = (int) d.getWidth();
    int height = (int) d.getHeight();
    //reuse above setSize method, which will determine which param is smaller
    this.setSize(width, height);
  }
  
  /**
   * @see Scalable#scale(double)
   */
  public void scale(double percent) {
    int newWidth =  (int) (this.getWidth() * percent);
    this.setSize(newWidth, newWidth);
  }
  
  
  /**
   * Tests a Square by creating a few and trying to 
   * set their sizes incorrectly.
   */
  public static void main(String[] args) {
    Frame window = new Frame();
    
    //Testing default constructor
    Rectangle red = new Square();  //using a variable of the superclass type
    //Testing other constructor.
    Square green = new Square(200, 200, 20, Color.green);
    
    Rectangle orange = new Square(200, 250, 20, Color.orange); 
    //testing setSize(int, int): orange should be size 30, not 100
    orange.setSize(100, 30);
    
    Rectangle blue = new Square(200, 300, 20, Color.blue); 
    //testing setSize(Dimension): blue should be size 50, not 200
    blue.setSize(new Dimension(50, 200));
    
    Scalable pink = new Square(450, 200, 20, Color.pink);
    //testing scale: pink should be 10 x 10
    pink.scale(0.5);
    
  }

}
