// Program: Ray Charles' Etch-A-Sketch drawing of Brian
// Author:  Brian Kell
// Date: September 2001

import aLibrary.*;
import java.awt.*;

public class Director {
  final int up = 0, right = 1, down = 2, left = 3; // constants for
                                                   // direction of stylus
  // The objects we need to draw with:
  private AWindow universe;
  private ALabel portraitCaption;
  private ARectangle tSide, bSide, lSide, rSide, screen;
  private AOval ne, se, nw, sw, leftKnob, rightKnob; // ne, se, nw, sw: corners
  private DrawingTool stylus;

  // Other variables
  private int stylusX, stylusY, direction, pixForward;

  public Director() {

    // set up window
    universe = new AWindow(100, 100, 400, 350); // x, y, width, height

    // draw Etch-A-Sketch outside red frame
    lSide = new ARectangle(10, 20, 20, 270); // x, y, width, height
    lSide.place(universe);
    lSide.setColor(Color.red);
    lSide.setToFill();
    rSide = new ARectangle(370, 20, 20, 270);
    rSide.place(universe);
    rSide.setColor(Color.red);
    rSide.setToFill();
    tSide = new ARectangle(20, 10, 360, 20);
    tSide.place(universe);
    tSide.setColor(Color.red);
    tSide.setToFill();
    bSide = new ARectangle(20, 250, 360, 50);
    bSide.place(universe);
    bSide.setColor(Color.red);
    bSide.setToFill();
    nw = new AOval(10, 10, 20, 20); // x, y, width, height
    nw.place(universe);
    nw.setColor(Color.red);
    nw.setToFill();
    ne = new AOval(370, 10, 20, 20);
    ne.place(universe);
    ne.setColor(Color.red);
    ne.setToFill();
    sw = new AOval(10, 280, 20, 20);
    sw.place(universe);
    sw.setColor(Color.red);
    sw.setToFill();
    se = new AOval(370, 280, 20, 20);
    se.place(universe);
    se.setColor(Color.red);
    se.setToFill();

    // draw Etch-A-Sketch screen
    screen = new ARectangle(30, 30, 340, 220); // x, y, width, height
    screen.place(universe);
    screen.setColor(new Color(.9f, .9f, .9f));
    screen.setToFill();

    // draw Etch-A-Sketch knobs
    leftKnob = new AOval(50, 260, 30, 30); // x, y, width, height
    leftKnob.place(universe);
    leftKnob.setColor(Color.white);
    leftKnob.setToFill();
    rightKnob = new AOval(320, 260, 30, 30);
    rightKnob.place(universe);
    rightKnob.setColor(Color.white);
    rightKnob.setToFill();

    // place caption
    portraitCaption = new ALabel(20, 300, 350, 50); // x, y, width, height
    portraitCaption.place(universe);
    portraitCaption.setFontSize(12);
    portraitCaption.setColor(Color.black);
    portraitCaption.setText("Ray Charles draws Brian with an Etch-A-Sketch.");

    // now show everything we've drawn
    universe.repaint();

    // set up DrawingTool
    stylus = new DrawingTool();
    stylus.place(universe);
    stylus.draw();
    direction = up;

    while (true) { // infinite loop - keep drawing until user closes window

      // Figure out where we're at
      stylusX = stylus.getX();
      stylusY = stylus.getY();

      if (Math.random() < .3) { // 30% chance stylus will change direction
          WatchOutForThatTree(); // change direction (George didn't)
      }

      // make sure we don't draw outside of window
      while (((direction == up) && (stylusY < 50))
        || ((direction == down) && (stylusY > 230))
        || ((direction == left) && (stylusX < 50))
        || ((direction == right) && (stylusX > 350))) {
          WatchOutForThatTree();
      }

      // move forward a random amount (use a skewed normal distribution)
      pixForward = 0;
      for (int k = 0; k < 20; k++)
        if (Math.random() < .4)
          pixForward++;
      stylus.moveForwardBy(pixForward);

    }

  }

  private void WatchOutForThatTree() {

    // Change direction of stylus (rotate clockwise and set new direction)
    for (int j = 0; j < Math.floor(Math.random() * 3 + 1); j++) {
      stylus.turnClockwise();
      direction++;
      direction = direction % 4; // so that 3++ becomes 0, not 4
    }
  }
}
