1 / 43

Objective

Tutorial 21 – “Cat and Mouse” Painter Application Introducing Interfaces, Mouse Input; the Event-Handling Mechanism.

millerkeith
Download Presentation

Objective

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. Tutorial 21 – “Cat and Mouse” Painter ApplicationIntroducing Interfaces, Mouse Input; the Event-Handling Mechanism Outline21.1 Test-Driving the Painter Application21.2 Constructing the Painter Application21.3 Interfaces21.4 The mousePressed Event Handler21.5 The mouseReleased Event Handler21.6 The mouseDragged Event Handler21.7 Wrap-Up

  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.

  3. 21.1 Test-Driving the Painter Application

  4. 21.1 Test-Driving the Painter Application (Cont.) Figure 21.1 Painter application before drawing.

  5. Drawing lines composed of small, colored circles 21.1 Test-Driving the Painter Application (Cont.) Figure 21.2 Drawing on the Painter application’s DrawJPanel. • To draw on the JDrawPanel, click and hold the left mouse button and drag the mouse

  6. 21.1 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

  7. Erasing by drawing circles that are the same color as the DrawJPanel’s background 21.1 Test-Driving the Painter Application (Cont.) Figure 21.4 Erasing part of the drawing. • To erase, click and hold the right mouse button and drag it over part of your drawing

  8. 21.2 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

  9. 21.2 Constructing the Painter Application (Cont.)

  10. 21.3 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

  11. Adding a MouseListener object 21.3 Interfaces (Cont.) Figure 21.6 Calling the addMouseListener method. • The MouseListener interface declares event handler headers for mouse events.

  12. Creating an anonymous inner class 21.3 Interfaces (Cont.) Figure 21.7 Creating an instance of the MouseListener interface.

  13. 21.3 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

  14. Empty event handlers in the anonymous inner class 21.3 Interfaces (Cont.) Figure 21.8 Declaring event handlers for the MouseListener interface.

  15. Adding code to the mousePressed event handler 21.4 The mousePressed Event Handler Figure 21.9 Coding the mousePressed event handler. • mousePressedevent handler is called when the mouse button is pressed on a component

  16. Calling method getPoint to store the mouse’s position 21.4 The mousePressed Event Handler (Cont.) Figure 21.10 Storing the position of the mouse cursor. • The getPoint method returns a Point object

  17. 21.4 The mousePressed Event Handler (Cont.) Figure 21.11 Setting the color and diameter of the circle.

  18. 21.4 The mousePressed Event Handler (Cont.) Figure 21.12 Setting the color using method setColor.

  19. Calling method fillOval 21.4 The mousePressed Event Handler (Cont.) Figure 21.13 Drawing the circle using method fillOval. • The fillOval method of class Graphics draws a filled oval.

  20. Bounding box 21.4 The mousePressed Event Handler (Cont.) Figure 21.14 General oval. • A bounding box specifies an oval’s upper-left x- and y-coordinates, width and height

  21. 21.4 The mousePressed Event Handler (Cont.) Figure 21.15 Running the application.

  22. 21.5 The mouseReleased Event Handler Figure 21.16 Declaring constants for the released circle’s color and size.

  23. Adding code to the mouseReleased event handler 21.5 The mouseReleased Event Handler (Cont.) Figure 21.17 Coding the mouseReleased event handler.

  24. Coding thedrawJPanelMouseReleasedmethod 21.5 The mouseReleased Event Handler (Cont.) Figure 21.18 Storing the location of the mouse’s cursor.

  25. 21.5 The mouseReleased Event Handler (Cont.) Figure 21.19 Setting the color and size of the circle to be drawn.

  26. Drawing a flower using only mouseReleased and mousePressed event handlers 21.5 The mouseReleased Event Handler (Cont.) Figure 21.20 Running the application.

  27. 21.6 The mouseDragged Event Handler Figure 21.21 Adding constants for the erasing circle. • The getBackground method returns a Color object representing the color of the DrawJPanel’s background

  28. 21.6 The mouseDragged Event Handler (Cont.) Figure 21.22 Removing the method call to drawJPanelMouseReleased.

  29. Determining which mouse button is pressed 21.6 The mouseDragged Event Handler (Cont.) Figure 21.23 Using the isMetaDown method to determine 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

  30. 21.6 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.

  31. Adding a MouseMotionListener to the DrawJPanel 21.6 The mouseDragged Event Handler (Cont.) Figure 21.24 Calling method addMouseMotionListener. • The addMouseMotionListener method of the DrawJPanel class registers an event listener with an event source

  32. Creating an anonymous inner class that implements the MouseMotionListener interface 21.6 The mouseDragged Event Handler (Cont.) Figure 21.25 Creating an anonymous inner class.

  33. 21.6 The mouseDragged Event Handler (Cont.) Figure 21.26 Coding the mouseDragged event handler.

  34. 21.6 The mouseDragged Event Handler (Cont.) Figure 21.27 Storing the location of the mouse’s cursor.

  35. 21.6 The mouseDragged Event Handler (Cont.) Figure 21.28 Running the completed Painter application.

  36. 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 publicclass DrawJPanel extends JPanel 8 { 9 // Point to hold the mouse cursor's location 10 private Point currentPoint; 11 12 // constants for the drawn circle 13 privatefinal Color DRAW_COLOR = Color.BLUE; 14 privatefinalintDRAW_DIAMETER = 8; 15 16 // constants for the erase circle 17 privatefinal Color ERASE_COLOR = this.getBackground(); 18 privatefinalintERASE_DIAMETER = 8; 19 20 // instance variables for the circle 21 private Color drawColor; 22 privateint drawDiameter; 23 DrawJPanel.java(1 of 6)

  37. 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 24 // constructor 25 public DrawJPanel() 26 { 27 addMouseListener( 28 29 new MouseListener() // anonymous inner class 30 { 31 // event handler called when a mouse button is clicked 32 publicvoid mouseClicked( MouseEvent event ) 33 { 34 } 35 36 // event handler called when mouse enters this DrawJPanel 37 publicvoid mouseEntered( MouseEvent event ) 38 { 39 } 40 41 // event handler called when mouse exits this DrawJPanel 42 publicvoid mouseExited( MouseEvent event ) 43 { 44 } 45 DrawJPanel.java(2 of 6)

  38. Calling the drawJPanelMousePressed method when the mouse is pressed Empty event handlers in the anonymous inner class Adding aMouseMotionListener tothe DrawJPanel Creating an anonymous inner class that implements the MouseMotionListener interface Calling the drawJPanelMouseDragged method when the mouse is dragged 46 // event handler called when a mouse button is pressed 47 publicvoid mousePressed( MouseEvent event ) 48 { 49 drawJPanelMousePressed( event ); 50 } 51 52 // event handler called when a mouse button is released 53 publicvoid mouseReleased( MouseEvent event ) 54 { 55 } 56 57 } // end anonymous inner class 58 59 ); // end call to addMouseListener 60 61 addMouseMotionListener( 62 63 new MouseMotionListener() // anonymous inner class 64 { 65 // event handler called when the mouse is dragged 66 publicvoid mouseDragged( MouseEvent event ) 67 { 68 drawJPanelMouseDragged( event ); 69 } 70 DrawJPanel.java(3 of 6)

  39. Calling methodgetPoint tostore the mouse’s position Determining which mouse button was pressed 71 // event handler called when the mouse is moved 72 publicvoid mouseMoved( MouseEvent event ) 73 { 74 } 75 76 } // end anonymous inner class 77 78 ); // end call to addMouseMotionListener 79 80 } // end constructor 81 82 // draw a circle on this DrawJPanel 83 private void drawJPanelMousePressed( MouseEvent event ) 84 { 85 // store the location of the mouse 86 currentPoint = event.getPoint(); 87 88 if ( event.isMetaDown() ) // right mouse button is pressed 89 { 90 drawColor = ERASE_COLOR; 91 drawDiameter = ERASE_DIAMETER; 92 } DrawJPanel.java(4 of 6)

  40. Using the Graphics object to draw a circle 93 else // left mouse button is pressed 94 { 95 drawColor = DRAW_COLOR; 96 drawDiameter = DRAW_DIAMETER; 97 } 98 99 repaint(); // repaint this DrawJPanel 100 101 } // end method drawJPanelMousePressed 102 103 // draw a small circle at the mouse's location 104 publicvoid paintComponent( Graphics g ) 105 { 106 g.setColor( drawColor ); // set the color 107 108 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 } 114 115 } // end method paintComponent 116 DrawJPanel.java(5 of 6)

  41. 117 // draw a circle on this DrawJPanel 118 privatevoid drawJPanelMouseDragged( MouseEvent event ) 119 { 120 // store the location of the mouse in currentPoint 121 currentPoint = event.getPoint(); 122 123 repaint(); // repaint this DrawJPanel 124 125 } // end method drawJPanelMouseDragged 126 127 } // end class DrawJPanel DrawJPanel.java(6 of 6)

  42. 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 publicclass Painter extends JFrame 9 { 10 // DrawJPanel for circles drawn by user 11 private DrawJPanel myDrawJPanel; 12 13 // no-argument constructor 14 public Painter() 15 { 16 createUserInterface(); 17 } 18 19 // set up the GUI components 20 publicvoid createUserInterface() 21 { 22 // get content pane for attaching GUI components 23 Container contentPane = getContentPane(); 24 Painter.java(1 of 2)

  43. 25 // enable explicit positioning of GUI components 26 contentPane.setLayout( null ); 27 28 // set up myDrawJPanel 29 myDrawJPanel = new DrawJPanel(); 30 myDrawJPanel.setBounds( 0, 0, 300, 300 ); 31 contentPane.add( myDrawJPanel ); 32 33 // 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 37 38 } // end method createUserInterface 39 40 // main method 41 publicstaticvoid main( String[] args ) 42 { 43 Painter application = new Painter(); 44 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 45 46 } // end method main 47 48 } // end class Painter Painter.java(2 of 2)

More Related