Java Reference
In-Depth Information
Line 6 begins the class declaration for
Shapes
, which extends
JPanel
.
I
nstance vari-
able
choice
determines whether
paintComponent
should draw rectangles or ovals. The
Shapes
constructor initializes
choice
with the value passed in parameter
userChoice
.
Method
paintComponent
performs the actual drawing. Remember, the first statement
in every
paintComponent
method should be a call to
super.paintComponent
, as in line
19. Lines 21-35 loop 10 times to draw 10 shapes. The
nested
switch
statement (lines 24-
34) chooses between drawing rectangles and drawing ovals.
If
choice
is 1, then the program draws rectangles. Lines 27-28 call
Graphics
method
drawRect
. Method
drawRect
requires four arguments. The first two represent the
x-
and
y-
coordinates of the upper-left corner of the rectangle; the next two represent the rect-
angle's width and height. In this example, we start at a position 10 pixels down and 10
pixels right of the top-left corner, and every iteration of the loop moves the upper-left
corner another 10 pixels down and to the right. The width and the height of the rectangle
start at 50 pixels and increase by 10 pixels in each iteration.
If
choice
is 2, the program draws ovals. It creates an imaginary rectangle called a
bounding rectangle
and places inside it an oval that touches the midpoints of all four sides.
Method
drawOval
(lines 31-32) requires the same four arguments as method
drawRect
.
The arguments specify the position and size of the bounding rectangle for the oval. The
values passed to
drawOval
in this example are exactly the same as those passed to
drawRect
in lines 27-28. Since the width and height of the bounding rectangle are identical in this
example, lines 27-28 draw a
circle
. As an exercise, try modifying the program to draw both
rectangles and ovals to see how
drawOval
and
drawRect
are related.
Class
ShapesTest
Figure 5.28 is responsible for handling input from the user and creating a window to dis-
play the appropriate drawing based on the user's response. Line 3 imports
JFrame
to han-
dle the display, and line 4 imports
JOptionPane
to handle the input. Lines 11-13 prompt
the user with an input dialog and store the user's response in variable
input
. Notice that
when displaying multiple lines of prompt text in a
JOptionPane
, you must use
\n
to begin
a new line of text, rather than
%n
. Line 15 uses
Integer
method
parseInt
to convert the
String
entered by the user to an
int
and stores the result in variable
choice
. Line 18 cre-
ates a
Shapes
object and passes the user's choice to the constructor. Lines 20-25 perform
the standard operations that create and set up a window in this case study—create a
frame
,
set it to exit the application when closed, add the drawing to the frame, set the frame size
and make it visible.
1
// Fig. 5.28: ShapesTest.java
2
// Obtaining user input and creating a JFrame to display Shapes.
3
import
javax.swing.JFrame;
//handle the display
4
import
javax.swing.JOptionPane;
5
6
public
class
ShapesTest
7
{
8
public
static
void
main(String[] args)
9
{
Fig. 5.28
|
Obtaining user input and creating a
JFrame
to display
Shapes
. (Part 1 of 2.)