5.11 (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.27.

 1   // Fig. 5.27: Shapes.java 2   // Drawing a cascade of shapes based on the user’s choice. 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 choice11      public Shapes(int userChoice)12      {13         choice = userChoice;14      }15 16      // draws a cascade of shapes starting from the top-left corner17       ...

Get Java™ How To Program (Early Objects), Tenth 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.