/* File: mString.java */ import java.awt.*; public class mString extends mPoint { // Instance variables: protected int w, h; protected String s; protected Font f; // Constructor: public mString( int new_x, int new_y, String new_s ) { // Invoke mPoint's constructor: super( new_x, new_y ); s = new_s; // A default font: setFont( new Font( "SansSerif", Font.PLAIN, 36 ) ); } // Accessors and mutators: public void setString( String new_s ) { s = new_s; } public void setFont( Font _f ) { f = _f; } // Implement mPoint.paint( Graphics ): public void paint( Graphics g ) { g.setColor( color ); g.setFont(f); FontMetrics fm = g.getFontMetrics(); w = fm.stringWidth(s); // width of string in current font h = fm.getHeight() - fm.getDescent(); g.drawString( s, x, y + h ); } // Implement mPoint.isInside( int, int ): public boolean isInside( int some_x, int some_y ) { Rectangle r = new Rectangle( x, y, w, h ); return r.contains( some_x, some_y ); } // Implement mPoint.checkBoundary( Rectangle ): public void checkBoundary( Rectangle r ) { int nx = x + dx, ny = y + dy; if ( (nx < r.x) || (nx + w > r.x + r.width) ) dx = -dx; if ( (ny < r.y) || (ny + h > r.y + r.height) ) dy = -dy; } }