Download presentation
Presentation is loading. Please wait.
Published byBriana Bond Modified over 9 years ago
1
Mouse Events
2
Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener; MouseListener; MouseMotionListener 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 F mouseEntered(MouseEvent e) F mouseExited(MouseEvent e) F mousePressed(MouseEvent e) F mouseReleased(MouseEvent e) F mouseClicked(MouseEvent e)
4
MouseMotionListener Methods F mouseDragged(MouseEvent e) F mouseMoved(MouseEvent e)
5
MouseEvent Class Since the MouseEvent class inherits InputEvent, you can use the methods defined in the InputEvent class: F public boolean ifAltDown() F public boolean ifControlDown() F public boolean ifShiftDown() F public boolean ifMetaDown() (the right mouse button is pressed)
6
Methods of MouseEvent Class F public int getClickCount() F public int getX() F public int getY() F public Point getPoint()
7
MouseMotionListener Methods F mouseDragged(MouseEvent e) F mouseMoved(MouseEvent e) Methods F public int getX() F public int getY() F 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) } all // 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) } all // 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 F Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations. F An adapter class provides an empty implementation of all methods in an event listener interface. F 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. F 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 MyMouseAdapter extends MouseAdapter { // Implement methods you really need 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 for the 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 F 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. F 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 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. To process a keyboard event, use the following handlers in the KeyListener interface:
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;...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.