1  /*
  2   *  File:  FontMetricsTest4.java
  3   *
  4   *  Given  n  strings, center them both vertically and
  5   *  horizontally in the applet window.
  6   *
  7   *  Copyright:  Northeast Parallel Architectures Center
  8   *  
  9   */
 10  
 11  import java.applet.Applet;
 12  import java.awt.Graphics;
 13  import java.awt.Font;
 14  import java.awt.Color;
 15  import java.awt.FontMetrics;
 16  
 17  public class FontMetricsTest4 extends Applet {
 18  
 19     private int n;       // number of strings
 20     private String[] s;  // an array of strings to be drawn
 21     
 22     public void init() {
 23        
 24        // Get the "numStrings" parameter from the HTML document:
 25        String nStr = getParameter( "numStrings" );
 26        // Convert string 'nStr' to an integer:
 27        n = ( nStr == null ) ? 0 : Integer.parseInt( nStr );
 28        
 29        // Instantiate an array of strings:
 30        s = new String[n];
 31        
 32        // Get the strings to be drawn from the HTML document:
 33        for ( int i = 0; i < n; i++ ) {
 34          s[i] = getParameter( "string" + i );
 35          if ( s[i] == null ) s[i] = "";
 36        }
 37        
 38        // Get the "fontName" parameter from the HTML document:
 39        String name = getParameter( "fontName" );
 40        if ( name == null ) name = "Serif";
 41        
 42        // Get the "bgColor" parameter from the HTML document:
 43        String bgColorStr = getParameter( "bgColor" );
 44        // Default background color is white:
 45        if ( bgColorStr == null ) bgColorStr = "#FFFFFF";
 46        // Convert hexadecimal color string to an integer:
 47        int bgColor = colorStringToInt( bgColorStr );
 48        
 49        // Instantiate a font object:
 50        Font f = new Font( name, Font.PLAIN, 18 );
 51        
 52        // Set the font:
 53        this.getGraphics().setFont( f );
 54        
 55        // Set the background color of the applet:
 56        this.setBackground( new Color( bgColor ) );
 57        
 58     }
 59     
 60     public void paint( Graphics g ) {
 61        
 62        // If there are no strings to draw, do nothing:
 63        if ( n == 0 ) return;
 64        
 65        // Get the FontMetrics object associated with this applet:
 66        FontMetrics fm = g.getFontMetrics();
 67        
 68        // Determine the width of the applet:
 69        int appletWidth = this.getSize().width;
 70        
 71        // Determine the width of the first string:
 72        int stringWidth = fm.stringWidth( s[0] );
 73        
 74        // Calculate the x-coordinate of the first string:
 75        int x = (appletWidth - stringWidth)/2;
 76        
 77        // Determine the height of the applet:
 78        int appletHeight = this.getSize().height;
 79        
 80        // Calculate the height of *any* string:
 81        int ascent = fm.getAscent();
 82        int descent = fm.getDescent();
 83        int stringHeight = ascent + descent;
 84        
 85        // Calculate the height of  n  strings:
 86        int leading = fm.getLeading();
 87        int height = n * stringHeight + (n - 1) * leading;
 88        
 89        // Calculate the y-coordinate of the first string:
 90        int y = (appletHeight - height)/2 + ascent;
 91        
 92        // Draw the first string at point (x,y):
 93        g.drawString( s[0], x, y );
 94        
 95        // Calculate the font height:
 96        int fontHeight = stringHeight + leading;
 97  
 98        for ( int i = 1; i < n; i++ ) {
 99           
100           // Calculate the width of this string:
101           stringWidth = fm.stringWidth( s[i] );
102           
103           // Calculate the x-coordinate of this string:
104           x = (appletWidth - stringWidth)/2;
105           
106           // Calculate the y-coordinate of this string:
107           y += fontHeight;
108                 
109           // Draw the current string at point (x,y):
110           g.drawString( s[i], x, y );
111           
112        }
113        
114     }
115     
116     private int colorStringToInt( String colorString ) {
117     
118        // Strip off whitespace from color string:
119        colorString = colorString.trim();
120        
121        // Strip off leading '#' from color string, if necessary:
122        if ( colorString.charAt(0) == '#' ) {
123          colorString = colorString.substring(1);
124        }
125        
126        // Convert hexadecimal color string to an integer:
127        return Integer.parseInt( colorString, 16 );
128        
129     }
130     
131  }