Click Here!
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


All the controls such as Label, TextArea, Choice, and Button have setFont() methods. This is because these controls are extended from the Container class that has a setFont() method. The following simple applet shows you a complete program in Listing 10.7 that creates and adds two labels with different fonts. The program is shown running in Figure 10.13.

Listing 10.7 Source Code for the Program That Creates and Adds Two Labels with Different Fonts

1   import java.awt.*;
2   import java.applet.*;
3
4   public class Applet1 extends Applet
5   {
6
7       public void init()
8       {
9           Label label1, label2;
10
11          label1 = new Label( "This is label 1" );
12          Font font1 = new Font( "TimesRoman", Font.PLAIN, 25 );
13          label1.setFont( font1 );
14          label2 = new Label( "This is label 2" );
15          Font font2 = new Font( "Courier", Font.BOLD | Font.ITALIC, 35 );
16          label2.setFont( font2 );
17
18          add( label1 );
19          add( label2 );
20      }
21
22  }


Figure 10.13  This is what the simple program shown in Listing 10.7 looks like when it runs.

When you add controls such as Labels and Buttons in a Visual J++ application, you can easily set the font by changing the control’s properties.

Summary

In this chapter you’ve learned how to create fonts, how to select them into the Graphics class, and how to draw to the Java window. You’ve also learned how to create colors and select them for your drawing. Using text and fonts in your Visual J++ programs is an essential part of creating professional-looking programs.

The techniques shown in this chapter are just the basics. It’s up to you now to take what you’ve learned and experiment with it.

Q&A

Q Why do we need to know about the Graphics class to draw text?

A Drawing text to a Java screen is part of Java’s Graphics class. Anytime you draw to the Java window, you need to use the Graphics methods. The most notable exceptions to this rule are the Java controls. They take care of drawing text themselves. But you can be sure that somewhere within the inner workings of the Java controls, text is being drawn in a way similar to what you do when you draw text with the Graphics class.

Q In what methods does the Java window perform screen updates in response to window and mouse events?

A For applets, screen updates happen in the paint() method. For applications, screen updates happen in the onPaint() method. Neither of these methods is created when the startup code for a project is generated by Visual J++. You must add them yourselves. When added, they override the default methods.

Q What are the repaint() and update() methods?

A The repaint() method can be called from anywhere in a Java applet to force the screen to be updated. It’s usually called when a display element changes and a screen redraw is necessary. The update() method is called in response to a request for a screen update. This can be either as a result of a window or mouse event, or the program actually calling the repaint() method. You should never call the update() method yourself.

Q How is the Java screen coordinate system laid out?

A The upper-left corner of the window starts at 0,0. As you move to the right, the x value increases. For each pixel you move, the value increments by one. As you move down, the y value increase. As with the x values, for each pixel you move, the value increments by one.

Q What is the Font class?

A The Font class is a Java object that can be created and that represents a font. When it’s created, the font type name, font style, and font size must be specified. Font objects must be selected into the Graphics class before they’re actually used.

Q Which font styles are available?

A There are three font styles: PLAIN, ITALIC, and BOLD. You can combine them by adding their values.

Q What methods can be used to draw text to the Java window?

A There are two: the drawString() and drawChars() methods. By far, the drawString() method is the easiest to use. You’ll end up using it 99% of the time.

Q If the drawChars() method is more difficult to use than the drawString() method, why use it?

A Because it gives you greater control over how the string is drawn. You can draw each character separately. It also draws the contents of a char buffer. This means that you can load in data from a file or URL and then display it.

Q How can I change the color with which my text draws?

A You must first create a Color class. Color classes take three values: red, green, and blue. Depending on how you mix these three color channels, you can get any one of millions of colors. There are 13 predefined Color classes for the most common colors, including white, black, red, blue, and orange.

Q How can I find out the pixel width of a text string?

A You can use the FontMetrics class. For any font, you simply obtain a FontMetrics class, then use its getStringWidth() member method.

Q Why do you need to know which fonts are available?

A Some Java Virtual Machines, especially for different operating systems, might have different fonts available. Your presentation might depend on the way the text looks. For this reason, you might want to find out which fonts are available.


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.