(Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals

(Optional) GUI and Graphics Case Study Drawing Rectangles and Ovals

This section introduces two other shapes you can draw using the graphics features in Javarectangles and ovals. To draw rectangles and ovals, we call Graphics methods drawRect and drawOval, respectively, as demonstrated in Fig. 5.26.

Figure 5.26. Drawing a cascade of shapes based on the user's choice.

(This item is displayed on pages 213 - 214 in the print version)

 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 = 0; 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) performs the actual drawing. Remember, the first statement in every paintComponent method should be a call to super.paintComponent, as in line 19. The for statement (lines 2135) loops 10 times to draw 10 shapes. The switch statement (Lines 2434) chooses between drawing rectangles and drawing ovals.

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 upper-left corner of the rectangle. The next two represent the width and the height of the rectangle. 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, 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 size of the bounding rectangle for the oval. The values passed to drawOval in this example are exactly the same as the values passed to drawRect in lines 2728. Since the width and height of the bounding rectangle are identical in this example, lines 2728 draw a circle. You may modify the program to draw both rectangles and ovals to see how drawOval and drawRect are related.

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.

Figure 5.27. Obtaining user input and creating a JFrame to display Shapes.

(This item is displayed on page 215 in the print version)

 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
" +
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.

GUI and Graphics Case Study Exercises

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.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net