1  /*
  2   *  File: DrawableString.java
  3   *
  4   *  A DrawableString class with centering capability
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   * 
  8   */
  9  
 10  import java.awt.*;
 11  
 12  public class DrawableString extends Font {
 13  
 14    private String string;
 15    private Color color;
 16    
 17    // Does it make sense to have a no-arg constructor?
 18    public DrawableString() {
 19      super( "SansSerif", Font.BOLD, 18 );
 20      this.string = "";
 21      this.color = Color.black;
 22    }
 23    
 24    public DrawableString( String string ) {
 25      this();
 26      this.string = string;
 27    }
 28    
 29    public String getString() { return this.string; }
 30    public void setString( String string ) {
 31      this.string  = string ;
 32    }
 33    
 34    public Color getColor() { return this.color; }
 35    public void setColor( Color color ) {
 36      this.color = color;
 37    }
 38    
 39    // Variables name, style, and size are inherited from Font:
 40    public void setFontName( String name ) {
 41      this.name = name;
 42    }
 43    public void setFontStyle( int style ) {
 44      this.style = style;
 45    }
 46    public void setFontSize( int size ) {
 47      this.size = size;
 48    }
 49    
 50    // These methods are for compatibility with DrawablePolygon:
 51    public void draw( Graphics g, int x, int y ) {
 52      g.drawString( this.string, x, y );
 53    }
 54    public void draw( Component c, int x, int y ) {
 55      this.draw( c.getGraphics(), x, y );
 56    }
 57    public void fill( Graphics g, int x, int y ) {
 58      this.draw( g, x, y );
 59    }
 60    public void fill( Component c, int x, int y ) {
 61      this.fill( c.getGraphics(), x, y );
 62    }
 63    
 64    // Center this string in the given component:
 65    public void centerDraw( Component c ) {
 66    
 67      if ( this.string == null ) return;
 68  
 69      // Should we restore the previous color?
 70      if ( this.color != null ) c.setForeground( color );
 71      
 72      // Get graphics object for this component:
 73      Graphics g = c.getGraphics();
 74      
 75      // Methods getName(), getStyle(), and getSize() are
 76      // inherited from Font:
 77      g.setFont( new Font( getName(), getStyle(), getSize() ) );
 78  
 79      // Get a FontMetrics object:
 80      FontMetrics fm = g.getFontMetrics();
 81      
 82      // Determine the width of the component:
 83      int componentWidth = c.getSize().width;
 84      
 85      // Determine the width of the string:
 86      int stringWidth = fm.stringWidth( this.string );
 87      
 88      // Calculate the x-coordinate of the string:
 89      int x = ( componentWidth - stringWidth )/2;
 90      
 91      // Determine the height of the component:
 92      int componentHeight = c.getSize().height;
 93      
 94      // Calculate the height of *any* string:
 95      int ascent = fm.getAscent();
 96      int descent = fm.getDescent();
 97      int stringHeight = ascent + descent;
 98      
 99      // Calculate the y-coordinate of the string:
100      int y = ( componentHeight - stringHeight )/2 + ascent;
101      
102      // Draw the string at point (x,y):
103      this.draw( g, x, y );
104      
105    }
106        
107  }