Archive for December, 2007

614 Graphics and Java2D (Photoshop web design) Chapter 11 Portability Tip

Monday, December 24th, 2007

614 Graphics and Java2D Chapter 11 Portability Tip 11.2 The number of fonts varies greatly across systems. The J2SDK guarantees that the fonts Serif, Monospaced, SansSerif, Dialogand DialogInputwill be available. Common Programming Error 11.2 Specifying a font that is not available on a system is a logic error. Java will substitute that system s default font. The program of Fig. 11.9 displays text in four different fonts, with each font in a different size. The program uses the Fontconstructor to initialize Fontobjects on lines 30, 35, 40 and 47 (each in a call to Graphicsmethod setFontto change the drawing font). Each call to the Font constructor passes a font name (Serif, Monospaced or Sans- Serif) as a String, a font style (Font.PLAIN, Font.ITALICor Font.BOLD) and a font size. Once Graphics method setFont is invoked, all text displayed following the call will appear in the new font until the font is changed. Note that line 35 changes the drawing color to red, so the next string displayed appears in red. 1 // Fig. 11.9: Fonts.java 2 // Using fonts 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 Fonts extends JFrame { 12 13 // set window’s title bar and dimensions 14 public Fonts() 15 { 16 super( “Using fonts” ); 17 18 setSize( 420, 125 ); 19 setVisible( true ); 20 } 21 22 // display Strings in different fonts and colors 23 public void paint( Graphics g ) 24 { 25 // call superclass’s paint method 26 super.paint( g ); 27 28 // set current font to Serif (Times), bold, 12pt 29 // and draw a string 30 g.setFont( new Font( “Serif”, Font.BOLD, 12 ) ); 31 g.drawString( “Serif 12 point bold.”, 20, 50 ); 32 Fig. 11.9 Using Graphicsmethod setFontto change Fonts (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Submit web site - Chapter 11 Graphics and Java2D 613 Method or

Sunday, December 23rd, 2007

Chapter 11 Graphics and Java2D 613 Method or constant Description public final static int PLAIN // Font class A constant representing a plain font style. public final static int BOLD // Font class A constant representing a bold font style. public final static int ITALIC // Font class A constant representing an italic font style. public Font( String name, int style, int size ) Creates a Fontobject with the specified font, style and size. public int getStyle() // Font class Returns an integer value indicating the current font style. public int getSize() // Font class Returns an integer value indicating the current font size. public String getName() // Font class Returns the current font name as a string. public String getFamily() // Font class Returns the font s family name as a string. public boolean isPlain() // Font class Tests a font for a plain font style. Returns trueif the font is plain. public boolean isBold() // Font class Tests a font for a bold font style. Returns trueif the font is bold. public boolean isItalic() // Font class Tests a font for an italic font style. Returns trueif the font is italic. public Font getFont() // Graphics class Returns a Fontobject reference representing the current font. public void setFont( Font f ) // Graphics class Sets the current font to the font, style and size specified by the Font object reference f. Fig. 11.8 Fontmethods, constants and font-related Graphicsmethods . Class Font s constructor takes three arguments the font name, font style and font size. The font name is any font currently supported by the system where the program is running, such as standard Java fonts Monospaced, SansSerifand Serif. The font style is Font.PLAIN, Font.ITALIC or Font.BOLD (static constants of class Font). Font styles can be used in combination (e.g., Font.ITALIC + Font.BOLD). The font size is measured in points. A point is 1/72 of an inch. Graphicsmethod set- Font sets the current drawing font the font in which text will be displayed to its Font argument. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web site translator - 612 Graphics and Java2D Chapter 11 These represent

Saturday, December 22nd, 2007

612 Graphics and Java2D Chapter 11 These represent three different ways to select a color. The HSB tab allows you to select a color based on hue, saturation and brightness. The RGB tab allows you to select a color by using sliders to select the red, green and blue components of the color. The HSB and RGB tabs are shown in Fig. 11.7. 11.4 Font Control This section introduces methods and constants for font control. Most font methods and font constants are part of class Font. Some methods of class Font and class Graphics are summarized in Fig. 11.8. Sliders to select the red, green and blue color components Fig. 11.7 The HSB and RGB tabs of the JColorChooserdialog. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Php web hosting - Chapter 11 Graphics and Java2D 611 Select a

Saturday, December 22nd, 2007

Chapter 11 Graphics and Java2D 611 Select a color from one of the color swatches. Fig. 11.6 Demonstrating the JColorChooserdialog (part 3 of 3). Lines 35 36 (from method actionPerformed for changeColor) use static method showDialog of class JColorChooser to display the color chooser dialog. This method returns the selected Colorobject (null, if the user presses Cancel or closes the dialog without pressing OK). Method showDialogtakes three arguments a reference to its parent Component, a String to display in the title bar of the dialog and the initial selected Color for the dialog. The parent component is the window from which the dialog is displayed. While the color chooser dialog is on the screen, the user cannot interact with the parent component. This type of dialog is called a modal dialog and is discussed in Chapter 13. Notice the special syntax ShowColors2.this used in the preceding statement. When using an inner class, you can access the outer class object s thisreference by qualifying thiswith the name of the outer class and the dot (.) operator. After the user selects a color, lines 39 40 determine whether coloris null, and, if so, set color to the default Color.lightGray. Line 43 uses method setBackgroundto change the background color of the content pane (represented by container in this program). Method setBackground is one of the many Component methods that can be used on most GUI components. The second screen capture of Fig. 11.6 demonstrates the default JColorChooser dialog that allows the user to select a color from a variety of color swatches. Notice that there are actually three tabs across the top of the dialog Swatches, HSB and RGB. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

