/* * File: DrawableStrings.java * * A DrawableStrings class with centering capability * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.*; public class DrawableStrings extends Font { // Use mutators to set private class variables: private int n = 0; // number of strings private String[] string; // strings to be drawn private Color color; // foreground color // This constructor takes an array of strings: public DrawableStrings( String[] string ) { super( "SansSerif", Font.BOLD, 18 ); this.color = Color.black; setString( string ); } // To do: write a constructor that takes a single string... // This mutator takes an array of strings: public void setString( String[] string ) { n = string.length; this.string = new String[n]; for ( int i = 0; i < n; i++ ) { this.string[i] = string[i]; } } // This mutator takes a single string: public void setString( String string ) { String[] s = { string }; setString( s ); } // Color accessor and mutator: public Color getColor() { return this.color; } public void setColor( Color color ) { if ( color != null ) this.color = color; } // Variables name, style, and size are inherited from Font: public void setFontName( String name ) { if ( name != null ) this.name = name; } public void setFontStyle( int style ) { this.style = style; } public void setFontSize( int size ) { this.size = size; } // Center the strings in the given component: public void centerDraw( Component c ) { // If there are no strings to draw, do nothing: if ( n == 0 ) return; // Save the current foreground color: if ( this.color != null ) { Color color = c.getForeground(); c.setForeground( this.color ); } // Get a graphics object for this component: Graphics g = c.getGraphics(); // Methods getName(), getStyle(), and getSize() are // inherited from Font: g.setFont( new Font( getName(), getStyle(), getSize() ) ); // Get a FontMetrics object: FontMetrics fm = g.getFontMetrics(); // Determine the width of the component: int componentWidth = c.getSize().width; // Determine the width of the first string: int stringWidth = fm.stringWidth( this.string[0] ); // Calculate the x-coordinate of the first string: int x = ( componentWidth - stringWidth )/2; // Determine the height of the component: int componentHeight = c.getSize().height; // Calculate the height of *any* string: int ascent = fm.getAscent(); int descent = fm.getDescent(); int stringHeight = ascent + descent; // Calculate the height of n strings: int leading = fm.getLeading(); int height = n * stringHeight + (n - 1) * leading; // Calculate the y-coordinate of the first string: int y = ( componentHeight - height )/2 + ascent; // Draw the first string at point (x,y): g.drawString( this.string[0], x, y ); // Calculate the font height: int fontHeight = stringHeight + leading; for ( int i = 1; i < n; i++ ) { // Calculate the width of this string: stringWidth = fm.stringWidth( this.string[i] ); // Calculate the x-coordinate of this string: x = ( componentWidth - stringWidth )/2; // Calculate the y-coordinate of this string: y += fontHeight; // Draw the current string at point (x,y): g.drawString( this.string[i], x, y ); } // Restore the original foreground color: c.setForeground( color ); } }