1 / 19

Mouse Events

Mouse Events. Handling Mouse Events. Java provides two listener interfaces to handle mouse events: MouseListener; MouseMotionListener . The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked .

Download Presentation

Mouse Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mouse Events

  2. Handling Mouse Events Java provides two listener interfaces to handle mouse events: • MouseListener; • MouseMotionListener. The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseMotionListener listens for actions such as dragging or moving the mouse.

  3. MouseListener Methods • mouseEntered(MouseEvent e) • mouseExited(MouseEvent e) • mousePressed(MouseEvent e) • mouseReleased(MouseEvent e) • mouseClicked(MouseEvent e)

  4. MouseMotionListener Methods • mouseDragged(MouseEvent e) • mouseMoved(MouseEvent e)

  5. MouseEvent Class Since the MouseEvent class inherits InputEvent, you can use the methods defined in the InputEvent class: • public boolean ifAltDown() • public boolean ifControlDown() • public boolean ifShiftDown() • public boolean ifMetaDown() (the right mouse button is pressed)

  6. Methods of MouseEvent Class • public int getClickCount() • public int getX() • public int getY() • public Point getPoint()

  7. MouseMotionListener Methods • mouseDragged(MouseEvent e) • mouseMoved(MouseEvent e) Methods • public int getX() • public int getY() • public Point getPoint()

  8. Example 1: mouse is clicked import javax.*; import java.awt.*; public class TestMouse { public TestMouse() { setTitle(“Test Mouse”); getContentPane().add(new MousePanel()); } public static void main(Strings[] args) { TestMouse frame = new TestMouse(); frame.setSize(300,300); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }

  9. Example 1: mouse is clicked class MousePanel extends JPanel implements MouseListener { int mx, my; public MousePanel() { addMouseListener(this) } // Implement all methods declared in MouseListener // interface! public void mousePressed(MouseEvent e) { } public void mouseReleassed(MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mouseFocus(MouseEvent e) { } public void mouseClicked(MouseEvent e) { mx = getX(); my = getY(); // System.out.println(“Clicked at (“+mx +“,”+my + ”)”); repaint(); } ...

  10. Example 1: Mouse is clicked … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.drawRect(mx-20, my-20, 40, 40); } } // Inner Class MousePanel }// Outer Class TestMouse

  11. Example 2: Mouse is dragged class MousePanel2 extends JPanel implements MouseMotionListener { int mx, my; // Register the Listener public MousePanel2() { addMouseMotionListener(this) } // Implement all methods declared in MouseMotionListener // interface public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { mx = getX(); my = getY(); //System.out.println(“Mouse at (“ +mx + “,”+my+ ”)”); repaint(); } ...

  12. Example 2: Mouse is dragged … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.fillRect(mx-2, my-2, 4, 4); } } // Inner Class MousePanel2 }// Outer Class TestMouse

  13. Adapter Class vs. Listener Interface • Java provides a special feature, called an adapterclass, that can simplify the creation of event handlers in certain situations. • An adapter class provides an empty implementation of all methods in an event listener interface. • Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. • You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested.

  14. Example 3 with Adapter class MousePanel3 extends JPanel { // Register the event handler int mx, my; public MousePanel3() { addMouseListener(new MyMouseAdapter()); } // Inner class extends Adapter class class MyMouseAdapterextendsMouseAdapter { // Implement methods you reallyneed public void mouseClicked(MouseEvent e) { mx = getX(); my = getX(); //System.out.println(“Clicked at (“+mx+“,”+my+”)”); repaint(); } ...

  15. Example 3: Mouse is clicked … public void paintComponent(Graphics g) super.paintComponent(g); g.setColor(Color.red); g.fillRect(mx-20, my-20, 40, 40); } } // Inner Class MousePanel3 }// Outer Class TestMouse

  16. Example 4: Draw shapes • Suppose, the method draw(Graphics g,...) is implemented forthe classes Circle and Rectangle (sub-classes of the abstract class Shape). We can draw array of Shapes. … public void paintComponent(Graphics g) super.paintComponent(g); for(int k = 0; k < shapes.length; k++) { shapes[k].draw(g, mx, my); } repaint(); } } // Inner Class MousePanel4 }// Outer Class TestMouse

  17. Handling Complex Mouse Events • Objective: Create a program for drawing using a mouse. Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. • See code and applet on web: http://www.cs.armstrong.edu/liang/introjb4/example/chapter12/ScribbleDemo.java http://www.cs.armstrong.edu/liang/introjb4/example/ScribbleDemo.html • See more examples of applets on web: http://www.dgp.toronto.edu/~mjmcguff/learn/java/

  18. Handling Keyboard Events To process a keyboard event, use the following handlers in theKeyListener interface: • void keyPressed(KeyEvent e) Called when a key is pressed. • void keyReleased(KeyEvent e) Called when a key is released. • void keyTyped(KeyEvent e) Called when a key is pressed and then released.

  19. Handling Keyboard Events char keyChar; void keyPressed (KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_DOWN: y+= 10; break; case KeyEvent.VK_UP: y-= 10; break; case KeyEvent.VK_LEFT: x-= 10; break; case KeyEvent.VK_RIGHT: x+= 10; break; default: keyChar = e.getChar(); } repaint(); } VK_HOME, VK_PGUP, VK_PGDN, VK_ESCAPE, VKF1, …, VK12; VK_0, …, VK_9; VK_A, … VK_Z; ...

More Related