// MouseDemoApplet.java (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2007-02-25 File created. // 2009-11-05 Last modification // This program demonstrates how a Java program can react to // mouse events. The following is the mechanism to react to // mouse events: // -- this program implements the MouseListener and MouseMotionListener // interfaces which specify a set of methods that are called // automatically by the program execution system // -- in the init() method the MouseDemoPanelApplet object informs the // program execution system that it will listen to mouse events // -- this program provides either dummy or non-dummy implementations // for all the methods specified in the interfaces // This program also shows how to find out which mouse button was used // and whether the Ctrl or Shift key was pressed simulaneously with // a mouse button. // This is a JPanel-based applet which means that all functionality is // built into a class that is derived from class JPanel. In the applet // class we simply attach a JPanel-based object to the applet. // JPanel-based applets are equipped with automatic double buffering // which means that the flickering of the screen is minimized. import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; class MouseDemoPanel extends JPanel implements MouseListener, MouseMotionListener { int current_mouse_position_x = 340 ; int current_mouse_position_y = 120 ; boolean left_mouse_button_is_down = false ; boolean middle_mouse_button_is_down = false ; boolean right_mouse_button_is_down = false ; boolean control_or_shift_key_is_down = false ; public MouseDemoPanel() { setBackground( new Color( 0xC8, 0xDC, 0xFF ) ) ; // Light blueish addMouseListener( this ) ; addMouseMotionListener( this ) ; } // The following dummy methods are needed // to fully implement the mouse listener interfaces. public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} // The following method will be called automatically by the // program execution system when a mouse button is pressed // down. The MouseEvent object that is received as a parameter // contains information related to the mouse event. public void mousePressed( MouseEvent event ) { current_mouse_position_x = event.getX() ; current_mouse_position_y = event.getY() ; if ( event.getButton() == MouseEvent.BUTTON1 ) { left_mouse_button_is_down = true ; } else if ( event.getButton() == MouseEvent.BUTTON2 ) { middle_mouse_button_is_down = true ; } else if ( event.getButton() == MouseEvent.BUTTON3 ) { right_mouse_button_is_down = true ; } if ( event.isControlDown() == true || event.isShiftDown() == true ) { control_or_shift_key_is_down = true ; } else { control_or_shift_key_is_down = false ; } repaint() ; } // The mouseMoved() method is called when the mouse is moved without // pressing its buttons. public void mouseMoved( MouseEvent event ) { current_mouse_position_x = event.getX() ; current_mouse_position_y = event.getY() ; repaint(); } // The mouseDragged() method is called when the mouse is moved while // one of its buttons is pressed down. public void mouseDragged( MouseEvent event ) { current_mouse_position_x = event.getX() ; current_mouse_position_y = event.getY() ; repaint(); } public void mouseReleased( MouseEvent event ) { if ( event.getButton() == MouseEvent.BUTTON1 ) { left_mouse_button_is_down = false ; } else if ( event.getButton() == MouseEvent.BUTTON2 ) { middle_mouse_button_is_down = false ; } else if ( event.getButton() == MouseEvent.BUTTON3 ) { right_mouse_button_is_down = false ; } repaint(); } public void paintComponent( Graphics graphics ) { super.paintComponent( graphics ) ; // The following statements draw a body for the mouse. // The mouse body is a large oval (ellipse). graphics.setColor( Color.DARK_GRAY ) ; graphics.fillOval( current_mouse_position_x - 80, current_mouse_position_y - 30, 160, 210 ) ; // The mouse buttons will be drawn with a different color // if either the Ctrl or the Shift key is pressed down. if ( control_or_shift_key_is_down == true ) { graphics.setColor( Color.MAGENTA ) ; } else { graphics.setColor( Color.YELLOW ) ; } // We'll draw only those mouse buttons that are being pressed. // Small ellipses (ovals) represent mouse buttons. if ( left_mouse_button_is_down == true ) { graphics.fillOval( current_mouse_position_x - 55, current_mouse_position_y + 5, 30, 60 ) ; } if ( middle_mouse_button_is_down == true ) { graphics.fillOval( current_mouse_position_x - 15, current_mouse_position_y - 5, 30, 60 ) ; } if ( right_mouse_button_is_down == true ) { graphics.fillOval( current_mouse_position_x + 25, current_mouse_position_y + 5, 30, 60 ) ; } } } public class MouseDemoApplet extends JApplet { public void init() { getContentPane().add( new MouseDemoPanel() ) ; } }