simplyjava1_21
Download
Report
Transcript simplyjava1_21
1
Tutorial 21 – “Cat and Mouse” Painter
Application
Introducing Interfaces, Mouse Input; the EventHandling Mechanism
Outline
21.1
21.2
21.3
21.4
21.5
21.6
21.7
Test-Driving the Painter Application
Constructing the Painter Application
Interfaces
The mousePressed Event Handler
The mouseReleased Event Handler
The mouseDragged Event Handler
Wrap-Up
© 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.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
21.1 Test-Driving the Painter Application
Application Requirements
The principal of an elementary school wants to introduce children to
computers by appealing to their creative side. Many elementary-level
applications test skills in mathematics, but the principal wishes to use an
application that allows children to express their artistic skills. Develop an
application that allows the student to “paint” on a JPanel, using the mouse.
The application should draw when the user moves the mouse with the left
mouse button held down and stop drawing when the left mouse button is
released. The application should draw small, filled, blue circles side by side to
trace out lines, curves and shapes. An important part of any drawing
application is the ability to erase mistakes. The user can erase a portion of the
drawing by moving the mouse over that portion of the drawing with the right
mouse button held down.
© 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Painter application before drawing.
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
© 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
© 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
© 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
9
21.2 Constructing the Painter Application
(Cont.)
Action
Get the mouse’s location
Component/Object
event (MouseEvent)
Store the mouse’s location
currentPoint (Point)
If left mouse button is pressed
Set the color to blue
event (MouseEvent)
Event/Method
User presses a
mouse button
drawColor (Color)
Set the diameter for drawing
drawDiameter (int)
Else
Set color to DrawJPanel’s
background color
Set the diameter for erasing
drawColor (Color)
Repaint the DrawJPanel
myDrawJPanel (DrawJPanel)
Store the mouse’s location
event (MouseEvent)
Repaint the DrawJPanel
myDrawJPanel (DrawJPanel)
Set the drawing color
g (Graphics object)
drawDiameter (int)
Draw a circle with appropriate
g (Graphics object)
diameter at mouse’s location
Figure 21.5 Painter application’s ACE table.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
User drags the
mouse
paintComponent
method is called
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
© 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.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
12
21.3 Interfaces (Cont.)
Figure 21.7
Creating an instance of the MouseListener interface.
Creating an anonymous
inner class
© 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
© 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
© 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
© 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
© 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.
© 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.
© 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.
© 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.
Width
(x,y )
Height
Bounding box
• A bounding box specifies an oval’s upper-left x- and ycoordinates, width and height
© 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Running the application.
22
21.5 The mouseReleased Event Handler
Figure 21.16
Declaring constants for the released circle’s color and size.
© 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
© 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
Coding the
drawJPanelMouseReleased
method
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
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.
© 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
Drawing a flower using only
mouseReleased and
mousePressed event handlers
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
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
© 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.
© 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
© 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.
© 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
© 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 that
implements the
MouseMotionListener
interface
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Creating an anonymous inner class.
33
21.6 The mouseDragged Event Handler (Cont.)
Figure 21.26
Coding the mouseDragged event handler.
© 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.
© 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.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Tutorial 21: DrawJPanel.java
// This class allows the user to draw and erase on the application.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
36
Outline
DrawJPanel.java
(1 of 6)
public class DrawJPanel extends JPanel
{
// Point to hold the mouse cursor's location
private Point currentPoint;
// constants for the drawn circle
private final Color DRAW_COLOR = Color.BLUE;
private final int DRAW_DIAMETER = 8;
// constants for the erase circle
private final Color ERASE_COLOR = this.getBackground();
private final int ERASE_DIAMETER = 8;
// instance variables for the circle
private Color drawColor;
private int drawDiameter;
2004 Prentice Hall, Inc.
All rights reserved.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// constructor
public DrawJPanel()
{
addMouseListener(
37
Outline
Adding a MouseListener
new MouseListener() // anonymous inner class
{
Creating an anonymous
// event handler called when
a mouse
inner
class button is clicked
public void mouseClicked( MouseEvent event )
{
}
// event handler called when mouse enters this DrawJPanel
public void mouseEntered( MouseEvent event )
{
}
// event handler called when mouse exits this DrawJPanel
public void mouseExited( MouseEvent event )
{
}
DrawJPanel.java
(2 of 6)
Empty event handlers
in the anonymous
inner class. To
implement the
MouseListener,
these methods must be
implemented
2004 Prentice Hall, Inc.
All rights reserved.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// event handler called when a mouse button is pressed
public void mousePressed( MouseEvent event )
{
drawJPanelMousePressed( event );
}
38
Outline
DrawJPanel.java
(3 of 6)
Calling the drawJPanelMousePressed
pressed
// event handler called when a mouse button is released
method when the mouse is
public void mouseReleased( MouseEvent event )
{
}
} // end anonymous inner class
); // end call to addMouseListener
addMouseMotionListener(
Empty event
handlers in the
anonymous
inner class
Adding a MouseMotionListener
to the DrawJPanel
new MouseMotionListener() // anonymous inner class Creating an anonymous inner class
{
that implements the
// event handler called when the mouse is dragged
MouseMotionListener interface
public void mouseDragged( MouseEvent event )
{
drawJPanelMouseDragged( event );
}
Calling the drawJPanelMouseDragged
method when the mouse is dragged
2004 Prentice Hall, Inc.
All rights reserved.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
39
// event handler called when the mouse is moved
public void mouseMoved( MouseEvent event )
{
}
Outline
DrawJPanel.java
(4 of 6)
} // end anonymous inner class
); // end call to addMouseMotionListener
}
// end constructor
// draw a circle on this DrawJPanel
private void drawJPanelMousePressed( MouseEvent event )
{
// store the location of the mouse
currentPoint = event.getPoint();
Calling method getPoint to
store the mouse’s position
if ( event.isMetaDown() ) // right mouse button is pressed
Determining which
{
button was pressed
drawColor = ERASE_COLOR;
drawDiameter = ERASE_DIAMETER;
}
mouse
2004 Prentice Hall, Inc.
All rights reserved.
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
else // left mouse button is pressed
{
drawColor = DRAW_COLOR;
drawDiameter = DRAW_DIAMETER;
}
40
Outline
DrawJPanel.java
(5 of 6)
repaint(); // repaint this DrawJPanel
} // end method drawJPanelMousePressed
// draw a small circle at the mouse's location
public void paintComponent( Graphics g )
{
g.setColor( drawColor ); // set the color
if ( currentPoint != null )
{
// draw a filled circle at the mouse's location
g.fillOval( currentPoint.x, currentPoint.y,
drawDiameter, drawDiameter );
}
Using the Graphics
object to draw a circle
} // end method paintComponent
2004 Prentice Hall, Inc.
All rights reserved.
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
41
Outline
DrawJPanel.java
(6 of 6)
2004 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Tutorial 21: Painter.java
// Application enables user to draw on a subclass of JPanel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
42
Outline
Painter.java
(1 of 2)
public class Painter extends JFrame
{
// DrawJPanel for circles drawn by user
private DrawJPanel myDrawJPanel;
// no-argument constructor
public Painter()
{
createUserInterface();
}
// set up the GUI components
public void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
2004 Prentice Hall, Inc.
All rights reserved.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up myDrawJPanel
myDrawJPanel = new DrawJPanel();
myDrawJPanel.setBounds( 0, 0, 300, 300 );
contentPane.add( myDrawJPanel );
43
Outline
Painter.java
(2 of 2)
// set properties of application's window
setTitle( "Painter" ); // set title bar text
setSize( 300, 300 );
// set window size
setVisible( true );
// display window
} // end method createUserInterface
// main method
public static void main( String[] args )
{
Painter application = new Painter();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class Painter
2004 Prentice Hall, Inc.
All rights reserved.