Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In...

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

Transcript of Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In...

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

Handling Mouse Events

Event-Driven ProgrammingApproccio standard per programmare GUI

In contrapposizione al polling offre: - Maggiore flessibilità- Minor spreco di risorse

In Java

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

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 visible ComponentListener

Table or list selection changes ListSelectionListener

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

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

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

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

MouseMotionListener Interface

Method Purpose

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

Method Purpose

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.

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

Handling Mouse Events

MouseEvent Class Method Purpose

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.

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.

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

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

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

Esempio: MouseXYHandling Mouse Events

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

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 EventsEsempio: MouseXY

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

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 EventsEsempio: MouseXY

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

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 EventsEsempio: MouseXY

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

Handling Mouse EventsEsempio: MousePainter

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

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 EventsEsempio: MousePainter

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

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 EventsEsempio: MousePainter

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

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 EventsEsempio: MousePainter

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

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 EventsEsempio: MousePainter