/*
 *  ==============================================================
 *  DemoMouse.java :  Demonstrate low-level mouse events.
 * 
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                          March, 2001
 *  ==============================================================
 */

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

// 1. Create a Swing applet that implements mouse listener.

public class DemoMouse extends JApplet implements MouseListener {
   Container container;
   int width, height;
   int x, y;
   int flag;
   String mouseStatus;
   
   public void init() {

      // 2. Get a reference to the applet's content pane.

      container = this.getContentPane();

      // 3. Initialize the data members.

      x     = 0;
      y     = 0;
      width = 2;
      height = 2; // For a small square to be drawn
      flag   = 0;

      // 4. Register the mouse listener with the applet.

      container.addMouseListener(this);   

   }

   /*
    *  --------------------------------------------------------
    *  Note: The class JApplet contains the update() method
    *  to override the same method from the class
    *  Component that repairs the container
    *  background with its background color and calls
    *  the paint() method. The update()in JApplet does not
    *  repair the background of the container. It calls the
    *  paint() method. You need to use an update() method that
    *  repairs the background.
    *  --------------------------------------------------------
    */

   // 5. The update() method to repair the applet's background.

   public void update(Graphics g) {
       g.setColor( this.getBackground() );
       g.fillRect(0, 0,         // x and y coordinates
                  getWidth(),   // Get the applet's width
                  getHeight()); // Get the applet's height
       paint(g);
   }

   // 6. The paint() method to paint the applet based on the flag.

   public void paint(Graphics g) {

      g.setColor( Color.blue );

      // Write instructions at pixel coordinates (x,y) = (5, 20) ...

      g.drawString("Click the Mouse Button...", 5, 20);

      // Print mouse event at pixel coordinate (x,y) = (5, 80)

      g.setColor(Color.red);
      if(flag == 1)
         g.drawString("Mouse Entered Applet!", 5, 80);

      else if(flag == 2)
         g.drawString("Mouse Exited Applet!", 5, 80);

      else if(flag == 3) {
         g.drawString("Mouse Entered Applet!", 5, 80);
         g.fillRect(x, y, width, height);
         g.drawString("Clicked Here!", x, y);
      }

      else if(flag == 4) {
         g.drawString("Mouse Entered Applet!", 5, 80);
         g.fillRect(x, y, width, height);
         g.drawString("Pressed Here!", x, y);
      }

      else if(flag == 5) {
         g.drawString("Mouse Entered Applet!", 5, 80);
         g.fillRect(x, y, width, height);
         g.drawString("Mouse Released!", x, y);
      }
   }

   // 7. Listener interface method, called when the mouse enters the applet.

   public void mouseEntered(MouseEvent me) {
      flag = 1;
      repaint();
   }

   // 8. Listener interface method, called when the mouse exits the applet.

   public void mouseExited(MouseEvent me) {
      flag = 2;
      repaint();
   }

   // 9. Listener interface method, called when the mouse button is clicked.

   public void mouseClicked(MouseEvent me) {
      flag = 3;
      x = me.getX();
      y = me.getY();
      repaint();
   }

   // 10. Listener interface method, called when the mouse button is pressed.

   public void mousePressed(MouseEvent me) {
      flag = 4;
      x = me.getX();
      y = me.getY();
      repaint();
   }

   // 11. Listener interface method, called when the mouse button is released.

   public void mouseReleased(MouseEvent me) {
      flag = 5;
      x = me.getX();
      y = me.getY();
      repaint();
   }
}