|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
To create a color class, all you need to do is use the new operator and supply the three-color components to the Color classs constructor. The following code illustrates how its done: Color color = new Color( r, g, b ); These three component values, represented here by r, g, and b, are integer values. Once again, they should contain values ranging from 0 to 255. You can also create a Color class by specifying explicit RGB values in the color constructor. For instance, if you want the red to be at half intensity, you can specify 128, which is approximately half of 255. If you want green and blue to be somewhat less, you can specify 100. This will give you a reddish-gray tint. The following example shows you how to create such a color: Color color = new Color( 128, 100, 100 ); The Color class has 13 predefined colors that might be all you need. Using these is much easier than creating custom colors by using RGB values. They represent the common colors of black, blue, cyan, dark gray, and so forth. Table 10.2 shows these values and their RGB equivalents.
After a color class has been created, you must select it into the Graphics class before any draw operations can use it. To do this, you use the setColor() method. The following source-code example shows you how to use the setColor() method: g.setColor( color ); Anytime you want to change the color youre drawing with, you must select the desired color into the Graphics class. You can also set colors into the canvas itself. The canvas is the main window of the applet. To do this, simply use the setForeground() and setBackground() methods. The following example shows how to create two colors and then set the foreground color and the background color of the canvas: Color color_f = new Color( 255, 255, 0 ); Color color_b = new Color( 0, 255, 255 ); setForeground( color_f ); setBackground( color_b );
The FontMetrics ClassYou can use the FontMetrics class to compute the exact length and width of a string. This is helpful for measuring a string size in order to display it in the right position. For example, you can center strings in the viewing area using the FontMetrics class. A FontMetrics class has the following attributes:
The concepts in the preceding list are illustrated in Figure 10.6.
To get a FontMetrics object for either the selected font or a specific font, choose one of the following: FontMetrics fm = g.getFontMetrics(); FontMetrics fm = g.getFontMetrics( font ); Notice that one line in the example gives the getFontMetrics() method an argument, and one does not. The version of the getFontMetrics() method that does not take an argument returns the FontMetrics class for whatever font is currently selected into the Graphics class. The getFontMetrics() method that takes an argument of a Font class returns a FontMetric class for the Font class that was passed into it. After you have a FontMetrics class, you will frequently use five methods: getAscent(), getDescent(), getLeading(), getHeight(), and stringWidth(). I have written a short method that takes two arguments: a Graphics class and a Fonts class. It returns a string that contains all the information available about the font. The source code for this method follows: String getFontInfo(Graphics g, Font font) { String strInfo; FontMetrics fm; fm = g.getFontMetrics( font ); strInfo = "The ascent is " + fm.getAscent() + ". "; strInfo += "The descent is " + fm.getDescent() + ". "; strInfo += "The leading is " + fm.getLeading() + ". "; strInfo += "The height is " + fm.getHeight() + ". "; strInfo += "The string width of 'Hello' is " + fm.getStringWidth( "Hello" ) + "."; return( strInfo ); }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |