Handling Mouse Events - PowerPoint PPT Presentation

Loading...

PPT – Handling Mouse Events PowerPoint presentation | free to download - id: 577f74-MTdlN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Handling Mouse Events

Description:

Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: Maggiore flessibilit – PowerPoint PPT presentation

Number of Views:160
Avg rating:3.0/5.0
Slides: 16
Provided by: Thri7
Learn more at: http://www.deit.univpm.it
Category:
Tags: events | handling | mouse

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Handling Mouse Events


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
Handling Mouse Events
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() )
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
3
Handling Mouse Events
  • 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)

4
Handling Mouse Events
MouseListener Interface 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.
MouseMotionListener Interface 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.
5
Handling Mouse Events
MouseEvent Class 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 "CtrlShift". These strings can be localized using the awt.properties file. Introduced in release 1.4.
6
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

7
Esempio MouseXY
Handling Mouse Events
8
Handling Mouse Events
Esempio MouseXY
  • 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)
  • .

9
Handling Mouse Events
Esempio MouseXY
  • 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)

10
Handling Mouse Events
Esempio MouseXY
  • 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)

11
Handling Mouse Events
Esempio MousePainter
12
Handling Mouse Events
Esempio MousePainter
  • 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

13
Handling Mouse Events
Esempio MousePainter
  • getContentPane().add(toolButton,BorderLayout.NOR
    TH)
  • getContentPane().add(label,BorderLayout.SOUTH)
  • setSize(350,350)
  • addMouseMotionListener (
  • new MouseMotionAdapter ()
  • public void mouseDragged(MouseEvent e)
  • xValue e.getX()
  • yValue e.getY()
  • repaint()
  • )
  • show()

14
Handling Mouse Events
Esempio MousePainter
  • 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)
  • )

15
Handling Mouse Events
Esempio MousePainter
  • 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
  • )
About PowerShow.com