import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Image; import java.awt.image.ImageObserver; import java.awt.Rectangle; /* * This class implements a TurtleGraphics LABEL graphics operation. ** @author Guy Haas */ class TGLabelOp implements ImageObserver, TGGraphicsOp { private Color color; private Font font; private String text; private TGPoint where; // // constructor // public TGLabelOp( String label, TGPoint where, Font font, Color color ) { this.color = color; this.font = font; this.text = label; this.where = where; } public Rectangle doIt( Image inMemoryImage ) { int canvasWidth = inMemoryImage.getWidth( this ); if ( canvasWidth < 0 ) return null; int canvasX = where.canvasX( canvasWidth ); int canvasHeight = inMemoryImage.getHeight( this ); if ( canvasHeight < 0 ) return null; int canvasY = where.canvasY( canvasHeight ); Graphics g = inMemoryImage.getGraphics(); g.setColor( color ); g.setFont( font ); FontMetrics fm = g.getFontMetrics(); int crX = canvasX; int crWidth = fm.stringWidth( text ); int crHeight = fm.getHeight(); int crY = canvasY - (crHeight - fm.getDescent()); g.setClip( crX, crY, crWidth, crHeight ); g.drawString( text, canvasX, canvasY ); g.dispose(); Rectangle clipRect = new Rectangle( crX, crY, crWidth, crHeight ); return clipRect; } // end doIt() public Color getColor() { return color; } public boolean imageUpdate(Image img, int flags, int x, int y, int wd, int ht) { return true; } } // end class TGLabelOp