Download presentation
Presentation is loading. Please wait.
Published byKody Saylor Modified over 9 years ago
1
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with JButtons and other GUI components, mouse movements generate Events –To have your program respond, you must implement either or both of MouseListener and MouseMotionListener this is a bit more involved than the action listener because there are 7 methods that must be implemented between the two Listeners –Fortunately, we can implement most as empty methods public void mouseMoved(MouseEvent e) { }// does nothing –We will often keep track of where the mouse is or where it was and now where it has been moved to example: when the mouse button is pressed, remember it’s x and y coordinates, when the mouse button is released, remember the new x and y coordinates, now draw a line from (old x, old y) to (new x, new y)
2
Mouse Listeners and Methods Two types of listeners for the mouse: –MouseListener – deals with mouse button operations and must implement the following methods: mousePressed mouseReleased mouseClicked mouseEntered mouseExited –when you hold down the mouse button or click the mouse button, both mousePressed and mouseClicked are called, when you release the mouse button or click the mouse button, both mouseReleased and mouseClicked are called –we won’t be using mouseEntered or mouseExited here –MouseMotionListener – deals with moving the mouse mouseMoved mouseDragged To specify that your program should listen for mouse events, you will add a mouse listener or motion listener as in –addMouseListener(this); –addMouseMotionListener(this);
3
Example: Drawing a line Here, we see how to use the MouseListener to draw a line on a JPanel when the mouse is dragged –As usual, we have created a JFrame which itself adds the JPanel below (MouseExample) to its contents public class MouseExample extends JPanel implements MouseListener { private int x1, x2, y1, y2; public MouseExample( ) { x1 = x2 = y1 = y2 = 0; addMouseListener(this); } public void mousePressed(MouseEvent e) { x1 = e.getX( ); y1 = e.getY( ); } public void mouseReleased(MouseEvent e) { x2 = e.getX( ); y2 = e.getY( ); repaint( ); } public void paintComponent(Graphics g) { g.setColor(Color.black); g.drawLine(x1, y1, x2, y2); } } // end class We would also implement mouseEntered, mouseExited and mouseClicked as { }
4
Example 2: Drawing dots Here, we place a dot every time you drag the mouse public class DotDrawer extends JPanel implements MouseMotionListener { private static int x, y; public DotDrawer( ) { x = y = 0; addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { x = e.getX( ); y = e.getY( ); repaint( ); } public void mouseMoved(MouseEvent e) { } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.fillOval(x-1, y-1, 2, 2); } } // end class
5
Storing Graphics Items If you notice, the previous program performs super.paintComponent(g); while the program before did not –this instruction cases the JPanel’s Graphics object to clear itself –if we do this, then we lose what we had already drawn –but if we don’t do this, the image can become cluttered What we want to do is perform this operation every time we do repaint( ) but we want to also draw each previous Graphics item on the screen –so we have to remember everything we had already done –we will store each item by using arrays –for our dots program, we can store each dot’s x and y coordinate an two arrays, x[i] and y[i] (for dot i) This is the first step toward building a more interesting Graphics program –Paint/drawing program –Computer game that has multiple Graphics items on the screen
6
Using Graphics and GUI Together Imaging that we want to create a Paint program –The Paint program will have a drawing area like we just saw –It also needs JButtons to control such things as changing shapes and colors Can we just add JButtons to our DotDrawer? –Unfortunately no, if we did that, the GUI interferes with the Graphics object causing some bizarre things to happen –So instead, we will create 2 JPanels in our main and add them both to the JFrame –The 2 JPanels will be of different classes One class will implement MouseListener/MouseMotionListener One class will implement ActionListener –See the Paint program for details
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.