|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
When the applet runs, youll 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.
Drawing TextWeve 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 youll 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.
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. Youll use the drawChars() method only in rare circumstances when you need extra control over whats being drawn on the screen. One example of this situation might be when youve 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. Ive 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 }
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 1216 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.
Setting Colors in JavaYou 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 colors 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.
|
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. |