

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


/**
 * A "MouseEventsDemo" object is a JPanel that responds to mouse events.  The class
 * is defined to "implement" MouseListener and MouseMotionListener -- these so-called
 * "interfaces" are part of the set-up for handling mouse events.
 */
public class MouseEventsDemo extends JPanel 
                  implements MouseListener, MouseMotionListener {
	
	/**
	 * Constructor initializes panel by arranging for it to receive mouse-related events
	 * and setting its preferred size and background color.  The perferred size will 
	 * determine how large the panel is when it is displayed in a MouseEventsFrame, but 
	 * has no effect on a MouseEventsApplet.
	 */
	public MouseEventsDemo() {
		addMouseListener(this);       // Arranges for basic mouse events to be received.  NO NOT CHANGE.
		addMouseMotionListener(this); // Arranges for mouse motion/drag events to be recieved.  NO NOT CHANGE.
		Dimension d;
		d = new Dimension(500,400);   // This will be the preferred size -- you can change this if you want.
		setPreferredSize(d);          // This panel wants to be 500-by-400 pixels.
		setBackground(Color.LIGHT_GRAY);  // Background color of panel is blue -- you can changet this if you want.
	}

	/**
	 * This method is called when the user presses a button on the mouse. It is 
	 * part of the MouseListener interface, and it will be called by the system
	 * because of the "addMouseListener" command in the constructor.  Information
	 * about the event can be found in the parameter e.  For example, e.getX() is
	 * the x-coordinate of the pixel where the mouse was pressed, and e.get() is
	 * the y-coordinate.  There are other methods in the event object that can
	 * tell you which button was pressed and whether the SHIFT key was being 
	 * held down when the button was pressed.
	 */
	public void mousePressed(MouseEvent e) {
		Graphics g = getGraphics();   // Get a Graphics object that can be used for drawing on the panel.
		g.setColor( Color.RED );
		if (e.isShiftDown()) {
			   // If the user is holding the shift key down,  draw a circle centered at (e.getX(),e.getY())
			g.fillOval(e.getX() - 20, e.getY() - 20, 40, 40);
		}
		else {
			   // If the user is NOT holding shift down,  draw a rectangle centered at (e.getX(),e.getY())
			g.fillRect(e.getX() - 20, e.getY() - 20, 40, 40);
		}
		g.dispose();  // Returns resources used by the Graphics object to the system.
	}

	/**
	 * This method is called when the user presses a button on the mouse. It is 
	 * part of the MouseListener interface. It is called by the system when the
	 * user releases the mouse button.  The position of the mouse when the
	 * user releases the button is given by e.getX() and e.getY().
	 * This method is part of the MouseListener interface.
	 */
	public void mouseReleased(MouseEvent e) {
	}

	/**
	 * This method is called when the user "clicks" a button on the mouse, which means
	 * that the mouse button is pressed and then quickly released at the same point.
	 * Note that this method is called in addition to calling mousePressed and
	 * mouseReleased.  It is more common to use mousePressed and mouseReleased
	 * than it is to use mouseClicked.
	 * This method is part of the MouseListener interface.
	 */
	public void mouseClicked(MouseEvent e) {
	}

	/**
	 * This method is called when the mouse moves from outside the panel into
	 * the panel. This method is part of the MouseListener interface.
	 */
	public void mouseEntered(MouseEvent e) {
	}

	/**
	 * This method is called when the mouse moves from inside the panel the
	 * the outside.  This method is part of the MouseListener interface.
	 */
	public void mouseExited(MouseEvent e) {
	}

	/**
	 * This method is called when the user drags the mouse while holding one
	 * of its buttons down.  The position of the mouse is given by e.getX()
	 * and e.getY().  This method is part of the MouseMotionListener interface.
	 */
	public void mouseDragged(MouseEvent e) {
	}

	/**
	 * This method is called when the user move the mouse over the panel, without
	 * holding any of its buttons down.  The position of the mouse is given by e.getX()
	 * and e.getY().  This method is part of the MouseMotionListener interface.
	 */
	public void mouseMoved(MouseEvent e) {
	}
	
}
