O'Reilly logo

Java™ How to Program, Seventh Edition by P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

This section demonstrates drawing rectangles and ovals, using the Graphics methods drawRect and drawOval, respectively. These methods are demonstrated in Fig. 5.26.

Figure 5.26. Drawing a cascade of shapes based on the user’s choice.
 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required