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


The setFont method takes a Font object as its argument. Java provides a Font class that gives you a lot of text-formatting flexibility. The Font class provides a single constructor

public Font(String  name, int  style, int  size)

Pass the name of the font, surrounded by double quotation marks, to the name parameter. The availability of fonts varies from system to system, so it is a good idea to make sure that the user has the font you want. You can check the availability of a font by using the Toolkit class getFontList method defined here:

public abstract String[] getFontList()

TROUBLESHOOTING
How do you avoid demanding the use of a font in your program that a user does not have?
Always query for the available fonts by using the getFontList method, and then use only those fonts in your program. This dynamic method of using fonts is the best policy for distributed computing software such as Java programs.

Typically, you don’t import the Toolkit class. Instead, you use the Applet class getToolkit() method (which is inherited from the Component class), defined as follows:

public Toolkit getToolkit()

You use the style parameter to set the font style. Bold, italic, plain, as well as the combination (bold and italic) are available. The Font class provides three class constants: Font.BOLD, Font.ITALIC, and Font.PLAIN, which you can use in any combination to set the font style. To set a font to bold, for example, pass Font.BOLD to the style parameter. If you want to display a bold italic font, you pass Font.BOLD + Font.ITALIC to Font class’s style parameter.

Finally, you set the point size of the font by passing an integer to Font class’s size parameter. The point size is a printing term. When printing on a printer, an inch has 100 points, but this does not necessarily apply to screen fonts. Just as in your word processor, a typical point size value for printed text is 12 or 14. The point size does not indicate the number of pixels high or wide; it is a relative term. A point size of 24 is twice as big as a point size of 12.

All this information is pulled together in the ShowFonts applet, shown in Listing 39.13. Figure 39.14 shows the output from the ShowFonts applet.

Listing 39.13 ShowFonts.javaUse This Simple Applet to Display the Variety of Fonts Available


import java.awt.Graphics;
import java.awt.Font;

public class ShowFonts extends java.applet.Applet
{
   public void paint(Graphics g)
   {
     String fontList[];
     int startY = 15;

     // Get the list of fonts installed on
     //  this computer.
     fontList = getToolkit().getFontList();

     // Go through the list of fonts and draw each
     //  to the applet window.
     for (int i=0; i < fontList.length; i++)
     {
        // Draw font name in plain type.
        g.setFont(new Font(fontList[i], Font.PLAIN, 12));
        g.drawString(“This is the “ + fontList[i] + “ font.”,
 ⇒5, startY);
        startY += 15;

        // Draw font name in bold type.
        g.setFont(new Font(fontList[i], Font.BOLD, 12));
        g.drawString(“This is the bold “+ fontList[i] + “ font.”,
 ⇒5, startY);
        startY += 15;

        // Draw font name in italic type.
        g.setFont(new Font(fontList[i], Font.ITALIC, 12));
        g.drawString(“This is the italic “ + fontList[i] + “ font.”,
 ⇒5, startY);
        startY += 20;
     }
   }
}


FIGURE 39.14  Java provides a number of fonts and font styles.

Displaying Images

To use Java to display images, you must first get the image, and then you must draw it. The Java Applet class provides methods for getting images, and the Java Graphics class provides methods for drawing them.

Java’s Applet class provides two getImage() methods, listed here:

public Image getImage(URL  url)
public Image getImage(URL  url, String  name)

In the first method listed, you provide an URL class. You can just type the URL surrounded by double quotation marks, or you can create an URL object and then pass it to the getImage() method. If you do the latter, be sure to import the URL class. The URL class is part of the java.net package.

Whichever way you pass the URL, the first method takes the whole path, including the filename of the image itself. Because images are usually aggregated into a single directory or folder, it is usually handier to keep the path and filename separate.

The second method takes the URL path to the image as the url parameter, and it takes the filename—or even part of the path and the filename—as a string enclosed by double quotation marks and passed to the name parameter.

The Applet class provides two particularly useful methods, the getDocumentBase() and getCodeBase() methods, as follows:

public URL getDocumentBase()
public URL getCodeBase()

The getDocumentBase() method returns an URL object containing the path to where the HTML document resides that displays the Java applet. Similarly, the getCodeBase() method returns an URL object that contains the path to where the applet is running the code. Using these methods makes your applet flexible. You and others can use your applet on different servers, directories, or folders.

Java’s Graphics class provides drawImage() methods to use for displaying images. The most basic method is listed here:

public abstract boolean drawImage(Image  img, int  x, int  y,
 ⇒ImageObserver  observer)


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.