
/*
   A simple program where the user can sketch curves in a variety of
   colors on a variety of background colors.  The user selects
   a drawing color form a pop-up menu at the bottom of the
   applet.  If the user clicks "Set Background", the background
   color is set to the current drawing color and the drawing
   area is filled with that color.  If the user clicks "Clear",
   the drawing area is just filled with the current background color.

   The user's drawing is not persistant.  It is cleared if the
   applet is resized.  If it is covered, in whole or part, and
   then uncovered, the part was covered is gone.
*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SimplePaint2 extends Applet {

   public void init() {
   
      setBackground(Color.gray);         // Background color of applet.
      setLayout(new BorderLayout(3,3));  // Layout manager for applet.
      
      SimplePaintCanvas canvas = new SimplePaintCanvas();  // The drawing area.
      add(canvas,BorderLayout.CENTER);
      
      Panel buttonBar = new Panel();       // A panel to hold the controls.
      add(buttonBar, BorderLayout.SOUTH);
      
      Choice choice = new Choice();  // The pop-up ment of colors.
      choice.addItem("Black");
      choice.addItem("Red");
      choice.addItem("Green");
      choice.addItem("Blue");
      choice.addItem("Cyan");
      choice.addItem("Magenta");
      choice.addItem("Yellow");
      choice.addItem("White");
      choice.setBackground(Color.white);
      buttonBar.add(choice);
      
      Button fill = new Button("Set Background");  // The first button.
      fill.addActionListener(canvas);
      fill.setBackground(Color.lightGray);
      buttonBar.add(fill);
      
      Button clear = new Button("Clear");   // The second button.
      clear.addActionListener(canvas);
      clear.setBackground(Color.lightGray);
      buttonBar.add(clear);
      
      canvas.colorChoice = choice;  // Canvas needs access to the pop-up menu,
                                    //   so it can check it to find out what
                                    //   color to use.
      
   }  // end init()

   public Insets getInsets() {
          // Specify how wide a border to leave around the edges of the applet.
      return new Insets(3,3,3,3);
   }

} // end class SimplePaint2



class SimplePaintCanvas extends Canvas 
                 implements MouseListener, MouseMotionListener, ActionListener {
                 
        // A SimplePaintCanvas lets the user use the mouse to draw colored
        // curves.  The current color is specified by a pop-up menu which
        // must be created by the applet and assigned to the instance variable
        // colorChoice.  The applet listens for action events from buttons
        // named "Clear" and "Set Background".  The "Clear" button fills
        // the canvas with the current background color.  The "Set Background"
        // sets the background color to the current drawing color and
        // then clears.
               

   private final static int 
               BLACK = 0,
               RED = 1,            // Some constants to make
               GREEN = 2,          // the code more readable.
               BLUE = 3,           // These numbers code for
               CYAN = 4,           // the differnet drawing colors.
               MAGENTA = 5,
               YELLOW = 6;

   Choice colorChoice;  // A Choice object, containing the possible drawing
                        // colors, which must be created by the applet.


   /* The following variables are used when the user is sketching a
      curve while dragging a mouse. */

   private int prevX, prevY;     // The previous location of the mouse.
   
   private boolean dragging;      // This is set to true while the user is drawing.
   
   private Graphics graphicsForDrawing;  // A graphics context for the applet
                                         // that is used to draw the user's curve.
                                         
   SimplePaintCanvas() {
          // Constructor.  When the canvas is first created, it is set to
          // listen for mouse events and mouse motion events from
          // itself.  The initial background color is white.
      addMouseListener(this);
      addMouseMotionListener(this);
      setBackground(Color.white);
   }
   
   
   public void actionPerformed(ActionEvent evt) {
           // Respond when the user clicks on a button.
      String command = evt.getActionCommand();
      if (command.equals("Clear")) {
             // Clear to current background color.
         repaint();
      }
      else if (command.equals("Set Background")) {
             // Set background color, then clear.
         setBackground(getCurrentColor());
         repaint();
      }
   }


   private Color getCurrentColor() {
            // Check the colorChoice menu to find the currently
            // selected color, and return the appropriate color
            // object.
      int currentColor = colorChoice.getSelectedIndex();
      switch (currentColor) {
         case BLACK:
            return Color.black;
         case RED:
            return Color.red;
         case GREEN:
            return Color.green;
         case BLUE:
            return Color.blue;
         case CYAN:
            return Color.cyan;
         case MAGENTA:
            return Color.magenta;
         case YELLOW:
            return Color.yellow;
         default:
            return Color.white;
      }
   }


   public void mousePressed(MouseEvent evt) {
           // This is called when the user presses the mouse on the
           // canvas.  This begins a draw operation in which the user
           // sketches a curve.
           
      if (dragging == true)  // Ignore mouse presses that occur
          return;            //    when user is already drawing a curve.
                             //    (This can happen if the user presses
                             //    two mouse buttons at the same time.)

      prevX = evt.getX();    // Start drawing.
      prevY = evt.getY();
      dragging = true;
      graphicsForDrawing = getGraphics();
      graphicsForDrawing.setColor(getCurrentColor());
      
   } // end mousePressed()
   

   public void mouseReleased(MouseEvent evt) {
           // Called whenever the user releases the mouse button.
           // If the user was drawing a curve, the curve is done,
           // so we should set drawing to false and get rid of
           // the graphics context that we created to use during
           // the drawing.
       if (dragging == false)
          return;  // Nothing to do because the user isn't drawing.
       dragging = false;
       graphicsForDrawing.dispose();
       graphicsForDrawing = null;
   }
   

   public void mouseDragged(MouseEvent evt) {
            // Called whenever the user moves the mouse
            // while a mouse button is held down.  If the
            // user is drawing, draw a line segment from the
            // previous mouse location to the current mouse
            // location, and set up prevX and prevY for the
            // next call. 

       if (dragging == false)
          return;  // Nothing to do because the user isn't drawing.
          
       int x = evt.getX();   // x-coordinate of mouse.
       int y = evt.getY();   // y=coordinate of mouse.
       
       graphicsForDrawing.drawLine(prevX, prevY, x, y);  // Draw the line.
       
       prevX = x;  // Get ready for the next line segment in the curve.
       prevY = y;
       
   } // end mouseDragged.
   

   public void mouseEntered(MouseEvent evt) { }   // Some empty routines.
   public void mouseExited(MouseEvent evt) { }    //    (Required by the MouseListener
   public void mouseClicked(MouseEvent evt) { }   //    and MouseMotionListener
   public void mouseMoved(MouseEvent evt) { }     //    interfaces).
  
                
} // end class SimplePaintCanvas
