import objectdraw.*;
import java.awt.*;

/*
 * Better simulation of the sun setting in which it changes color from
 * yellow to red.  This program also illustrates what happens when you
 * attempt to create a color with values out of the 0..255 range.  The
 * error occurs once the sun has dropped slightly below the horizon.
 */
public class ColorfulSunset extends WindowController {

  private FilledRect sky;
  private FilledRect grass;
  private FilledOval sun;

  private static final int SCENE_WIDTH = 400;
  private static final int SKY_HEIGHT = 250;
  private static final int GROUND_HEIGHT = 150;
  private static final int SUN_X = 80;
  private static final int SUN_Y = 0;
  private static final int SUN_DIAMETER = 100;

  private static final int VELOCITY = 1;  // rate at which the sun moves.

  /* colors for the sun */
  private static final int YELLOW_AMOUNT = 255;
  private static final int BLUE_AMOUNT = 0;

  private int greenAmount = 255;  // not constant, so I can vary it

  /*
   * Set up initial scene.
   */
  public void begin() {
    sky = new FilledRect(0, 0, SCENE_WIDTH, SKY_HEIGHT, canvas);
    sun = new FilledOval(SUN_X, SUN_Y, SUN_DIAMETER, SUN_DIAMETER, canvas);
    grass = new FilledRect(0, SKY_HEIGHT, SCENE_WIDTH, GROUND_HEIGHT, canvas);

    sky.setColor(Color.blue);
    grass.setColor(Color.green);

    sun.setColor(new Color(YELLOW_AMOUNT, greenAmount, BLUE_AMOUNT));
  }

  /*
   * Move the sun down the screen,
   * changing its color gradually by removing a bit of green
   * on each step.
   */
  public void onMouseMove(Location point) {

    sun.move(0, VELOCITY);

    // remove a little green from the color we're about to create
    greenAmount = greenAmount - 1;  

    /* 
       The above may cause greenAmount to become < 0, which will cause
       the program to crash when we use it as a parameter to the 
       new Color construction.  To fix, we can replace the above line with
       an if statement that prevents it from becoming negative:
    
       if (greenAmount > 0) {
          greenAmount = greenAmount - 1;
       }
    */

    sun.setColor(new Color(YELLOW_AMOUNT, greenAmount, BLUE_AMOUNT));
  }

}
