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


Using Java’s String Class

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


FIGURE 39.13  The drawBytes method displays the first line of text in blue, and the drawChars method displays the second line of text in red.

If you put double quotation marks around a string, Java automatically makes a String object. You then pass an x coordinate—the integer value that represents the beginning position of the text in number of pixels from the left edge of the applet’s window, and pass a y coordinate—the distance in pixels from the top of the applet’s window to the text’s 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 applet’s 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 Java’s 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.


To count your offset, start with zero. An offset of six ends up on W in “Hello World!” Don’t forget to count spaces as characters.

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, you’ll get an empty string:

public String()

You can then use the class’s methods to dynamically change the content. Class String has more than 40 properties and methods—see your documentation to learn how to manipulate Strings.


CAUTION:  

For security reasons, Java’s strings are immutable—after you make one, you cannot change it. When you manipulate it (for example, by extracting substrings) the Java runtime makes a new string. All this building and releasing of string storage can have an impact on performance.



If your program manipulates passwords, store them as a char array rather than as a String object. After you’ve used the password, overwrite it, minimizing the time the password is in memory.

Using Java’s Font Class

You 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. Java’s Graphics class provides the setFont() method, defined here, so that you can change your text’s font characteristics:

public abstract void setFont(Font  font)


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.