1 / 7

Object Oriented Programming with JAVA Graphical User Interface GUI Lecture 8

Object Oriented Programming with JAVA Graphical User Interface GUI Lecture 8. Mouse Event Handling. • MouseListener and MouseMotionListener event-listener interfaces are for handling mouse events. • MouseListener and MouseMotionListener interface methods:

mio
Télécharger la présentation

Object Oriented Programming with JAVA Graphical User Interface GUI Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgrammingwithJAVA Graphical User Interface GUI Lecture 8

  2. Mouse Event Handling •MouseListener and MouseMotionListener event-listener interfaces are for handling mouse events. • MouseListener and MouseMotionListener interface methods: Methods of interface MouseListener •public void mousePressed( MouseEvent event ) – Called when a mouse button is pressed while the mouse cursor is on a component. •public void mouseClicked( MouseEvent event ) – Called when a mouse button is pressed and released while the mouse cursor remains stationary on a component. This event is always preceded by a call to mousePressed. •public void mouseReleased( MouseEvent event ) – Called when a mouse button is released after being pressed. This event is always preceded by a call to mousePressed and one or more calls to mouseDragged.

  3. Methods of interface MouseListener, cont.. public void mouseEntered( MouseEvent event ) – Called when the mouse cursor enters the bounds of a component. •public void mouseExited( MouseEvent event ) – Called when the mouse cursor leaves the bounds of a component.

  4. Methods of interface MouseMotionListener •public void mouseDragged( MouseEvent event ) – Called when the mouse button is pressed while the mouse cursor is on a component and the mouse is moved while the mouse button remains pressed. This event is always preceded by a call to mousePressed. •public void mouseMoved( MouseEvent event ) – Called when the mouse is moved when the mouse cursor is on a component. All move events are sent to the component over which the mouse is currently positioned.

  5. Example import java.awt.*; import java.awt.event.*; import javax.swing.*; class myPane extends JPanel{ Point pt1,pt2; public myPane(){ pt1=null; pt2=null; myListener m=new myListener(); addMouseListener(m); addMouseMotionListener(m); } public void paintComponent(Graphics g){ super.paintComponent(g); if(pt1!=null && pt2!=null){ g.drawLine((int)pt1.getX(),(int)pt1.getY(),(int)pt2.getX(),(int)pt2.getY()); } }

  6. Example, cont.. class myListener implements MouseListener, MouseMotionListener{ public void mousePressed(MouseEvent event){ pt1=event.getPoint(); repaint(); } public void mouseClicked( MouseEvent event ){} public void mouseReleased(MouseEvent event){ pt1=null; pt2=null; } public void mouseEntered(MouseEvent event){} public void mouseExited(MouseEvent event){} public void mouseDragged( MouseEvent event){ if(pt1!=null){ pt2=event.getPoint(); repaint(); } } public void mouseMoved( MouseEvent event ){} } }

  7. Example, cont.. class MyFrame extends JFrame{ myPane ppn; public MyFrame(){ super ("Test Controls"); InitializeComponents(); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } void InitializeComponents(){ ppn=new myPane(); add(ppn); } } public class MouseEventTest{ public static void main(String[] args){ MyFrame mf=new MyFrame(); } }

More Related