© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application 21.2 Constructing the Painter Application 21.3 Interfaces 21.4 The mousePressed Event Handler 21.5 The mouseReleased Event Handler 21.6The mouseDragged Event Handler 21.7 Wrap-Up Tutorial 21 – “Cat and Mouse” Painter Application Introducing Interfaces, Mouse Input; the Event- Handling Mechanism
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objective In this tutorial, you will learn to: –Use mouse events to allow user interaction with an application. –Use the mousePressed, mouseReleased and mouseDragged event handlers. –Use the Graphics object to draw circles on a JPanel. –Determine which mouse button was pressed.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.1 Painter application before drawing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.2 Drawing on the Painter application’s DrawJPanel. Drawing lines composed of small, colored circles To draw on the JDrawPanel, click and hold the left mouse button and drag the mouse
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.3 Drawing a cat and a computer mouse on the DrawJPanel. Be creative – draw a cat and a computer mouse on the JDrawPanel
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Painter Application (Cont.) Figure 21.4 Erasing part of the drawing. Erasing by drawing circles that are the same color as the DrawJPanel ’s background To erase, click and hold the right mouse button and drag it over part of your drawing
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Painter Application When the mouse is pressed Store the mouse’s location If the left mouse button is pressed Set the color to blue Set the diameter for drawing Else Set the color to the DrawJPanel’s background color Set the diameter for erasing Repaint the DrawJPanel When the mouse is dragged Store the mouse’s location Repaint the DrawJPanel When the paintComponent method is called Set the drawing color Draw a circle with the appropriate diameter at the mouse’s location
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Painter Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces Event source –The GUI component with which the user interacts Event listener –The object that is notified by the event source when an event occurs Interface –Describes what a class does –But not how it is done Implementing the interface –Declare all the methods in the interface
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Figure 21.6 Calling the addMouseListener method. Adding a MouseListener object The MouseListener interface declares event handler headers for mouse events.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Creating an anonymous inner class Figure 21.7 Creating an instance of the MouseListener interface.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Inner class –Declared inside another class –Anonymous inner class Inner class with no name MouseEvent –Generated when the mouse is used to interact with an application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Interfaces (Cont.) Figure 21.8 Declaring event handlers for the MouseListener interface. Empty event handlers in the anonymous inner class
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler Figure 21.9 Coding the mousePressed event handler. Adding code to the mousePressed event handler mousePressed event handler is called when the mouse button is pressed on a component
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Storing the position of the mouse cursor. Calling method getPoint to store the mouse’s position The getPoint method returns a Point object
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Setting the color and diameter of the circle.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Setting the color using method setColor.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Drawing the circle using method fillOval. Calling method fillOval The fillOval method of class Graphics draws a filled oval.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure General oval. Bounding box A bounding box specifies an oval’s upper-left x - and y - coordinates, width and height
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mousePressed Event Handler (Cont.) Figure Running the application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler Figure Declaring constants for the released circle’s color and size.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Coding the mouseReleased event handler. Adding code to the mouseReleased event handler
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Storing the location of the mouse’s cursor. Coding the drawJPanelMouseReleased method
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Setting the color and size of the circle to be drawn.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseReleased Event Handler (Cont.) Figure Running the application. Drawing a flower using only mouseReleased and mousePressed event handlers
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler Figure Adding constants for the erasing circle. The getBackground method returns a Color object representing the color of the DrawJPanel ’s background
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Removing the method call to drawJPanelMouseReleased.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Using the isMetaDown method to determine which mouse button is pressed. Determining which mouse button is pressed The isMetaDown method returns true when the user presses the right mouse button on a mouse with two or three buttons
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) MouseMotionListener interface –Declares event handlers for mouse events –mouseDragged event handler Called when a mouse button is pressed and the mouse is dragged. –mouseMoved event handler Called when the mouse is moved without any buttons pressed.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Calling method addMouseMotionListener. Adding a MouseMotionListener to the DrawJPanel The addMouseMotionListener method of the DrawJPanel class registers an event listener with an event source
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Creating an anonymous inner class. Creating an anonymous inner class that implements the MouseMotionListener interface
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Coding the mouseDragged event handler.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Storing the location of the mouse’s cursor.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The mouseDragged Event Handler (Cont.) Figure Running the completed Painter application.
2004 Prentice Hall, Inc. All rights reserved. Outline 36 DrawJPanel.java (1 of 6) 1 // Tutorial 21: DrawJPanel.java 2 // This class allows the user to draw and erase on the application. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class DrawJPanel extends JPanel 8 { 9 // Point to hold the mouse cursor's location 10 private Point currentPoint; // constants for the drawn circle 13 private final Color DRAW_COLOR = Color.BLUE; 14 private final int DRAW_DIAMETER = 8; // constants for the erase circle 17 private final Color ERASE_COLOR = this.getBackground(); 18 private final int ERASE_DIAMETER = 8; // instance variables for the circle 21 private Color drawColor; 22 private int drawDiameter; 23
2004 Prentice Hall, Inc. All rights reserved. Outline 37 DrawJPanel.java (2 of 6) 24 // constructor 25 public DrawJPanel() 26 { 27 addMouseListener( new MouseListener() // anonymous inner class 30 { 31 // event handler called when a mouse button is clicked 32 public void mouseClicked( MouseEvent event ) 33 { 34 } // event handler called when mouse enters this DrawJPanel 37 public void mouseEntered( MouseEvent event ) 38 { 39 } // event handler called when mouse exits this DrawJPanel 42 public void mouseExited( MouseEvent event ) 43 { 44 } 45 Adding a MouseListener Creating an anonymous inner class Empty event handlers in the anonymous inner class. To implement the MouseListener, these methods must be implemented
2004 Prentice Hall, Inc. All rights reserved. Outline // event handler called when a mouse button is pressed 47 public void mousePressed( MouseEvent event ) 48 { 49 drawJPanelMousePressed( event ); 50 } // event handler called when a mouse button is released 53 public void mouseReleased( MouseEvent event ) 54 { 55 } } // end anonymous inner class ); // end call to addMouseListener addMouseMotionListener( new MouseMotionListener() // anonymous inner class 64 { 65 // event handler called when the mouse is dragged 66 public void mouseDragged( MouseEvent event ) 67 { 68 drawJPanelMouseDragged( event ); 69 } 70 DrawJPanel.java (3 of 6) Calling the drawJPanelMousePressed method when the mouse is pressed Calling the drawJPanelMouseDragged method when the mouse is dragged Empty event handlers in the anonymous inner class Adding a MouseMotionListener to the DrawJPanel Creating an anonymous inner class that implements the MouseMotionListener interface
2004 Prentice Hall, Inc. All rights reserved. Outline 39 DrawJPanel.java (4 of 6) 71 // event handler called when the mouse is moved 72 public void mouseMoved( MouseEvent event ) 73 { 74 } } // end anonymous inner class ); // end call to addMouseMotionListener } // end constructor // draw a circle on this DrawJPanel 83 private void drawJPanelMousePressed( MouseEvent event ) 84 { 85 // store the location of the mouse 86 currentPoint = event.getPoint(); if ( event.isMetaDown() ) // right mouse button is pressed 89 { 90 drawColor = ERASE_COLOR; 91 drawDiameter = ERASE_DIAMETER; 92 } Determining which mouse button was pressed Calling method getPoint to store the mouse’s position
2004 Prentice Hall, Inc. All rights reserved. Outline 40 DrawJPanel.java (5 of 6) 93 else // left mouse button is pressed 94 { 95 drawColor = DRAW_COLOR; 96 drawDiameter = DRAW_DIAMETER; 97 } repaint(); // repaint this DrawJPanel } // end method drawJPanelMousePressed // draw a small circle at the mouse's location 104 public void paintComponent( Graphics g ) 105 { 106 g.setColor( drawColor ); // set the color if ( currentPoint != null ) 109 { 110 // draw a filled circle at the mouse's location 111 g.fillOval( currentPoint.x, currentPoint.y, 112 drawDiameter, drawDiameter ); 113 } } // end method paintComponent 116 Using the Graphics object to draw a circle
2004 Prentice Hall, Inc. All rights reserved. Outline 41 DrawJPanel.java (6 of 6) 117 // draw a circle on this DrawJPanel 118 private void drawJPanelMouseDragged( MouseEvent event ) 119 { 120 // store the location of the mouse in currentPoint 121 currentPoint = event.getPoint(); repaint(); // repaint this DrawJPanel } // end method drawJPanelMouseDragged } // end class DrawJPanel
2004 Prentice Hall, Inc. All rights reserved. Outline 42 Painter.java (1 of 2) 1 // Tutorial 21: Painter.java 2 // Application enables user to draw on a subclass of JPanel. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.util.*; 7 8 public class Painter extends JFrame 9 { 10 // DrawJPanel for circles drawn by user 11 private DrawJPanel myDrawJPanel; // no-argument constructor 14 public Painter() 15 { 16 createUserInterface(); 17 } // set up the GUI components 20 public void createUserInterface() 21 { 22 // get content pane for attaching GUI components 23 Container contentPane = getContentPane(); 24
2004 Prentice Hall, Inc. All rights reserved. Outline 43 Painter.java (2 of 2) 25 // enable explicit positioning of GUI components 26 contentPane.setLayout( null ); // set up myDrawJPanel 29 myDrawJPanel = new DrawJPanel(); 30 myDrawJPanel.setBounds( 0, 0, 300, 300 ); 31 contentPane.add( myDrawJPanel ); // set properties of application's window 34 setTitle( "Painter" ); // set title bar text 35 setSize( 300, 300 ); // set window size 36 setVisible( true ); // display window } // end method createUserInterface // main method 41 public static void main( String[] args ) 42 { 43 Painter application = new Painter(); 44 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class Painter