
/* 
   This applet lets the user sketch in different colors
   on a black or on a white background.  The user can
   turn on "kaleidascopic" symmetry.  In this case,
   an eight-way symmetric picture is drawn as the
   user drags the mouse.
*/

import java.awt.*;
import java.applet.Applet;

public class KaleidaSketch extends Applet {

   KSCanvas canvas;  // Rectangular area where the actual drawins is done.

   Button whiteButton, blackButton;  // Buttons for filling the canvas with
                                     //     black or white.

   Checkbox kaleidascopicCheckbox;   // When this checkbox is checked,
                                     //     symmetric pictures are drawn.

   Choice colorChoice;               // A pop-up menu containing the list
                                     //   of available drawing colors.

   final static Color[] colorList = {  // Available drawing colors.
          Color.black,                 //   (Note that these must correspond
          Color.darkGray,              //    to items in colorChoice.)
          Color.gray, 
          Color.lightGray, 
          Color.white,
          Color.red, 
          Color.blue, 
          new Color(0,200,0),  // A darker green than the usual one
          Color.cyan, 
          Color.magenta, 
          Color.yellow,
          Color.pink, 
          Color.orange, 
          new Color(150,120,40)  // brown
       };


   public void init() {
         // init() method lays out the applet, with the controls lined up
         // below a large drawing area.

      setBackground(Color.gray);

      // Create the objects that will appear in the applet.

      canvas = new KSCanvas();
      whiteButton = new Button("All White");
      blackButton = new Button("All Black");
      kaleidascopicCheckbox = new Checkbox("Kaleidascopic");
      colorChoice = new Choice();

      colorChoice.addItem("Black");       // Add available colors to pop-up menu.
      colorChoice.addItem("Dark Gray");
      colorChoice.addItem("Gray");
      colorChoice.addItem("Light Gray");
      colorChoice.addItem("White");
      colorChoice.addItem("Red");
      colorChoice.addItem("Blue");
      colorChoice.addItem("Green");
      colorChoice.addItem("Cyan");
      colorChoice.addItem("Magenta");
      colorChoice.addItem("Yellow");
      colorChoice.addItem("Pink");
      colorChoice.addItem("Orange");
      colorChoice.addItem("Brown");

      // Create a bottom Panel to hold the controls.

      Panel bottom = new Panel();
      bottom.setBackground(Color.lightGray);
      bottom.add(kaleidascopicCheckbox);
      bottom.add(colorChoice);
      bottom.add(blackButton);
      bottom.add(whiteButton);

      // Set the applet layout to BorderLayout, and add the componets.

      setLayout( new BorderLayout(3,3) );
      add("Center",canvas);
      add("South",bottom);

   } // end init()


   public Insets insets() { 
         // Leave a 3-pixel border around the edge of the applet.
      return new Insets(3,3,3,3);
   }


   public boolean action(Event evt, Object arg) {
         // Respond to user action by calling a method in the KSCanvas class.

      if (evt.target == whiteButton) {  // "All White" button was clicked.
         canvas.clear(Color.white); 
         return true;
      }

      else if (evt.target == blackButton) { // "All Black" button was clicked.
         canvas.clear(Color.black);
         return true;
      }
      else if (evt.target == colorChoice) {  // Item was chosen from color pop-up menu.
         int itemNum = colorChoice.getSelectedIndex();
         canvas.setDrawingColor(colorList[itemNum]);
         return true;
      }
      else if (evt.target == kaleidascopicCheckbox) {  // Checkbox has changed state.
         boolean checked = kaleidascopicCheckbox.getState();
         canvas.setKaleidascopic(checked);
         return true;
      }
      else {
         return super.action(evt,arg);
      }
   } // end action()


}  // end class KaleidaSketch
