Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


To create a color class, all you need to do is use the new operator and supply the three-color components to the Color class’s constructor. The following code illustrates how it’s 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.

Table 10.2 Standard Colors

Color Name RGB Value

Color.white 255, 255, 255
Color.black 0, 0, 0
Color.lightGray 192, 192, 192
Color.gray 128, 128, 128
Color.darkGray 64, 64, 64,
Color.red 255, 0, 0
Color.green 0, 255, 0
Color.blue 0, 0, 255
Color.yellow 255, 255, 0
Color.magenta 255, 0, 255
Color.cyan 0, 255, 255
Color.pink 255, 175, 175
Color.orange 255, 200, 0

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 you’re 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 );


Tip:  You’ll have a hard time getting the exact color you want if you have to rely on your judgment (except for a handful of colors that you might have memorized). The way I find the exact color I want is to run Microsoft Paint. I then double-click on a color to bring up the Edit Colors dialog box. Then I click on the Define Custom Colors button, which brings up an addition to the dialog box that then lets me experiment with colors. I can find out what any numeric value will look like, or use a color selector to go the other direction and find the numeric values for a color.

The FontMetrics Class

You 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:

  Leading: This is pronounced ledding. This is the amount of space between lines of text.
  Ascent: This is the height of a character, from the baseline to the top.
  Descent: This is the distance from the baseline to the bottom of a descending character, such as j, y, and g.
  Height: This is the sum of leading, ascent, and descent.

The concepts in the preceding list are illustrated in Figure 10.6.


Figure 10.6  Java’s font terminology originated mostly in the publishing field.

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 );

}


Previous Table of Contents Next


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.