import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** Demonstrate mouse events -- bare bones.<p>
*
* JFC/Swing includes two mouse listeners: <code>MouseMotionListener</code>
* with two methods (<code>mouseMoved</code> and <code>mouseDragged</code>)
* and <code>MouseListener</code> with four methods
* (<code>mouseClicked</code>, <code>mouseEntered</code>,
* <code>mousePressed</code>, and <code>mouseReleased</code>).  Both
* listeners are implemented in this demo.
* <p>
*
* An empty <code>JPanel</code> is instantiated and configured to
* listen for mouse events.  As the mouse is manipulated over the
* <code>JPanel</code>, numerous mouse events occur.  The listener
* methods simply output the return
* string from the <code>paramString</code> method to the console.
* <p>
*
* Screen snap:<p>
* <center><img src="DemoMouseEvents-1.gif"></center><p>
*
* @see <a href="DemoMouseEvents.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoMouseEvents
{
   public static void main(String[] args)
   {
      MouseEventsDemoFrame frame = new MouseEventsDemoFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoMouseEvents");
      frame.pack();
      frame.show();
   }
}

class MouseEventsDemoFrame extends JFrame
implements MouseMotionListener, MouseListener
{
   private JPanel mousePanel;

   // constructor

   public MouseEventsDemoFrame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      mousePanel = new JPanel();
      mousePanel.setBackground(Color.pink);
      mousePanel.setPreferredSize(new Dimension(200, 200));

      // -------------
      // add listeners
      // -------------

      mousePanel.addMouseMotionListener(this);
      mousePanel.addMouseListener(this);

      // ------------------
      // arrange components
      // ------------------

      // nothing to arrange for this demo!

      // make panel this JFrame's content pane

      this.setContentPane(mousePanel);
   }

   // -----------------------------------------
   // implement MouseMotionListener methods (2)
   // -----------------------------------------

   public void mouseDragged(MouseEvent me) { outputMouseStatus(me); }
   public void mouseMoved(MouseEvent me) { outputMouseStatus(me); }

   // -----------------------------------
   // implement MouseListener methods (5)
   // -----------------------------------

   public void mouseClicked(MouseEvent me) { outputMouseStatus(me); }
   public void mouseEntered(MouseEvent me) { outputMouseStatus(me); }
   public void mouseExited(MouseEvent me) { outputMouseStatus(me); }
   public void mousePressed(MouseEvent me) { outputMouseStatus(me); }
   public void mouseReleased(MouseEvent me) { outputMouseStatus(me); }

   /** Output the mouse status.
   *
   * The paramString method in the MouseEvent class is a convenient
   * way to get the mouse status.  In practice, the following
   * methods are typically used:
   *   getX - get the x position of the pointer
   *   getY - get the y position of the pointer
   *   getModifiers - get the modifier byte for this InputEvent (used
   *     to determine, for example, which, if any, mouse button is pressed)
   *   getSource - get the source of this EventObject
   */
   private void outputMouseStatus(MouseEvent me)
   {
      System.out.println(me.paramString());
   }
}


