Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application."— Presentation transcript:

1 © Copyright 1992-2004 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

2 © Copyright 1992-2004 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.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 21.1 Test-Driving the Painter Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 21.1 Test-Driving the Painter Application (Cont.) Figure 21.1 Painter application before drawing.

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 21.1 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

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 21.1 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

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 21.2 Constructing the Painter Application (Cont.)

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 21.3 Interfaces (Cont.) Figure 21.6 Calling the addMouseListener method. Adding a MouseListener object The MouseListener interface declares event handler headers for mouse events.

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 21.3 Interfaces (Cont.) Creating an anonymous inner class Figure 21.7 Creating an instance of the MouseListener interface.

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 21.3 Interfaces (Cont.) Figure 21.8 Declaring event handlers for the MouseListener interface. Empty event handlers in the anonymous inner class

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 21.4 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

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 21.4 The mousePressed Event Handler (Cont.) Figure 21.10 Storing the position of the mouse cursor. Calling method getPoint to store the mouse’s position The getPoint method returns a Point object

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 21.4 The mousePressed Event Handler (Cont.) Figure 21.11 Setting the color and diameter of the circle.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 21.4 The mousePressed Event Handler (Cont.) Figure 21.12 Setting the color using method setColor.

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 21.4 The mousePressed Event Handler (Cont.) Figure 21.13 Drawing the circle using method fillOval. Calling method fillOval The fillOval method of class Graphics draws a filled oval.

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 21.4 The mousePressed Event Handler (Cont.) Figure 21.14 General oval. Bounding box A bounding box specifies an oval’s upper-left x - and y - coordinates, width and height

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 21.4 The mousePressed Event Handler (Cont.) Figure 21.15 Running the application.

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 21.5 The mouseReleased Event Handler Figure 21.16 Declaring constants for the released circle’s color and size.

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 21.5 The mouseReleased Event Handler (Cont.) Figure 21.17 Coding the mouseReleased event handler. Adding code to the mouseReleased event handler

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 21.5 The mouseReleased Event Handler (Cont.) Figure 21.18 Storing the location of the mouse’s cursor. Coding the drawJPanelMouseReleased method

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 21.5 The mouseReleased Event Handler (Cont.) Figure 21.19 Setting the color and size of the circle to be drawn.

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 21.5 The mouseReleased Event Handler (Cont.) Figure 21.20 Running the application. Drawing a flower using only mouseReleased and mousePressed event handlers

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 21.6 The mouseDragged Event Handler (Cont.) Figure 21.22 Removing the method call to drawJPanelMouseReleased.

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 21.6 The mouseDragged Event Handler (Cont.) Figure 21.23 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

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 21.6 The mouseDragged Event Handler (Cont.) Figure 21.24 Calling method addMouseMotionListener. Adding a MouseMotionListener to the DrawJPanel The addMouseMotionListener method of the DrawJPanel class registers an event listener with an event source

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 21.6 The mouseDragged Event Handler (Cont.) Figure 21.25 Creating an anonymous inner class. Creating an anonymous inner class that implements the MouseMotionListener interface

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 21.6 The mouseDragged Event Handler (Cont.) Figure 21.26 Coding the mouseDragged event handler.

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 21.6 The mouseDragged Event Handler (Cont.) Figure 21.27 Storing the location of the mouse’s cursor.

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 21.6 The mouseDragged Event Handler (Cont.) Figure 21.28 Running the completed Painter application.

36  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; 11 12 // constants for the drawn circle 13 private final Color DRAW_COLOR = Color.BLUE; 14 private final int DRAW_DIAMETER = 8; 15 16 // constants for the erase circle 17 private final Color ERASE_COLOR = this.getBackground(); 18 private final int ERASE_DIAMETER = 8; 19 20 // instance variables for the circle 21 private Color drawColor; 22 private int drawDiameter; 23

37  2004 Prentice Hall, Inc. All rights reserved. Outline 37 DrawJPanel.java (2 of 6) 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 public void mouseClicked( MouseEvent event ) 33 { 34 } 35 36 // event handler called when mouse enters this DrawJPanel 37 public void mouseEntered( MouseEvent event ) 38 { 39 } 40 41 // 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

38  2004 Prentice Hall, Inc. All rights reserved. Outline 38 46 // event handler called when a mouse button is pressed 47 public void mousePressed( MouseEvent event ) 48 { 49 drawJPanelMousePressed( event ); 50 } 51 52 // event handler called when a mouse button is released 53 public void 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 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

39  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 } 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 } Determining which mouse button was pressed Calling method getPoint to store the mouse’s position

40  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 } 98 99 repaint(); // repaint this DrawJPanel 100 101 } // end method drawJPanelMousePressed 102 103 // draw a small circle at the mouse's location 104 public void 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 Using the Graphics object to draw a circle

41  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(); 122 123 repaint(); // repaint this DrawJPanel 124 125 } // end method drawJPanelMouseDragged 126 127 } // end class DrawJPanel

42  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; 12 13 // no-argument constructor 14 public Painter() 15 { 16 createUserInterface(); 17 } 18 19 // set up the GUI components 20 public void createUserInterface() 21 { 22 // get content pane for attaching GUI components 23 Container contentPane = getContentPane(); 24

43  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 ); 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 public static void 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


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application."

Similar presentations


Ads by Google