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


When the applet runs, you’ll see two lines of text. The first line will be in a smaller font, saying, “This is the first line.” The second line will be in a larger italic font, saying, “This is the second line.” You can see the applet running in Figure 10.4.


Figure 10.4  This applet creates and uses two different fonts.


Note:  The default applet size that’s created is 320×200. For this applet, you’ll need to change the width so that these large text strings will fit in the window. I changed mine to 720×200 and the text strings could be seen completely.

Drawing Text

We’ve already talked about drawing text to a Graphics class using the drawString() method. This method takes three different arguments: the first argument is the string you want to draw to the Graphics class; the second argument is the x coordinate to which you’ll be drawing; and the third argument is the y coordinate.

There is another method that belongs the Graphics class that allows you to draw text to the window. It is called drawChars(). This method contains five arguments: the first argument is a character buffer containing the characters you want drawn to the screen; the second argument is an integer indicating the index of the starting character you want to draw; the third argument tells it how many characters to draw; the fourth argument is the x coordinate; and the fifth argument is the y coordinate.


Note:  You can draw a text string with the drawChars() method and begin at any character within the string. For instance, you might have a string “This is a test” and just want “is a test” to be output. In this case, the starting character you’d specify would be 5 because the “i” character is at index 5.

Following is a short example showing how to allocate a character buffer, assign it characters, and then draw to the window using the drawChars() method:

char txt[] = new char [10];
txt[0] = 'H';
txt[1] = 'e';
txt[2] = 'l';
txt[3] = 'l';
txt[4] = 'o';
g.drawChars( txt, 0, 5, 50, 50 );

The drawChars() method is obviously not as easy to use as the drawString() method. You’ll use the drawChars() method only in rare circumstances when you need extra control over what’s being drawn on the screen. One example of this situation might be when you’ve loaded data from a file, or data from some sort of Internet URL, and you want to display only part of it. Another example of this is when you have a text string and you want more control over where each character is displayed in the window. You could display the first character at a certain coordinate, then add 15 to the x coordinate and add 5 to the y coordinate for the second character to make a more interesting display of your character string. I’ve written a method called drawCrazyText(). It takes that same “Hello” character buffer and draws it at random y locations, so it looks sort of crazy.

The drawCrazyText() method is used in the program shown in Listing 10.4. The result is shown in Figure 10.5.

Listing 10.4 Source Code for a Program That Uses the drawCrazy() Method

1   import java.awt.*;
2   import java.applet.*;
3
4   public class Applet1 extends Applet
5   {
6
7       public void init()
8       {
9       }
10
11      public void paint( Graphics g )
12      {
13          drawCrazyText( g );
14      }
15
16      public void drawCrazyText(Graphics g)
17      {
18          Font font = new Font( "TimesRoman", Font.PLAIN, 20 );
19          g.setFont( font );
20
21          char txt[] = new char [10];
22          txt[0] = 'H';
23          txt[1] = 'e';
24          txt[2] = 'l';
25          txt[3] = 'l';
26          txt[4] = 'o';
27
28          g.drawChars( txt, 0, 5, 50, 50 );
29
30          for( int i=0; i<5; i++ )
31          {
32              int nRnd = (int) ( Math.random() * 10.0 );
33              g.drawChars( txt, i, 1, 50 + i * 10, 80 + nRnd );
34          }
35
36      }
37
38  }


Figure 10.5  This program calls the drawCrazy() method, which displays the string drawn normally and the string drawn haphazardly.

The drawCrazy() method starts off by creating a new font and selecting it into the Graphics class in lines 8 and 9. A char buffer is created at line 11, and lines 12–16 assign characters to the first five array elements. Line 18 draws the character to the screen with a normal appearance. But in the for loop that starts at line 20, the characters are drawn one at a time, each with random x and y offsets so that the characters appear to be drawn haphazardly.

Random Numbers

You might have noticed in the drawCrazyText() method that a random number was generated using the Math.random() method. The Math.random() method generates a floating-point number from 0.0 to 1.0. If you want to get a range of values from 0 to 5, as in this example, multiply by 5.0. That way you get all sorts of floating-point (decimal) values from 0.0 to 5.0. Whatever your range is, you multiply Math.random() by your highest number. Then you need to make sure that if you need an integer instead of an actual floating-point number, you cast it, as I did in the drawCrazyText() method.

Setting Colors in Java

You can set the color with which the Graphics class will draw in Java by first creating a Color class and then selecting the Color class into the Graphics class. Colors have three components: a red, a green, and a blue component. Each component is represented by a byte value that describes the color’s intensity. These values range from 0 to 255. The lower the number, the less of that color is mixed in the resulting shade. For instance, if the red component has a value of 0, there will be no red in the resulting shade. If the green component has a value of 255, the resulting shade will contain the most green allowed. These three-color components are commonly referred to as the RGB model, with RGB standing for red, green, and blue.


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.