610 Graphics and Java2D Chapter 11 21 (How to cite a web site) container

Friday, December 21st, 2007

610 Graphics and Java2D Chapter 11 21 container = getContentPane(); 22 container.setLayout( new FlowLayout() ); 23 24 // set up changeColorButton and register its event handler 25 changeColorButton = new JButton( “Change Color” ); 26 27 changeColorButton.addActionListener( 28 29 // anonymous inner class 30 new ActionListener() { 31 32 // display JColorChooser when user clicks button 33 public void actionPerformed( ActionEvent event ) 34 { 35 color = JColorChooser.showDialog( 36 ShowColors2.this, “Choose a color”, color ); 37 38 // set default color, if no color is returned 39 if ( color == null ) 40 color = Color.lightGray; 41 42 // change content pane’s background color 43 container.setBackground( color ); 44 } 45 46 } // end anonymous inner class 47 48 ); // end call to addActionListener 49 50 container.add( changeColorButton ); 51 52 setSize( 400, 130 ); 53 setVisible( true ); 54 } 55 56 // execute application 57 public static void main( String args[] ) 58 { 59 ShowColors2 application = new ShowColors2(); 60 61 application.setDefaultCloseOperation( 62 JFrame.EXIT_ON_CLOSE ); 63 } 64 65 } // end class ShowColors2 Fig. 11.6 Demonstrating the JColorChooserdialog (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

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

Thursday, December 20th, 2007

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

608 Graphics and Java2D Chapter 11 37 38 (Cheap web hosting)

Thursday, December 20th, 2007

608 Graphics and Java2D Chapter 11 37 38 // set new drawing color using static Color objects 39 g.setColor( Color.blue ); 40 g.fillRect( 25, 75, 100, 20 ); 41 g.drawString( “Current RGB: ” + g.getColor(), 130, 90 ); 42 43 // display individual RGB values 44 Color color = Color.magenta; 45 g.setColor( color ); 46 g.fillRect( 25, 100, 100, 20 ); 47 g.drawString( “RGB values: ” + color.getRed() + “, ” + 48 color.getGreen() + “, ” + color.getBlue(), 130, 115 ); 49 } 50 51 // execute application 52 public static void main( String args[] ) 53 { 54 ShowColors application = new ShowColors(); 55 56 application.setDefaultCloseOperation( 57 JFrame.EXIT_ON_CLOSE ); 58 } 59 60 } // end class ShowColors Fig. 11.5Demonstrating setting and getting a Color(part 2 of 2). Fig. 11.5 When the application begins execution, class ShowColors s paint method (lines 23 49) is called to paint the window. Line 29 uses Graphicsmethod setColor to set the current drawing color. Method setColor receives a Color object. The expression newColor(255,0,0) creates a new Color object that represents red (red value 255, and 0for the green and blue values). Line 30 uses Graphicsmethod fillRectto draw a filled rectangle in the current color. Method fillRectreceives the same parameters as method drawRect (discussed in Chapter 3). Line 31uses Graphics method drawString to draw a String in the current color. The expression g.getColor() retrieves the current color from the Graphicsobject. The returned Coloris concatenated with string “Current RGB:”, resulting in an implicit call to class Color s toString method. Notice that the String representation of the Color object contains the class name and package (java.awt.Color), and the red, green and blue values. Lines 34 36 and lines 39 41 perform the same tasks again. Line 34 uses the Color constructor with three floatarguments to create the color green (0.0ffor red, 1.0ffor green and 0.0f for blue). Note the syntax of the constants. The letter f appended to a floating-point constant indicates that the constant should be treated as type float. Normally, floating-point constants are treated as type double. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting service - Chapter 11 Graphics and Java2D 607 Common Programming

Wednesday, December 19th, 2007

