// MouseDemoPanelApplet.java (c) 2007 Kari Laitinen // http://www.naturalprogramming.com/ // 2007-02-25 File created. // 2007-02-26 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 = 265 ; int current_mouse_position_y = 100 ; 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() { 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 preesed // 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 statement draws a body for the mouse. // The mouse body is a large rounded rectangle. graphics.fillRoundRect( current_mouse_position_x - 70, current_mouse_position_y - 30, 140, 200, 60, 60 ) ; // The mouse buttons will be drawn with red color if either // the Ctrl or the Shift key is pressed down. if ( control_or_shift_key_is_down == true ) { graphics.setColor( Color.red ) ; } else { graphics.setColor( Color.yellow ) ; } // We'll draw only those mouse buttons that are being pressed. // Small rounded rectangles represent mouse buttons. if ( left_mouse_button_is_down == true ) { graphics.fillRoundRect( current_mouse_position_x - 50, current_mouse_position_y, 30, 60, 10, 10 ) ; } if ( middle_mouse_button_is_down == true ) { graphics.fillRoundRect( current_mouse_position_x - 15, current_mouse_position_y, 30, 60, 10, 10 ) ; } if ( right_mouse_button_is_down == true ) { graphics.fillRoundRect( current_mouse_position_x + 20, current_mouse_position_y, 30, 60, 10, 10 ) ; } } } public class MouseDemoPanelApplet extends JApplet { public void init() { getContentPane().add( new MouseDemoPanel() ) ; } }