15.9. Lists

 
[Page 477]

14.4. Mouse Events

A mouse event is fired whenever a mouse is pressed, released, clicked, moved, or dragged on a component. The mouse event object captures the event, such as the number of clicks associated with it or the location ( x and y coordinates) of the mouse, as shown in Figure 14.10.

Figure 14.10. The MouseEvent class encapsulates information for mouse events.

Since the MouseEvent class inherits InputEvent , you can use the methods defined in the InputEvent class on a MouseEvent object.

The java.awt.Point class represents a point on a component. The class contains two public variables , x and y , for coordinates. To create a Point , use the following constructor:

 Point(   int   x,   int   y) 

This constructs a Point object with the specified x and y coordinates. Normally, the data fields in a class should be private. This class has two public data fields, which is not a good practice.

Java provides two listener interfaces, MouseListener and MouseMotionListener , to handle mouse events, as shown in Figure 14.11. Implement the MouseListener interface to listen for such actions as pressing, releasing, entering, exiting, or clicking the mouse, and implement the MouseMotionListener interface to listen for such actions as dragging or moving the mouse.

Figure 14.11. The MouseListener interface handles mouse pressed, released, clicked, entered, and exited events. The MouseMotionListener interface handles mouse dragged and moved events.
(This item is displayed on page 478 in the print version)

14.4.1. Example: Moving a Message on a Panel Using a Mouse

This example writes a program that displays a message in a panel, as shown in Listing 14.7. You can use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point. A sample run of the program is shown in Figure 14.12.


[Page 478]
Figure 14.12. You can move the message by dragging the mouse.


Listing 14.7. MoveMessageDemo.java
(This item is displayed on pages 478 - 479 in the print version)
 1   import   java.awt.*; 2   import   java.awt.event.*; 3   import   javax.swing.*; 4 5   public class   MoveMessageDemo   extends   JFrame { 6   public   MoveMessageDemo() { 7  // Create a MovableMessagePanel instance for moving a message  8  MovableMessagePanel p =   new   MovableMessagePanel(   "Welcome to Java"  );   9 10  // Place the message panel in the frame  11 getContentPane().setLayout(   new   BorderLayout()); 12 getContentPane().add(p); 13 } 14 15  /** Main method */  16   public static void   main(String[] args) { 17 MoveMessageDemo frame =   new   MoveMessageDemo(); 18 frame.setTitle(   "MoveMessageDemo"   ); 19 frame.setLocationRelativeTo(   null   );  // Center the frame  20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.setSize(   200   ,   100   ); 22 frame.setVisible(   true   ); 23 } 24 

[Page 479]
 25  // Inner class: MovableMessagePanel draws a message  26    static class   MovableMessagePanel   extends   Jpanel {  27   private   String message =   "Welcome to Java"   ; 28   private int   x =   20   ; 29   private int   y =   20   ; 30 31  /** Construct a panel to draw string s */  32   public   MovableMessagePanel(String s) { 33 message = s; 34  addMouseMotionListener(   new   MouseMotionAdapter() {  35  /** Handle mouse dragged event */  36   public void  mouseDragged(MouseEvent e) {  37  // Get the new location and repaint the screen  38 x = e.getX(); 39 y = e.getY(); 40 repaint(); 41 } 42  }  ); 43 } 44 45  /** Paint the component */  46   protected void   paintComponent(Graphics g) { 47   super   .paintComponent(g); 48 g.drawString(message, x, y); 49 } 50 } 51 } 

The MovableMessagePanel class extends JPanel to draw a message (line 26). Additionally, it handles redisplaying the message when the mouse is dragged. This class is declared as an inner class inside the main class because it is only used in this class. Furthermore, the inner class is declared static because it does not reference any instance members of the main class.

The MouseMotionListener interface contains two handlers, mouseMoved and mouseDragged , for handling mouse-motion events. When you move the mouse with the button pressed, the mouseDragged method is invoked to repaint the viewing area and display the message at the mouse point. When you move the mouse without pressing the button, the mouseMoved method is invoked.

Because the listener is only interested in the mouse dragged event, the anonymous inner class listener extends MouseMotionAdapter to override the mouseDragged method. If the inner class implements the MouseMotionListener interface, you would have to implement all of the handlers even if your listener does not care about some of the events.

The mouseDragged method is invoked when you move the mouse with a button pressed. This method obtains the mouse location using getX and getY methods (lines 38 “39) in the MouseEvent class. This becomes the new location for the message. Invoking the repaint() method (line 40) causes paintComponent to be invoked (line 46), which displays the message in a new location.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net