home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap17 / FontApplet3.java < prev    next >
Encoding:
Java Source  |  1996-02-22  |  1.2 KB  |  51 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class FontApplet3 extends Applet
  5. {
  6.     Button button;
  7.     int fontNum;
  8.  
  9.     public void init()
  10.     {
  11.         button = new Button("Next Font");
  12.         add(button);
  13.         fontNum = 0;
  14.     }
  15.  
  16.     public void paint(Graphics g)
  17.     {
  18.         Font font;
  19.  
  20.         if (fontNum == 0)
  21.             font = new Font("Courier", Font.BOLD, 32);
  22.         else if (fontNum == 1)
  23.             font = new Font("TimesRoman", Font.BOLD, 32);
  24.         else
  25.             font = new Font("Helvetica", Font.BOLD, 32);
  26.  
  27.         g.setFont(font);
  28.         FontMetrics fontMetrics = g.getFontMetrics(font);
  29.         int height = fontMetrics.getHeight();
  30.  
  31.         int row = 80;
  32.         g.drawString("This is the first line.", 20, row);
  33.         row += height;
  34.         g.drawString("This is the second line.", 20, row);
  35.         row += height;
  36.         g.drawString("This is the third line.", 20, row);
  37.         row += height;
  38.         g.drawString("This is the fourth line.", 20, row);
  39.     }
  40.  
  41.     public boolean action(Event event, Object arg)
  42.     {
  43.         ++fontNum;
  44.         if (fontNum == 3)
  45.             fontNum = 0;
  46.  
  47.         repaint();
  48.         return true;
  49.     }
  50. }
  51.