Chapter 11 Graphics and Java2D 609 Line 39 (Web design conference)

Chapter 11 Graphics and Java2D 609 Line 39 sets the current drawing color to one of the predefined Color constants (Color.blue). Note that the new operator is not needed to create the constant. The Color constants are static, so they are defined when class Color is loaded into memory at execution time. The statement at lines 47 48 demonstrates Colormethods getRed, getGreenand getBlueon the predefined Color.magentaobject. Notice lines 56 57 in main. JFrame method setDefaultCloseOperation specifies the default action to take when the user clicks the close box on an application window. In this case, we specify JFrame.EXIT_ON_CLOSEto indicate that the program should terminate when the user clicks the close box. Other options are DO_NOTHING_ON_CLOSE(to ignore the window-closing event), HIDE_ON_CLOSE(to hide the window, such that it can be redisplayed later) and DISPOSE_ON_CLOSE(to dispose of the window, such that it cannot be redisplayed later). From this point forward, we implement our own WindowListener only if the program should perform additional tasks when the user clicks the window s close box. Otherwise, we use method setDefaultCloseOperation to specify that the program should terminate when the user clicks the close box. Software Engineering Observation 11.2 To change the color, you must create a new Color object (or use one of the predefined Colorconstants); there are no set methods in class Colorto change the characteristics of the current color. One of the newer features of Java is the predefined GUI component JColor- Chooser (package javax.swing) for selecting colors. The application of Fig. 11.6 enables you to press a button to display a JColorChooser dialog. When you select a color and press the dialog s OK button, the background color of the application window changes colors. 1 // Fig. 11.6: ShowColors2.java 2 // Demonstrating JColorChooser. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class ShowColors2 extends JFrame { 12 private JButton changeColorButton; 13 private Color color = Color.lightGray; 14 private Container container; 15 16 // set up GUI 17 public ShowColors2() 18 { 19 super( “Using JColorChooser” ); 20 Fig. 11.6 Demonstrating the JColorChooserdialog (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply