/** A version of SimpleApplet3 using arrays, for-loops, and conds.
 **
 ** Modified to SimpleApplet4 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:  SimpleApplet4.html

/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet4 extends Applet
   {String text = "Java has a population of 107,573,749 (1990).";
 
     public void init()
        {setBackground(Color.white);}          // Change the gray background to white.
 
    /** CREATE THE CURRENT FRAME **/

    public void paint(Graphics g)
       {Font font[] = new Font[4];                                            // Define and initialize an array
         font[0] = new Font("TimesRoman",Font.PLAIN,18);     // (list) of different fonts.
         font[1] = new Font("TimesRoman",Font.BOLD,18);
         font[2] = new Font("TimesRoman",Font.ITALIC,18);
         font[3] = new Font("TimesRoman",Font.BOLD+Font.ITALIC,18);
 
         FontMetrics fontmet[] = new FontMetrics[4];         // Define an array of font metrics
         int fontht[] = new int[4];                                        // and an array of font heights.
         int yspacing = size().height;                                    // Also, initialize the spacing
                                                                                     // between lines.
         for (int i = 0; i < 4; i++)
           {fontmet[i] = getFontMetrics(font[i]);                   // Initialize the font metrics.
             fontht[i] = fontmet[i].getHeight();                       // Initialize the font heights.
             yspacing -= fontht[i];                                         // Compute the spacing between
             if ( i = = 3) {yspacing /= 5;}                               // lines.
             }
 
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:  SimpleApplet4.html
 
         int xstart[] = new int[4];                                 // Define an array of x-coords.
         int ystart[] = new int[4];                                 // Define an array of y-coords.
         g.setColor(Color.blue);
 
         for (int i = 0; i < 4; i++)
            {xstart[i] = (size().width - fontmet[i].stringWidth(text))/2;       // Compute x-coords.
              if ( i = = 0)
              {ystart[i] = yspacing + fontmet[i].getAscent();}                    // Compute y-coords.
             else
              {ystart[i] = ystart[i-1] + fontmet[i-1].getDescent() +
                                                                       yspacing + fontmet[i].getAscent();}
             g.setFont(font[i]);
             g.drawString(text,xstart[i],ystart[i]);                                      // Paint text.
             }
         }
   }