
import java.awt.*;
import java.io.*;
import java.util.*;

class Text extends Figure {
    String _text;
    static Font _font;
    int _width;
    int _height;

    Text( String color, int x0, int y0, String text, BoilerDraw bd ) {
	super( color, x0, y0 );

	_width = 0;
	_height = 0;
	_text = text;

	// Set font only once
	if ( _font == null ) {
	    _font = new Font( "SansSerif", Font.BOLD, 16 );
	}

	FontMetrics fm = bd.getGraphics().getFontMetrics( _font );

	_width = fm.stringWidth( _text );
	_height = fm.getHeight();
    }

    public void resize( int x0, int y0, int x1, int y1 ) {
	_x0 = x0;
	_y0 = y0;
    }

    public void paint( Graphics g ) {

	g.setColor( getColor() );
	g.setFont( _font );
	g.drawString( _text, _x0, _y0 );
    }

    public String toString() {
	String s = "Text|";
	s += super.toString();
	s += _text + "\n";
	return s;
    }

    Text( StringTokenizer t, BoilerDraw bd ) {
	super( t );
	_text = t.nextToken();

	// Set font only once
	if ( _font == null ) {
	    _font = new Font( "SansSerif", Font.BOLD, 16 );
	}

	FontMetrics fm = bd.getGraphics().getFontMetrics( _font );

	_width = fm.stringWidth( _text );
	_height = fm.getHeight();
    }

    // Return true if figure is selected using these coordinates
    public boolean isSelected( int x, int y ) {
	if ( x >= _x0 && x < _x0 + _width && 
	     y <= _y0 && y >= _y0 - _height ) {
	    return true;
	}
	else {
	    return false;
	}
    }

    public void paintSelected( Graphics g ) {
	g.setColor( Color.blue.brighter() );
	g.drawRect( _x0-2, _y0-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0-_height-2, 4, 4 );
	g.drawRect( _x0+_width-2, _y0-2, 4, 4 );
	g.drawRect( _x0-2, _y0-_height-2, 4, 4 );
    }

    public boolean isInBox( int x0, int y0, int width, int height ) {
	if ( x0 <= getMinX() && x0 + width >= getMaxX() && 
	     y0 <= getMinY() && y0 + height >= getMaxY() ) {
	    return true;
	}
	else {
	    return false;
	}
    }

    public int getMinX() {
	return _x0;
    }

    public int getMaxX() {
	return _x0 + _width;
    }

    public int getMinY() {
	return _y0 - _height;
    }

    public int getMaxY() {
	return _y0;
    }
}


