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 ovalsan arc is simply a section of an oval.
Figure 7.22. Drawing a rainbow using arcs and an array of colors.
(This item is displayed on pages 325 - 326 in the print version)
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 = new Color( 75, 0, 130 ); 12 13 // colors to use in the rainbow, starting from the innermost 14 // The two white entries result in an empty arc in the center 15 private Color colors[] = 16 { Color.WHITE, Color.WHITE, VIOLET, INDIGO, Color.BLUE, 17 Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED }; 18 19 // constructor 20 public DrawRainbow() 21 { 22 setBackground( Color.WHITE ); // set the background to white 23 } // end DrawRainbow constructor 24 25 // draws a rainbow using concentric circles 26 public void paintComponent( Graphics g ) 27 { 28 super.paintComponent( g ); 29 30 int radius = 20; // radius of an arch 31 32 // draw the rainbow near the bottom-center 33 int centerX = getWidth() / 2; 34 int centerY = getHeight() - 10; 35 36 // draws filled arcs starting with the outermost 37 for ( int counter = colors.length; counter > 0; counter-- ) 38 { 39 // set the color for the current arc 40 g.setColor( colors[ counter - 1 ] ); 41 42 // fill the arc from 0 to 180 degrees 43 g.fillArc( centerX - counter * radius, 44 centerY - counter * radius, 45 counter * radius * 2, counter * radius * 2, 0, 180 ); 46 } // end for 47 } // end method paintComponent 48 } // end class DrawRainbow |
Figure 7.23. Creating JFrame to display a rainbow.
(This item is displayed on page 327 in the print version)
1 // Fig. 7.23: DrawRainbowTest.java 2 // Test application to display a rainbow. 3 import javax.swing.JFrame; 4 5 public class DrawRainbowTest 6 { 7 public static void main( String args[] ) 8 { 9 DrawRainbow panel = new DrawRainbow(); 10 JFrame application = new JFrame(); 11 12 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 13 application.add( panel ); 14 application.setSize( 400, 250 ); 15 application.setVisible( true ); 16 } // end main 17 } // end class DrawRainbowTest |
Figure 7.22 begins with the usual import statements for creating drawings (lines 35). Lines 910 declare and create two new colorsVIOLET and INDIGO. As you may know, the colors of a rainbow are red, orange, yellow, green, blue, indigo and violet. Java only has predefined constants for the first five colors. Lines 1517 initialize an array with the colors of the rainbow, starting with the innermost arcs first. The array begins with two Color.WHITE elements, which, as you will soon see, are for drawing the empty arcs at the center of the rainbow. Note that the instance variables can be initialized when they are declared, as shown in lines 1017. The constructor (lines 2023) contains a single statement that calls method setBackground (which is inherited from class JPanel) with the parameter Color.WHITE. Method setBackground takes a single Color argument and sets the background of the component to that color.
Line 30 in paintComponent declares local variable radius, which determines the thickness of each arc. Local variables centerX and centerY (lines 3334) determine the location of the midpoint on the base of the rainbow. The loop at lines 3746 uses control variable counter to count backwards from the end of the array, drawing the largest arcs first and placing each successive smaller arc on top of the previous one. Line 40 sets the color to draw the current arc from the array. The reason we have Color.WHITE entries at the beginning of the array is to create the empty arc in the center. Otherwise, the center of the rainbow would just be a solid violet semicircle. [Note: You can change the individual colors and the number of entries in the array to create new designs.]
The fillArc method call at lines 4345 draws a filled semicircle. Method fillArc requires six parameters. The first four represent the bounding rectangle in which the arc will be drawn. The first two specify the coordinates for the upper-left corner of the bounding rectangle, and the next two specify its width and height. The fifth parameter is the starting angle on the oval, and the sixth specifies the sweep, or the amount of arc to cover. The starting angle and sweep are measured in degrees, with zero degrees pointing right. A positive sweep draws the arc counter-clockwise, while a negative sweep draws the arc clockwise. A method similar to fillArc is drawArcit requires the same parameters as fillArc, but draws the edge of the arc rather than filling it.
Class DrawRainbowTest (Fig. 7.23) creates and sets up a JFrame to display the rainbow. Once the program makes the JFrame visible, the system calls the paintComponent method in class DrawRainbow to draw the rainbow on the screen.
GUI and Graphics Case Study Exercise
7.1 |
(Drawing Spirals) In this exercise, you will draw spirals with methods drawLine and drawArc.
|
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