< Day Day Up > |
Using the Font ClassColors 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 only will 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:
Displaying fonts nicely requires Java's support for anti-aliasing, a graphical technique that draws lines more smoothly. Without the technique, some lines appear jagged, especially in diagonal directions and curves. In anti-aliased graphics, the color of pixels at the edges of a line are altered in color, making the lines cleaner and less blocky in appearance. A Swing graphical interface component only employs anti-aliasing when explicitly requested by using a rendering hint. A Graphics2 object has a setRenderingHint() method that accomplishes this task. The method takes two arguments:
These values are instance variables in the RenderingHints class of java.awt. To activate anti-aliasing, call setRenderingHint() with two arguments: comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); The first project you undertake during this hour draws text on a panel under a graphic. This class draws fonts by overriding a panel's paintComponent() method. Open your word processor and create a new file called Player.java, entering the text of Listing 22.1 in the file. Listing 22.1. The Full Text of Player.java1: import java.awt.*; 2: import javax.swing.*; 3: 4: public class Player extends JFrame { 5: public Player() { 6: super("Player"); 7: setSize(294, 396); 8: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 9: PlayerPanel fp = new PlayerPanel(); 10: add(fp); 11: setVisible(true); 12: } 13: 14: public static void main(String[] arguments) { 15: Player frame = new Player(); 16: } 17: } 18: 19: class PlayerPanel extends JPanel { 20: public PlayerPanel() { 21: ImageIcon player = new ImageIcon("johnny-evers-1913.jpg"); 22: JLabel playerLabel = new JLabel(player); 23: add(playerLabel); 24: } 25: 26: public void paintComponent(Graphics comp) { 27: super.paintComponent(comp); 28: Graphics2D comp2D = (Graphics2D)comp; 29: comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 30: RenderingHints.VALUE_ANTIALIAS_ON); 31: int width = getSize().width; 32: int height = getSize().height; 33: Font currentFont = new Font("Dialog", Font.BOLD, 18); 34: comp2D.setFont(currentFont); 35: comp2D.drawString("JOHNNY EVERS", width - 155, height - 50); 36: currentFont = new Font("Dialog", Font.ITALIC, 12); 37: comp2D.setFont(currentFont); 38: comp2D.drawString("second baseman", width - 155, height - 30); 39: currentFont = new Font("Dialog", Font.PLAIN, 12); 40: comp2D.setFont(currentFont); 41: comp2D.drawString("CHICAGO CUBS", width - 155, height - 10); 42: } After you compile the file with javac or another compiler, you'll need the graphic of Hall of Fame second baseman Johnny Evers. Visit the book's official website at http://www.java24hours.com, then open the Hour 22 page. Right-click the link to johnny-evers-1913.jpg and store it in the same folder as Player.java. When that's done, run the application. The program should resemble Figure 22.1. Figure 22.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 PlayerPanel class, which is defined in Lines 19–42. 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 28. All subsequent font and drawing methods will be called on this object, such as the call to set up anti-aliasing in Lines 29–30. A Font object is created in Line 33 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 36–41. The Player application uses the width and height of the panel to determine where the text should be drawn. The first text, the name Johnny Evers, is displayed 155 pixels to the left of the right edge and 50 pixels above the bottom edge. If you resize the application window, the text moves accordingly. |
< Day Day Up > |