Chapter 11 Graphics and Java2D 607 Common Programming Error 11.1 Spelling any staticColorclass constant with an initial capital letter is a syntax error. Two Colorconstructors are shown in Fig. 11.4 one that takes three intarguments, and one that takes three float arguments, with each argument specifying the amount of red, green and blue, respectively. The int values must be between 0 and 255 and the float values must be between 0.0 and 1.0. The new Color object will have the specified amounts of red, green and blue. Color methods getRed, getGreen and getBlue return integer values from 0 to 255 representing the amount of red, green and blue, respectively. Graphics method getColor returns a Color object representing the current drawing color. Graphicsmethod setColorsets the current drawing color. The application of Fig. 11.5 demonstrates several methods from Fig. 11.4 by drawing filled rectangles and strings in several different colors. 1 // Fig. 11.5: ShowColors.java 2 // Demonstrating Colors. 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 ShowColors extends JFrame { 12 13 // constructor sets window’s title bar string and dimensions 14 public ShowColors() 15 { 16 super( “Using colors” ); 17 18 setSize( 400, 130 ); 19 setVisible( true ); 20 } 21 22 // draw rectangles and Strings in different colors 23 public void paint( Graphics g ) 24 { 25 // call superclass’s paint method 26 super.paint( g ); 27 28 // set new drawing color using integers 29 g.setColor( new Color( 255, 0, 0 ) ); 30 g.fillRect( 25, 25, 100, 20 ); 31 g.drawString( “Current RGB: ” + g.getColor(), 130, 40 ); 32 33 // set new drawing color using floats 34 g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); 35 g.fillRect( 25, 50, 100, 20 ); 36 g.drawString( “Current RGB: ” + g.getColor(), 130, 65 ); Fig. 11.5Demonstrating setting and getting a Color(part 1 of 2). Fig. 11.5 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hosting your own web site - 606 Graphics and Java2D Chapter 11 ColorConstant Color

Tuesday, December 18th, 2007

606 Graphics and Java2D Chapter 11 ColorConstant Color RGB value public final static Color orange orange 255, 200, 0 public final static Color pink pink 255, 175, 175 public final static Color cyan cyan 0, 255, 255 public final static Color magenta magenta 255, 0, 255 public final static Color yellow yellow 255, 255, 0 public final static Color black black 0, 0, 0 public final static Color white white 255, 255, 255 public final static Color gray gray 128, 128, 128 public final static Color lightGray light gray 192, 192, 192 public final static Color darkGray dark gray 64, 64, 64 public final static Color red red 255, 0, 0 public final static Color green green 0, 255, 0 public final static Color blue blue 0, 0, 255 Fig. 11.3 Colorclass staticconstants and RGB values Method Description public Color( int r, int g, int b ) Creates a color based on red, green and blue contents expressed as integers from 0 to 255. public Color( float r, float g, float b ) Creates a color based on red, green and blue contents expressed as floating- point values from 0.0 to 1.0. public int getRed() // Color class Returns a value between 0 and 255 representing the red content. public int getGreen() // Color class Returns a value between 0 and 255 representing the green content. public int getBlue() // Color class Returns a value between 0 and 255 representing the blue content. public Color getColor() // Graphics class Returns a Colorobject representing the current color for the graphics context. public void setColor( Color c ) // Graphics class Sets the current color for drawing with the graphics context. Fig. 11.4 Colormethods and color-related Graphicsmethods . Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Free web hosts - Chapter 11 Graphics and Java2D 605 we have

Monday, December 17th, 2007

Chapter 11 Graphics and Java2D 605 we have been using in our applet classes. Actually, the Component class is an indirect base class of class JApplet the superclass of every applet in this book. Many capabilities of class JApplet are inherited from class Component. The paint method defined in class Component does nothing by default it must be overridden by the programmer. The paint method is seldom called directly by the programmer because drawing graphics is an event-driven process. When an applet executes, the paint method is automatically called (after calls to the JApplet s init and start methods). For paint to be called again, an event must occur (such as covering and uncovering the applet). Similarly, when any Component is displayed, that Component s paint method is called. If the programmer needs to call paint, a call is made to the Component class repaint method. Method repaintrequests a call to the Component class update method as soon as possible to clear the Component s background of any previous drawing, then update calls paint directly. The repaint method is frequently called by the programmer to force a paint operation. Method repaint should not be overridden, because it performs some system-dependent tasks. The update method is seldom called directly and sometimes overridden. Overriding the update method is useful for smoothing animations (i.e., reducing flicker ) as we will discuss in Chapter 18, Multimedia. The headers for repaint and update are public void repaint() public void update( Graphics g ) Method update takes a Graphics object as an argument, which is supplied automatically by the system when update is called. In this chapter, we focus on the paint method. In the next chapter, we concentrate more on the event-driven nature of graphics and discuss the repaint and update methods in more detail. We also discuss in that chapter class JComponent a superclass of many GUI components in package javax.swing. Subclasses of JComponenttypically paint from their paintComponent methods. 11.3 Color Control Colors enhance the appearance of a program and help convey meaning. For example, a traffic light has three different color lights red indicates stop, yellow indicates caution and green indicates go. Class Color defines methods and constants for manipulating colors in a Java program. The predefined color constants are summarized in Fig. 11.3, and several color methods and constructors are summarized in Fig. 11.4. Note that two of the methods in Fig. 11.4 are Graphics methods that are specific to colors. Every color is created from a red, a green and a blue component. Together these components are called RGB values. All three RGB components can be integers in the range from 0 to 255, or all three RGB parts can be floating-point values in the range 0.0 to 1.0. The first RGB part defines the amount of red, the second defines the amount of green and the third defines the amount of blue. The larger the RGB value, the greater the amount of that particular color. Java enables the programmer to choose from 256 256 256 (or approximately 16.7 million) colors. However, not all computers are capable of displaying all these colors. If this is the case, the computer will display the closest color it can. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01