Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.

Similar presentations


Presentation on theme: "Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor."— Presentation transcript:

1 Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor spreco di risorse In Java

2 User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window)WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visibleComponentListener Table or list selection changesListSelectionListener Un oggetto può essere notificato delloccorrenza di un evento registrandosi come event listener implementando lapposita interfaccia. Ogni Swing Component è un event source e può avere più EventListener in ascolto Loccorrenza di un evento chiama la rispettivo metodo dellEventListener. (es. mouse clicked MouseListener.mouseClicked() ) Handling Mouse Events

3 MouseEvents (il puntatore entra o esce da un componente, lutente preme un bottone) MouseListener (Eventi che richiedono il tracciamento del movimento del cursore) MouseMotionListener (MouseWheelEvents MouseWheelListener) Handling Mouse Events

4 MouseMotionListener Interface MethodPurpose mouseDragged(MouseEvent) Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component. mouseMoved(MouseEvent) Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor. Handling Mouse Events MouseListener Interface MethodPurpose mouseClicked(MouseEvent) Called just after the user clicks the listened-to component. mouseEntered(MouseEvent) Called just after the cursor enters the bounds of the listened-to component. mouseExited(MouseEvent) Called just after the cursor exits the bounds of the listened-to component. mousePressed(MouseEvent) Called just after the user presses a mouse button while the cursor is over the listened-to component. mouseReleased(MouseEvent) Called just after the user releases a mouse button after a mouse press over the listened-to component.

5 Handling Mouse Events MouseEvent Class MethodPurpose int getClickCount() Return the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. int getX() int getY() Point getPoint() Return the (x,y) position at which the event occurred, relative to the component that fired the event. int getButton() Return which mouse button, if any, has changed state. One of the following constants is returned: NOBUTTON, BUTTON1, BUTTON2, or BUTTON3. Introduced in release 1.4. boolean isPopupTrigger() Return true if the mouse event should cause a popup menu to appear. Because popup triggers are platform dependent, if your program uses popup menus, you should call isPopupTrigger for all mouse-pressed and mouse-released events fired by components over which the popup can appear. See Bringing Up a Popup Menu for more information about popup menus.Bringing Up a Popup Menu String getMouseModifiersText(int) Return a String describing the modifier keys and mouse buttons that were active during the event, such as "Shift", or "Ctrl+Shift". These strings can be localized using the awt.properties file. Introduced in release 1.4.

6 Implementando le Interfacce MouseListener e MouseMotionListener si è costretti ad implementare tutti i metodi Spesso ci interessano solo alcuni eventi (pochi metodi) Classi Adapter (estendono le interfacce e danno una imlementazione di default dei metodi) MouseAdapter mplements MouseListener MouseMotionAdapter implements MouseMotionListener MouseInputadapter implements MouseInputListener and MouseMotionListener Handling Mouse Events

7 Esempio: MouseXY Handling Mouse Events

8 class MouseXY extends Frame implements WindowListener, MouseListener { public MouseXY(String text) { super(text); addWindowListener(this); addMouseListener(this); } public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} ……. Handling Mouse Events Esempio: MouseXY

9 public void mousePressed(MouseEvent event) { int xCoord = event.getX(); int yCoord = event.getY(); Graphics g = getGraphics(); g.drawString("+ [" + String.valueOf(xCoord) + "," + String.valueOf(yCoord) + "]", xCoord,yCoord); } Handling Mouse Events Esempio: MouseXY

10 public void windowClosed(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} public void windowOpened(WindowEvent event) {} public void windowClosing(WindowEvent event) { System.exit(0); } public static void main(String[] args) { MouseXY screen = new MouseXY("Mouse example"); screen.setSize(400,300); screen.setVisible(true); } Handling Mouse Events Esempio: MouseXY

11 Handling Mouse Events Esempio: MousePainter

12 public class MousePainter extends JFrame{ private int xValue = -10, yValue = -10; boolean rubber = false; Label label; Button toolButton; public MousePainter () { super("Mouse Painter Example"); label = new Label("Drag mouse to paint"); toolButton = new Button("Use Rubber"); toolButton.addMouseListener( new MouseInputAdapter () { public void mouseClicked(MouseEvent e){ if (e.getButton() == 1) { String newLabel = rubber ? "Use rubber" : "Use pen"; toolButton.setLabel(newLabel); rubber = rubber ? false : true; } ); Handling Mouse Events Esempio: MousePainter

13 getContentPane().add(toolButton,BorderLayout.NORTH); getContentPane().add(label,BorderLayout.SOUTH); setSize(350,350); addMouseMotionListener ( new MouseMotionAdapter () { public void mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } ); show(); }; Handling Mouse Events Esempio: MousePainter

14 public void paint(Graphics g) { if (rubber) { g.clearRect(xValue,yValue,15,15); } else { g.fillOval(xValue,yValue,3,3); }; public static void main(String[] args){ MousePainter app = new MousePainter(); app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } ); } Handling Mouse Events Esempio: MousePainter

15 addMouseListener ( new MouseInputAdapter () { public void mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } public void mouseClicked(MouseEvent e){ if (e.getComponent().equals(toolButton)) { String newLabel = rubber ? "Use rubber" : "Use pen"; toolButton.setLabel(newLabel); rubber = rubber ? false : true; } ); Handling Mouse Events Esempio: MousePainter


Download ppt "Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor."

Similar presentations


Ads by Google