Java Reference
In-Depth Information
10
// obtain user's choice
11
String input = JOptionPane.sho
wIn
putDialog(
12
"Enter 1 to draw rectangles
"
+
\n
13
"Enter 2 to draw ovals"
);
14
15
int
choice = Integer.parseInt(input);
// convert input to int
16
17
// create the panel with the user's input
18
Shapes panel =
new
Shapes(choice);
19
20
JFrame application =
new
JFrame();
// creates a new JFrame
21
22
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
23
application.add(panel);
24
application.setSize(
300
,
300
);
25
application.setVisible(
true
);
26
}
27
}
// end class ShapesTest
Fig. 5.28
|
Obtaining user input and creating a
JFrame
to display
Shapes
. (Part 2 of 2.)
GUI and Graphics Case Study Exercises
5.1
Draw 12 concentric circles in the center of a
JPanel
(Fig. 5.29). The innermost circle
should have a radius of 10 pixels, and each successive circle should have a radius 10 pixels larger than
the previous one. Begin by finding the center of the
JPanel
. To get the upper-left corner of a circle,
move up one radius and to the left one radius from the center. The width and height of the bound-
ing rectangle are both the same as the circle's diameter (i.e., twice the radius).