|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Using Javas String ClassThe Java Graphics class provides a method for displaying text by drawing a string of characters. The method, the drawString() method shown here, takes a String object as a parameter: public abstract void drawString(String str, int x, int y)
If you put double quotation marks around a string, Java automatically makes a String object. You then pass an x coordinatethe integer value that represents the beginning position of the text in number of pixels from the left edge of the applets window, and pass a y coordinatethe distance in pixels from the top of the applets window to the texts baseline. Making Strings Automatically The StringObjects applet in Listing 39.11 demonstrates two ways of passing a String object to the drawString() method. A String object is automatically built in the argument list of line 12. The drawString method in line 15 is passed to a String object assigned to the myString variable, which was built in line 5. This call to the drawString method produces text displayed in red, because the color is changed in line 14. The StringObjects applets output is identical to that shown in Figure 39.13. Listing 39.11 StringObjects.javaYou Can Make a String Automatically or Explicitly import java.awt.Graphics; import java.awt.Color; public class StringObjects extends java.applet.Applet { String myString = new String(Hello World!); public void paint(Graphics g) { // Draw Hello World! in blue. g.setColor(Color.blue); g.drawString(Hello World!, 10, 20); // Draw Hello World! in red. g.setColor(Color.red); g.drawString(myString, 10, 50); } } Building One String from Another The String object assigned to the myString variable was initiated with another String object through the following String class constructor: public String(String value) Building Strings from Byte Arrays Javas String class provides nine constructors, including the preceding one, so you have several options for building a String object. In fact, you can build String objects from the byte arrays or character arrays you used for the drawBytes and drawChars methods previously discussed. You can use one of the following two constructors when you create String objects from byte arrays: public String(byte bytes[]) public String(byte bytes[], int offset, int length) The first parameter in both constructors, bytes[], is the byte array. In the second constructor, the offset parameter refers to the position of the first byte in the array to draw. The length parameter is the total number of bytes to include in the output. The StringsOne applet demonstrates these two String constructors in Listing 39.12. The String object assigned to the bytesString variable is passed to an array of ASCII codes. The String object assigned to the bytesAnotherString variable is passed to the array of ASCII codes; in addition, a 6 is passed to the offset parameter and another 6 is passed to the length parameter.
Listing 39.12 StringsOne.javaBuild Strings from Byte Arrays import java.awt.Graphics; import java.awt.Color; public class StringsOne extends java.applet.Applet { byte[] bytesToDraw = { 72, 101, 108, 108, 111, 32, ⇒87, 111, 114, 108, 100, 33 }; String bytesString = new String( bytesToDraw ); String bytesAnotherString = new String( bytesToDraw, 6, 6 ); public void paint(Graphics g) { // Draw Hello World! in blue. g.setColor(Color.blue); g.drawString(bytesString, 10, 20); // Draw World! in red. g.setColor(Color.red); g.drawString(bytesAnotherString, 10, 50); } } The StringsOne applet output is identical to the output shown in Figure 39.13. Building Strings From Character Arrays You use one of the following two constructors when you make String objects from character arrays: public String(char value[]) public String(char value[], int offset, int count) These constructors behave analogously to the two constructors that use byte arrays. Making an Empty String If you pass nothing to a String object when you make it, youll get an empty string: public String() You can then use the classs methods to dynamically change the content. Class String has more than 40 properties and methodssee your documentation to learn how to manipulate Strings.
Using Javas Font ClassYou may find that the default font you have been working with so far is not very interesting. Fortunately, you can select from a number of fonts. Javas Graphics class provides the setFont() method, defined here, so that you can change your texts font characteristics: public abstract void setFont(Font font)
|
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. |