1  /* File:  mString.java */
  2  
  3  import java.awt.*;
  4  
  5  public class mString extends mPoint 
  6  {
  7    // Instance variables:
  8    protected int w, h;
  9    protected String s;
 10    protected Font f;
 11    
 12    // Constructor:
 13    public mString( int new_x, int new_y, String new_s ) 
 14    {
 15      // Invoke mPoint's constructor:
 16      super( new_x, new_y );
 17      s = new_s;
 18      // A default font:
 19      setFont( new Font( "SansSerif", Font.PLAIN, 36 ) );
 20    }
 21    
 22    // Accessors and mutators:
 23    public void setString( String new_s ) 
 24    {
 25      s = new_s;
 26    }
 27    public void setFont( Font _f ) 
 28    {
 29      f = _f;
 30    }
 31    
 32    // Implement mPoint.paint( Graphics ):
 33    public void paint( Graphics g ) 
 34    {
 35      g.setColor( color );
 36      g.setFont(f);
 37      FontMetrics fm = g.getFontMetrics();
 38      w = fm.stringWidth(s);  // width of string in current font
 39      h = fm.getHeight() - fm.getDescent();
 40      g.drawString( s, x, y + h );
 41    }
 42    
 43    // Implement mPoint.isInside( int, int ):
 44    public boolean isInside( int some_x, int some_y ) 
 45    {
 46      Rectangle r = new Rectangle( x, y, w, h );
 47      return r.contains( some_x, some_y );
 48    }
 49  
 50    // Implement mPoint.checkBoundary( Rectangle ):
 51    public void checkBoundary( Rectangle r ) 
 52    {
 53      int nx = x + dx, ny = y + dy;
 54      if ( (nx < r.x) || (nx + w > r.x + r.width) ) dx = -dx;
 55      if ( (ny < r.y) || (ny + h > r.y + r.height) ) dy = -dy;
 56    }
 57    
 58  }