/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 9.2:  Many Different Fonts, and from
 ** Listing Listing 9.3:  Centering a String.
 **
 ** Modified to SimpleApplet3 by John Pais, December 1997.   **/
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet3.html
 
/** USE EXISTING JAVA CLASSES **/
 
import java.awt.*;
import java.applet.Applet;
 
/** EXTEND THE JAVA APPLET CLASS **/
 
public class SimpleApplet3 extends Applet
   {Font font1 = new Font("TimesRoman",Font.PLAIN,18);  // Define and initialize
    Font font2 = new Font("TimesRoman",Font.BOLD,18);   // SimpleApplet3 class variables.
    Font font3 = new Font("TimesRoman",Font.ITALIC,18); // The different fonts.
    Font font4 = new Font("TimesRoman",Font.BOLD+Font.ITALIC,18);
 
    FontMetrics font1m = getFontMetrics(font1);         // The different font metrics.
    FontMetrics font2m = getFontMetrics(font2);
    FontMetrics font3m = getFontMetrics(font3);
    FontMetrics font4m = getFontMetrics(font4);
 
    String text = "Java is densely populated.";
 
    public void init()
       {setBackground(Color.white);}                          // Change the gray background to white.
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet3.html
 
   /** CREATE THE CURRENT FRAME **/
 
    public void paint(Graphics g)
       {int xstart1 = (size().width - font1m.stringWidth(text))/2; // Center text horizontally.
         int xstart2 = (size().width - font2m.stringWidth(text))/2;
         int xstart3 = (size().width - font3m.stringWidth(text))/2;
         int xstart4 = (size().width - font4m.stringWidth(text))/2;
 
         int font1A = font1m.getAscent();                  // Contrary to the usual
         int font1D = font1m.getDescent();                // description, the Ascent
         int font1L = font1m.getLeading();                 // of a font contains white
         int font1H = font1m.getHeight();                  // space at the top.
 
         int font1Awhite = font1D - font1L;                // The Ascent white space.
         int font1Atext = font1A - font1Awhite;          // The Ascent text space.
         int font1text = font1Atext + font1D;              // The actual text space.
 
         int font2A = font2m.getAscent();
         int font2D = font2m.getDescent();
         int font2L = font2m.getLeading();
         int font2H = font2m.getHeight();
 
         int font2Awhite = font2D - font2L;                // The Ascent white space.
         int font2Atext = font2A - font2Awhite;          // The Ascent text space.
         int font2text = font2Atext + font2D;              // The actual text space.
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet3.html
 
         int font3A = font3m.getAscent();
         int font3D = font3m.getDescent();
         int font3L = font3m.getLeading();
         int font3H = font3m.getHeight();
 
          int font3Awhite = font3D - font3L;                // The Ascent white space.
          int font3Atext = font3A - font3Awhite;          // The Ascent text space.
          int font3text = font3Atext + font3D;              // The actual text space.
 
          int font4A = font4m.getAscent();
          int font4D = font4m.getDescent();
          int font4L = font4m.getLeading();
          int font4H = font4m.getHeight();
 
          int font4Awhite = font4D - font4L;                // The Ascent white space.
          int font4Atext = font4A - font4Awhite;          // The Ascent text space.
          int font4text = font4Atext + font4D;              // The actual text space.
 
          int yspacing =                                               // Compute vertical spacing.
               (size().height - (font1text + font2text + font3text + font4text))/5;
 
          int ystart1 = yspacing + font1Atext;              // Align text vertically.
          int ystart2 = ystart1 + font1D + yspacing + font2Atext;
          int ystart3 = ystart2 + font2D + yspacing + font3Atext;
          int ystart4 = ystart3 + font3D + yspacing + font4Atext;
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet3.html
 
         g.setColor(Color.blue);
         g.setFont(font1);
         g.drawString(text,xstart1,ystart1);
         g.setFont(font2);
         g.drawString(text,xstart2,ystart2);
         g.setFont(font3);
         g.drawString(text,xstart3,ystart3);
         g.setFont(font4);
         g.drawString(text,xstart4,ystart4);
         }
   }