/**
 * TestMouse.java -- this is an application/main class that illustrates how to
 * use the MouseListener interface. 
 * It has been created to show how one must implement the abstract methods of
 * an interface in order to use the interface.
 *
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 */

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

public class TestMouse extends JFrame implements MouseListener {

   private Container contentPane;       // to hold a button and a textarea      
   private JButton specialButton;       // the event source 
   private JTextArea output;            // where we print our event handling
   private JScrollPane scroll;          // to make the textarea scrollable

   /**
    * Constructor of the TestMouse class.  It sets up a contentPane for the
    * frame, and then calls setupTextArea() and setupButtons() to get the
    * components on the contentPane.
    */

   public TestMouse()  {

      contentPane = getContentPane();
      contentPane.setLayout(null);
      setSize(new Dimension(600,700));

      setupTextArea();
     
      setupButtons();

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      
   }  // end Constructor

   /**
    * This method sets up the JTextArea object (i.e., output) and attach a 
    * JScrollPane object (i.e., scroll) to the textarea.  It puts "I am ready"
    * in the textarea and also sets it to be non-editable.
    * It adds the JScrollPane object to contentPane.
    *
    * @returns void
    */

   public void setupTextArea()  {

      output = new JTextArea();
      output.setText("I am ready");
      output.setEditable(false);
      scroll = new JScrollPane(output);
      scroll.setBounds(0,0,600,250);
      contentPane.add(scroll);          // add it to the contentPane

   }  // end setupTextArea


   /**
    * This method sets up the buttons.  In this case, there is only one button
    * (i.e., specialButton).  It adds a MouseListener to the button.
    */

   public void setupButtons()  {

      specialButton = new JButton("SpecialButton");
      specialButton.setBounds(123,270,200,200);
      contentPane.add(specialButton);
      specialButton.addMouseListener(this);

   }  // end setupButtons

   /**
    * This method implements MouseListener's mouseClicked prototype.
    * It gets the location of the mouse (<x,y>) and prints it out to the
    * JTextArea object (i.e., output).
    */

   public void mouseClicked(MouseEvent e)  {

      Point location = e.getPoint();
      String act = "\n    The button has been clicked at <" 
                   + location.getX() + "," + location.getY() + ">";
      output.append(act);

   }

   /**
    * This method implements MouseListener's mouseEntered prototype.
    * It gets the location of the mouse (<x,y>) and prints it out to the
    * JTextArea object (i.e., output).
    */
   public void mouseEntered(MouseEvent e)  { 

      Point location = e.getPoint();
      String act = "\n    The button has been entered at <" 
                   + location.getX() + "," + location.getY() + ">";
      output.append(act);

   }  // mouseEntered

   /**
    * This method implements MouseListener's mouseExited prototype.
    * It gets the location of the mouse (<x,y>) and prints it out to the
    * JTextArea object (i.e., output).
    */

   public void mouseExited(MouseEvent e)  { 

      Point location = e.getPoint();
      String act = "\n    The button has been exited at <" 
                   + location.getX() + "," + location.getY() + ">";
      output.append(act);

   }  // end mouseExited

   /**
    * This method implements MouseListener's mousePressed prototype.
    * It gets the location of the mouse (<x,y>) and prints it out to the
    * JTextArea object (i.e., output).
    */
   public void mousePressed(MouseEvent e)  { 
 
      Point location = e.getPoint();
      String act = "\n    The button has been pressed at <" 
                   + location.getX() + "," + location.getY() + ">";
      output.append(act);

   }  // end mousePressed

   /**
    * This method implements MouseListener's mouseReleased prototype.
    * It gets the location of the mouse (<x,y>) and prints it out to the
    * JTextArea object (i.e., output).
    */
   public void mouseReleased(MouseEvent e)  { 
 
      Point location = e.getPoint();
      String act = "\n    The button has been released at <" 
                   + location.getX() + "," + location.getY() + ">";
      output.append(act);

   }  // end mouseReleased

   /**
    *  This method simply instantiates the TestMouse class and creates an
    *  object "frame".
    *
    *  @params String[] args
    */

   public static void main(String[] args)  {

      TestMouse frame = new TestMouse();
      frame.setVisible(true);

   }  // end Main

}  // end TestMouse class

