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.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



TROUBLESHOOTING
How do you return to the original color assigned to your program’s Graphics object?
It’s a good policy to always save the color assigned to your program’s Graphics object to a Color object variable before you assign it a new color. That way you can reassign the original color to the Graphics object after the program is finished using the new color.

Listing 39.9 ColorPlay.javaWhen You Set the Color You Affect All Subsequent Drawing


import java.awt.Graphics;
import java.awt.Color;

public class ColorPlay extends java.applet.Applet
{
   public void paint(Graphics g)
   {
     // Assign the red Color object to newColor.
     Color newColor = new Color(0xFFA978);
     // Assign Graphic object’s current color to default Color.
     Color defaultColor = g.getColor();
     // Draw a red oval 200 pixels wide and 200 pixels
     //  high with the upper left corner of its
     //  enclosing rectangle at (50, 50).
     g.setColor(newColor);
     g.fillOval(50, 50, 200, 200);
     // Draw an oval 200 pixels wide and 200 pixels
     //  high with the upper left corner of its
     //  enclosing rectangle at (300, 50) in the
     //  default color.
     g.setColor(defaultColor);
     g.fillOval(300, 50, 200, 200);
   }
}


FIGURE 39.11  Make color graphics by changing the graphics context’s current color using the setColor method.

Displaying Text

Java’s Graphics class provides seven methods related to displaying text. Before plunging into the various aspects of drawing text, however, you should be familiar with the following common terms for fonts and text:

  Baseline is the imaginary line that the text rests on.
  Descent is the distance below the baseline that a particular character extends. The letters g and j, for instance, extend below the baseline.
  Ascent is the distance above the baseline that a particular character extends. For instance, the letter d has a higher ascent than the letter x.
  Leading is the space between a line of text’s lowest descent and the following line of text’s highest ascent. Without leading, the letters g and j would touch the letters M and h on the next line.

Figure 39.12 illustrates the relationships among descent, ascent, baseline, and leading.


FIGURE 39.12  Java’s font terminology originated in the publishing field.


CAUTION:  

The term “ascent,” as used in Java, is slightly different from the way the term is used in the publishing world. The publishing term “ascent” refers to the distance from the top of a letter such as x to the top of a character such as d. In contrast, the Java term “ascent” refers to the distance from baseline to the top of a character.



NOTE:  You may hear the terms “proportional” and “fixed” associated with fonts. Characters in a proportional font take up only as much space as they need. In a fixed font, every character takes up the same amount of space.

Most of the text in this book is in a proportional font. Compare the width of the letters in a word with a proportional font with the letters of the same word in a fixed font:

FIXIM
FIXIM

One simple way to display text in Java is to draw from an array of bytes representing ASCII characters or from an array of 16-bit Unicode characters. You can use an array of ASCII codes when you use the drawBytes() method, or you can use an array of characters when you use the drawChars() method. Both of these methods are available in Java’s Graphics class. They are defined as

public void drawBytes(byte  data[], int  offset, int  length,
 ⇒int  x, int  y)
public void drawChars(char  data[], int  offset, int  length,
 ⇒int  x, int  y)

The offset parameter refers to the position of the first character or byte in the array to draw. This is most often zero because you will usually want to draw from the beginning of the array. The length parameter is the total number of bytes or characters in the array. The x coordinate is the integer value that represents the beginning position of the text, in number of pixels, from the left edge of the applet’s window. The y coordinate is distance, in pixels, from the top of the applet’s window to the text’s baseline. The DrawChars applet in Listing 39.10 displays text from an array of ASCII codes in blue and text from an array of characters in red. Figure 39.13 shows the DrawChars applet’s output.

Listing 39.10 DrawChars.javaThis Applet Draws Text Based on Arrays of bytes and chars


import java.awt.Graphics;
import java.awt.Color;

public class DrawChars extends java.applet.Applet
{
   byte[] bytesToDraw = { 72, 101, 108, 108, 111, 32,
 ⇒87, 111, 114, 108, 100, 33 };
   char[] charsToDraw = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘,
 ⇒’W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’ };

   public void paint(Graphics g)
   {
     // Draw Hello World! from bytes in blue.
     g.setColor(Color.blue);
     g.drawBytes(bytesToDraw, 0, bytesToDraw.length, 10, 20);
     // Draw Hello World! from characters in red.
     g.setColor(Color.red);
     g.drawChars(charsToDraw, 0, charsToDraw.length, 10, 50);
   }
}


NOTE:  The numbers used in the byte array, bytesToDraw, are base-10 ASCII codes. They are the numbers that most computer systems use to represent letters. (Java uses the 16-bit Unicode, but for the common characters and punctuation marks of English the Unicode codes are identical to the ASCII codes.) You could use any base for these numbers, including the popular hexadecimal.

Arrays are objects in Java. You made two array objects when you built the two arrays, bytesToDraw and charsToDraw, in Listing 39.10. That is why you could use the array property, length, to get the lengths of the arrays in number of bytes or characters.

Java provides another object type, the String object, which is similar to the array objects that you just built. It is, however, more convenient for manipulating text.


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.