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

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.