150 likes | 271 Views
Handling Mouse Events. Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: Maggiore flessibilità Minor spreco di risorse In Java . Handling Mouse Events.
E N D
Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: • Maggiore flessibilità • Minor spreco di risorse In Java
Handling Mouse Events Un oggetto può essere notificato dell’occorrenza di un evento registrandosi come event listener implementando l’apposita interfaccia. Ogni Swing Component è un event source e può avere più EventListener in ascolto L’occorrenza di un evento chiama la rispettivo metodo dell’EventListener. (es. mouse clicked MouseListener.mouseClicked() )
Handling Mouse Events MouseEvents (il puntatore entra o esce da un componente, l’utente preme un bottone) MouseListener (Eventi che richiedono il tracciamento del movimento del cursore) MouseMotionListener (MouseWheelEvents MouseWheelListener)
Handling Mouse Events 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 Esempio: MouseXY
Handling Mouse Events Esempio: MouseXY class MouseXY extends Frame implements WindowListener, MouseListener { public MouseXY(String text) { super(text); addWindowListener(this); addMouseListener(this); } publicvoid mouseClicked(MouseEvent event) {} publicvoid mouseEntered(MouseEvent event) {} publicvoid mouseExited(MouseEvent event) {} publicvoid mouseReleased(MouseEvent event) {} …….
Handling Mouse Events Esempio: MouseXY publicvoid 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 publicvoid windowClosed(WindowEvent event) {} publicvoid windowDeiconified(WindowEvent event) {} publicvoid windowIconified(WindowEvent event) {} publicvoid windowActivated(WindowEvent event) {} publicvoid windowDeactivated(WindowEvent event) {} publicvoid windowOpened(WindowEvent event) {} publicvoid windowClosing(WindowEvent event) { System.exit(0); } publicstaticvoid main(String[] args) { MouseXY screen = new MouseXY("Mouse example"); screen.setSize(400,300); screen.setVisible(true); } }
Handling Mouse Events Esempio: MousePainter
Handling Mouse Events Esempio: MousePainter publicclass MousePainter extends JFrame{ privateint 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 () { publicvoid 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 getContentPane().add(toolButton,BorderLayout.NORTH); getContentPane().add(label,BorderLayout.SOUTH); setSize(350,350); addMouseMotionListener ( new MouseMotionAdapter () { publicvoid mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } } ); show(); };
Handling Mouse Events Esempio: MousePainter publicvoid paint(Graphics g) { if (rubber) { g.clearRect(xValue,yValue,15,15); } else { g.fillOval(xValue,yValue,3,3); }; }; publicstaticvoid main(String[] args){ MousePainter app = new MousePainter(); app.addWindowListener( new WindowAdapter() { publicvoid windowClosing(WindowEvent e){ System.exit(0); } } ); } }
Handling Mouse Events Esempio: MousePainter addMouseListener ( new MouseInputAdapter () { publicvoid mouseDragged(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); repaint(); } publicvoid mouseClicked(MouseEvent e){ if (e.getComponent().equals(toolButton)) { String newLabel = rubber ? "Use rubber" : "Use pen"; toolButton.setLabel(newLabel); rubber = rubber ? false : true; } } } );