|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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 objects 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); } }
Displaying TextJavas 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:
Figure 39.12 illustrates the relationships among descent, ascent, baseline, and leading.
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 Javas 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 applets window. The y coordinate is distance, in pixels, from the top of the applets window to the texts 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 applets 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); } }
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.
|
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. |