7.13. (Optional) GUI and Graphics Case Study: Drawing Arcs

Using Java’s graphics features, we can create complex drawings that would be more tedious to code line by line. In Fig. 7.22 and Fig. 7.23, we use arrays and repetition statements to draw a rainbow by using Graphics method fillArc. Drawing arcs in Java is similar to drawing ovals—an arc is simply a section of an oval.

Figure 7.22. Drawing a rainbow using arcs and an array of colors.
 1 // Fig. 7.22: DrawRainbow.java
 2 // Demonstrates using colors in an array.
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import javax.swing.JPanel;
 6
 7 public class DrawRainbow extends JPanel
 8 {
 9    // Define indigo and violet
10    final Color VIOLET = new Color( 128, 0, 128 );
11    final Color INDIGO ...

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.