5.10. (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals
This section introduces two other
Figure 5.26. Drawing a cascade of shapes based on the
|
1 // Fig. 5.26: Shapes.java 2 // Demonstrates drawing different shapes. 3 import java.awt.Graphics; 4 import javax.swing.JPanel; 5 6 public class Shapes extends JPanel 7 { 8 private int choice; // user's choice of which shape to draw 9 10 // constructor sets the user's choice 11 public Shapes( int userChoice ) 12 { 13 choice = userChoice; 14 } // end Shapes constructor 15 16 // draws a cascade of shapes starting from the top left corner 17 public void paintComponent( Graphics g ) 18 { 19 super .paintComponent( g ); 20 21 for ( int i = ; i < 10 ; i++ ) 22 { 23 // pick the shape based on the user's choice 24 switch ( choice ) 25 { 26 case 1 : // draw rectangles 27 g.drawRect( 10 + i * 10 , 10 + i * 10, 28 50 + i * 10 , 50 + i * 10 ); 29 break ; 30 case 2 : // draw ovals 31 g.drawOval( 10 + i * 10 , 10 + i * 10 , 32 50 + i * 10 , 50 + i * 10 ); 33 break ; 34 } // end switch 35 } // end for 36 } // end method paintComponent 37 } // end class Shapes |
Line 6 begins the class declaration for Shapes , which extends JPanel.Shapes contains one instance variable, choice , declared on line 8, that determines whether paintComponent should draw rectangles or ovals. The Shapes constructor at lines 1114 initializes choice with the value passed in parameter userChoice .
Method
paintComponent
(lines 1736)
If
choice
is 1, then the program draws a rectangle. Lines 2728 call
Graphics
method
drawRect
. Method
drawRect
requires four arguments. The first two arguments represent the
x-
and
y-
coordinates of the
If
choice
is 2, then the program performs a similar operation, drawing an oval instead of a rectangle. When drawing an oval, an imaginary rectangle called a
bounding rectangle
is created, and an oval that touches the midpoints of all four sides of the bounding rectangle is placed inside. Method
drawOval
(lines 3132) requires the same four arguments as method
drawRect
. The arguments specify the position and
Figure 5.27 is responsible for handling input from the user and creating a window to display the appropriate drawing based on the user's response. Line 3 imports JFrame to handle the display, and Line 4 imports JOptionPane to handle the input.
1 // Fig. 5.27: ShapesTest.java 2 // Test application that displays class Shapes. 3 import javax.swing.JFrame; 4 import javax.swing.JOptionPane; 5 6 public class ShapesTest 7 { 8 public static void main( String args[] ) 9 { 10 // obtain user's choice 11 String input = JOptionPane.showInputDialog( 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 ); // add the panel to the frame 24 application.setSize( 300 , 300 ); // set the desired size 25 application.setVisible( true ); // show the frame 26 } // end main 27 } // end class ShapesTest
|
Lines 1113 prompt the user with an input dialog and store the user's response in variable input . Line 15 uses Integer method parseInt to convert the String entered by the user to an int and stores the result in variable choice . An instance of class Shapes is created at line 18, with the user's choice passed to the constructor. Lines 2025 perform the standard operations for creating and setting up a windowcreating a frame, setting it to exit the application when closed, adding the drawing to the frame, setting the frame size and making it visible.
| 5.1 |
Draw 12 concentric circles in the center of a JPanel (Fig. 5.28). 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 bounding rectangle is the diameter of the circle (twice the radius). Figure 5.28. Drawing concentric circles.
|
| 5.2 |
Modify Exercise 5.16 from the end-of-chapter exercises to read input using dialogs and to display the bar chart using rectangles of varying lengths. |