80 likes | 270 Views
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:
E N D
Object Oriented ProgrammingwithJAVA 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: 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.
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.
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.
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()); } }
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 ){} } }
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(); } }