Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Using the Font Class

Colors and fonts are represented in Java by the Color and Font classes, which both belong to the java.awt package. With these classes, you can present text in several different fonts and sizes, and change the colors of text, graphics, and other elements.

One of the principles of object-oriented programming is to make an object work for itself, and the Font and Color objects follow this rule. They store all the information that's needed to display a font or change a color, and they can handle other related tasks that are required.

There are three things you need to know about a font to display it:

Before you can display text in a certain typeface, style, and point size, you need to create a Font object that holds this information. The following statement creates a 12-point serif italic Font object:

Font currentFont = new Font("Serif", Font.ITALIC, 12); 

When selecting a typeface, it's better to choose one of the descriptive names: Serif, SanSerif, and Monospaced. This enables the system running the program to designate one of its own fonts that fits the description.

You can, however, refer to specific fonts, which will only be used if they are installed on the computer of the person running your program.

You choose the style of the font by using one or more constant variables. Specifying the style as Font.PLAIN makes it non-bold and non-italic, Font.BOLD makes it bold, and Font.ITALIC makes it italic. To combine bold and italic, use Font.BOLD+Font.ITALIC, as in the following code:

Font headlineFont = new Font("Courier New", Font.BOLD+Font.ITALIC, 72); 

The last argument specifies the point size of the font.

There are several ways to make use of fonts in a Java program:

The first project you undertake during this hour draws text on a panel. This class uses the second option for using fonts—overriding a panel's paintComponent() method. Open your word processor and create a new file called Player.java, entering the text of Listing 21.1 in the file.

Example 21.1. The Full Text of Player.java

 1: import java.awt.*; 
 2: import javax.swing.*; 
 3: 
 4: public class Player extends JFrame { 
 5:     public Player() { 
 6:         super("Player"); 
 7:         setSize(244, 286); 
 8:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 9:         PlayerPanel fp = new PlayerPanel(); 
10:         Container pane = getContentPane(); 
11:         pane.add(fp); 
12:         setContentPane(pane); 
13:         setVisible(true); 
14:     } 
15: 
16:     public static void main(String[] arguments) { 
17:         Player frame = new Player(); 
18:     } 
19: } 
20: 
21: class PlayerPanel extends JPanel { 
22:     public void paintComponent(Graphics comp) { 
23:         super.paintComponent(comp); 
24:         Graphics2D comp2D = (Graphics2D)comp; 
25:         int width = getSize().width; 
26:         int height = getSize().height; 
27:         Font currentFont = new Font("Dialog", Font.BOLD, 18); 
28:         comp2D.setFont(currentFont);  
29:         comp2D.drawString("ARMANDO BENITEZ", width - 185, height - 30); 
30:         currentFont = new Font("Dialog", Font.ITALIC, 12); 
31:         comp2D.setFont(currentFont); 
32:         comp2D.drawString("pitcher", width - 170, height - 10); 
33:         currentFont = new Font("Dialog", Font.PLAIN, 12); 
34:         comp2D.setFont(currentFont); 
35:         comp2D.drawString("NEW YORK METS", width - 110, height - 10); 
36:     } 
37: } 

After you compile the file with javac or another compiler, run the application. The program should resemble Figure 21.1.

21fig01.gif

Figure 21.1 Running the Player application.

The Player application is a frame that contains a panel where several strings will be drawn using different font objects. All of the work involving fonts takes place in the FontPanel class, which is defined in Lines 22–35.

The paintComponent( Graphics ) method of a container functions the same as the paint( Graphics ) method in an applet. This method is called automatically whenever the container needs to be redrawn.

The first thing that takes place in the method is the call to super.paintComponent(), which calls the method in the superclass to make sure that everything is set up correctly before you begin drawing in the panel.

Next, to make use of Java2D, the Graphics object is used to create a Graphics2D object called comp2D in Line 24. All subsequent font and drawing methods will be called on this object.

A Font object is created in Line 27 that represents a Dialog, bold 18-point font. The setFont() method of comp2D is called to make this the current font.

Next, the drawString( String ) method of comp2D is called with the text of a string to display. The same thing happens with different fonts and strings in Lines 30–35.

The Player application uses the width and height of the panel to determine where the text should be drawn. The first text, the name of New York Mets closer Armando Benitez, is displayed 185 pixels to the left of the right edge and 30 pixels above the bottom edge. If you resize the application window, the text will move accordingly.

Share ThisShare This

Informit Network