import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *  This class defines a panel that holds a "mosaic" of colored squares
 *  and makes it possible for the user to use the mouse to set the colors
 *  of the squares.  A row of buttons along the bottom of the panel let
 *  the user perform various actions.  (Note that this class "implements
 *  MouseListener, MouseMotionListener, ActionListener" so that it can
 *  handle events generated by the mouse and the buttons.)
 */
public class FirstEvents extends JPanel 
        implements MouseListener, MouseMotionListener, ActionListener {

   /**
    *  This main() routine makes it possible to run this class as an
    *  application.  The main() routine simply opens a window that
    *  contains a panel of type FirstEvents.  (Note that the main
    *  routine is completely separate from the use of this class
    *  to define objects!)
    */
   public static void main(String[] args) {
      JFrame window = new JFrame("Simple Mosaic Creator");
      window.setContentPane(new FirstEvents());
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLocation(100,100);
      window.pack();
      window.setResizable(false);
      window.setVisible(true);
   }
   
   
   /**
    *  An instance variable of type MosaicPanel represents the mosaic
    *  that is displayed in the panel.  This object provides various
    *  methods for working with the mosaic.
    */
   private MosaicPanel mosaic;

   /**
    *  An instance variable of type Color represents the color that
    *  the user will use for drawing with the mouse on the mosaic.  When
    *  the user clicks and drags the mouse on the mosaic, the square
    *  under the mouse is changed to this color.
    */
   private Color drawingColor = Color.RED;


   /**
    *  The constructor for the panel.  This creates the MosaicPanel and
    *  buttons that the panel will contain and sets up the user interface.
    */
   public FirstEvents() {

      /* Set some properties of the panel. */
      
      setBackground(Color.DARK_GRAY);
      setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
      setLayout(new BorderLayout(3,3));
      
      /* Create four buttons that will appear at the bottom of the panel. */

      JButton selectColorButton, fillButton, randomButton, clearButton;
      selectColorButton = new JButton("Pick Draw Color");
      fillButton = new JButton("Color Fill");
      randomButton = new JButton("Random Fill");
      clearButton = new JButton("Clear");
      
      /* Arrange for events from the button to be sent to this panel (by calling
         the actionPerformed() method that is defined below). */
         
      selectColorButton.addActionListener(this);
      fillButton.addActionListener(this);
      randomButton.addActionListener(this);
      clearButton.addActionListener(this);
      
      /* Create the MosaicPanel, and arrange for mouse events from the mosaic to
         be sent to this panel (by calling the mouse-handling methods defined below. */
      
      mosaic = new MosaicPanel(25,25,20,20);
      mosaic.addMouseListener(this);
      mosaic.addMouseMotionListener(this);
      
      /* Set up the user interface by adding buttons and mosaic to this panel. */
      
      JPanel buttonBar;
      buttonBar = new JPanel();
      buttonBar.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
      buttonBar.add(selectColorButton);
      buttonBar.add(fillButton);      
      buttonBar.add(randomButton);
      buttonBar.add(clearButton);
      add(buttonBar, BorderLayout.SOUTH);
      add(mosaic, BorderLayout.CENTER);
   }
   
   
   /**
    *  This method will be called when the user clicks one of the four buttons
    *  at the bottom of the panel.  Its purpose is to carry out the action
    *  that is represented by the button that was clicked.
    */
   public void actionPerformed(ActionEvent evt) {
      String command;  // The "action command" -- the text from the button that was clicked.
      command = evt.getActionCommand();
      if (command.equals("Random Fill")) {
         mosaic.fillRandomly(); // Respond to "Random Fill" command by filling the mosaic with random colors.
      }
   }
   
   
   /**
    *  This method will be called when the user presses a mouse key while
    *  the mouse cursor is in the mosaic.  The parameter, evt, has information
    *  about the event.  In particular, evt.getX() and evt.getY() give the
    *  x- and y-coordinates of the mouse in the mosaic's coordinate system.
    */
   public void mousePressed(MouseEvent evt) {
   }
   
   
   /**
    *  This method will be called when the user drags the mouse while holding
    *  a mouse button down, after pressing the mouse button on the mosaic.
    */
   public void mouseDragged(MouseEvent evt) {
   }
   

   /* The remaining 5 methods have no purpose in this panel, but they are 
      required because the class "implements MouseListener, MouseMotionListener" */
      
   public void mouseReleased(MouseEvent evt) {
   }
   public void mouseClicked(MouseEvent evt) {
   }
   public void mouseEntered(MouseEvent evt) {
   }
   public void mouseExited(MouseEvent evt) {
   }
   public void mouseMoved(MouseEvent evt) {
   }

} // end class FirstEvents


