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


Finding Out Which Fonts Are Available

If you’re using Windows or a similar operating system environment, you can have many fonts installed on your system. Unfortunately, Java applets can’t access your system fonts. Java applications, however, can.

For Java applets, depending on your version of the Java Virtual Machine (JVM), the fonts that are available to the JVM might differ slightly from those of another user. To help you identify the fonts you have available, you can use the getFontList() method.


Note:  Understanding the Toolkit Class

The Toolkit class is an abstract class you can use to get information about your system, such as the available fonts, screen resolution, and screen size. To access the Toolkit object, your applet must call the getDefaultToolkit() method, using the method’s return as a reference to the object. In addition to the getFontList() method used in the FontQuery1 applet, you might want to use the getScreenResolution() method, which returns the screen’s resolution in dots per inch.


The source code for the complete FontQuery1 applet is shown in Listing 10.5. The applet is shown running in Figure 10.7.

Listing 10.5 The FontQuery1 Applet

1   // FontQuery1.java
2
3   import java.awt.*;
4   import java.applet.*;
5
6   public class FontQuery1 extends Applet
7   {
8       public void init()
9       {
10      }
11
12      TextArea txArea = new TextArea();
13
14      public void populateFontList()
15      {
16          Toolkit toolkit = Toolkit.getDefaultToolkit();
17          String strFontList[] = toolkit.getFontList();
18          for( int i=0; i<strFontList.length; i++ )
19              txArea.append( strFontList[i] + "\n" );
20      }
21
22}


Figure 10.7  The FontQuery1 applet.

Fonts in Visual J++ Applications

If you’re writing an application in Visual J++, selecting and setting fonts couldn’t be easier. All you have to do to select the font for a label, or any other control for that matter, is pull up the Font Selector Common dialog box.

Let’s start by creating a new Visual J++ Windows application named ChangeFonts. Double-click the Form1.java file in the Project Explorer window so that the form window is displayed. Place four labels on the form as shown in Figure 10.8.


Figure 10.8  Place four labels on the blank form.

In the Properties window, find the font property as shown in Figure 10.9.


Figure 10.9  Find the font property in the Properties window.

If you click on the button that’s to the right of the font property field, a font selector box will appear. It’ll allow you to select the font type, style, and size as shown in Figure 10.10. Go ahead now and change the font type for all four labels. You can see my program in Figure 10.11—all four labels have a different font.


Figure 10.10  The font selector allows you to set the font type, style, and size.


Figure 10.11  All four labels of the form are set in different fonts.

A Marquee Applet

And now for the question everyone who has a home page is asking: How do I create one of those marquee applets that continuously scrolls a text message?

When programmers who aren’t familiar with Java find out I know Java, that’s one of the first questions they ask. Well, this section is dedicated to those individuals who are dying to put such an applet on their home page. Figure 10.12 shows a simple marquee applet that’s running. (It’s hard to capture the motion in a book!) The source code for the applet is given in Listing 10.6.


Figure 10.12  A simple marquee applet.

Listing 10.6 Source Code for a Simple Marquee Applet

1   // Applet1.java
2
3   import java.awt.*;
4   import java.applet.*;
5
6   /**
7    * This class reads PARAM tags from its HTML host page and sets
8    * the color and label properties of the applet. Program execution
9    * begins with the init() method.
10   */
11  public class Applet1 extends Applet implements Runnable
12  {
13      private Thread     m_Applet1 = null;
14      int x = 740;
15      int y = 45;
16      String m_strMessage = "Visual J++ is really cool. AND I MEAN
        ⇒COOL!!!";
17
18      /**
19       * The entry point for the applet.
20       */
21      public void init()
22      {
23          initForm();
24      }
25
26      /**
27       * Initializes values for the applet and its components
28       */
29      void initForm()
30      {
31          this.setBackground(Color.lightGray);
32          this.setForeground(Color.black);
33      }
34
35      public void destroy()
36      {
37      }
38
39      public void paint(Graphics g)
40      {
41          FontMetrics fm = g.getFontMetrics();
42          g.drawString( m_strMessage, x, y );
43          x -= 5;
44          if( x < -fm.stringWidth( m_strMessage ) )
45              x = 740;
46      }
47
48      public void start()
49      {
50          if (m_Applet1 == null)
51          {
52              m_Applet1 = new Thread(this);
53              m_Applet1.start();
54          }
55      }
56
57      public void stop()
58      {
59          if (m_Applet1 != null)
60          {
61              m_Applet1.stop();
62              m_Applet1 = null;
63          }
64      }
65
66      public void run()
67      {
68          while (true)
69          {
70              try
71              {
72                  repaint();
73                  Thread.sleep(50);
74              }
75              catch (InterruptedException e)
76              {
77                  stop();
78              }
79          }
80      }
81
82}

Setting the Font of a Control

At times you will want to change the font of a control in your program. Controls use a default font that can change from machine to machine. If you want complete control over what your controls look like, you’ll want to explicitly set their font instead of relying on the default font, which might not be what you’re expecting.

Setting the font of a control is easy: simply create a new font and then use the control’s setFont() method. The following example creates a Label control, creates a new font, calls the label’s setFont() method, and then adds the label to the container:

Label label1 = new Label( "This is a new label" );
Font font = new Font( "TimesRoman", Font.PLAIN, 25 );
label1.setFont( font );
add( label1 );


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.