
/* 
   A simple demonstration of MouseEvents.  Shapes are drawn
   on a black background when the user clicks the applet.  If
   the user Shift-clicks, the applet is cleared.  If the user
   right-clicks the applet, a red rectangle is drawn.  Otherwise,
   when the user clicks, a blue oval is drawn.  The contents of
   the applet are not persistent.  They will disappear if the
   applet is covered and uncovered.
*/


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

public class SimpleStamper extends JApplet {


   public void init() {
          // This method is called by the system to initialize
          // the applet.  An object belonging to the nested class
          // Display is created and installed as the content
          // pane of the applet.  This Display object does
          // all the real work.
      Display display = new Display();
      setContentPane(display);
   }
   

   class Display extends JPanel implements MouseListener {
         // A nested class to represent the drawing surface that
         // fills the applet.
   
      Display() {
             // This constructor simply sets the background color
             // of the panel to be black and sets the panel to
             // listen for mouse events on itself.
          setBackground(Color.black);
          addMouseListener(this);
      }

      public void mousePressed(MouseEvent evt) {
             // Since this panel has been set to listen for mouse
             // events on itself, this method will be called when the
             // user clicks the mouse on the panel.  (Since the panel
             // fills the whole applet, that means clicking anywhere
             // on the applet.)

          if ( evt.isShiftDown() ) {
                // The user was holding down the Shift key.  Just
                // repaint the panel.  Since this class does not
                // define a paintComponent() method, the method
                // from the superclass, JPanel, is called.  That 
                // method simply fills the panel with its background 
                // color, which is black
             repaint();
             return;
          }

          int x = evt.getX();  // x-coordinate where user clicked.
          int y = evt.getY();  // y-coordinate where user clicked.

          Graphics g = getGraphics();  // Graphics context for drawing
                                       // directly on this JPanel.

          if ( evt.isMetaDown() ) {
                 // User right-clicked at the point (x,y).
                 // Draw a blue oval centered at the point (x,y).
                 // (A black outline around the oval will make it 
                 // more distinct when ovals and rects overlap.)
              g.setColor(Color.blue);
              g.fillOval( x - 30, y - 15, 60, 30 );
              g.setColor(Color.black);
              g.drawOval( x - 30, y - 15, 60, 30 );
          }
          else {
                 // User left-clicked (or middle-clicked) at (x,y).
                 // Draw a red rectangle centered at (x,y).
              g.setColor(Color.red);
              g.fillRect( x - 30, y - 15, 60, 30 );
              g.setColor(Color.black);
              g.drawRect( x - 30, y - 15, 60, 30 );
          }

          g.dispose();  // We are finished with the graphics context,
                        //   so dispose of it.
                        
       } // end mousePressed();
       
      // The next four empty routines are required by the 
      // MouseListener interface.
      
      public void mouseEntered(MouseEvent evt) { }
      public void mouseExited(MouseEvent evt) { }
      public void mouseClicked(MouseEvent evt) { }
      public void mouseReleased(MouseEvent evt) { }

   }  // end nested class Display   


} // end class SimpleStamper
