
import java.awt.*;

/** Beinhaltet einen Kreis */
public class TRectangle extends TShapeGraphic implements java.io.Serializable {

  /** Erzeugt einen Kreis 
  @param radius Der Radius
  @param x Horizontale Koordinate der linken, oberen Ecke
  @param y Vertikale Koordinate der linken, oberen Ecke
  @param filled TRUE wenn die Figur gefüllt werden soll
  */
  public TRectangle( int x, int y, int width, int height, boolean filled, TPaintSettings PaintSettings ) {
    // den vererbten Konstruktor aufrufen
    super( PaintSettings );

    // Die Felder setzen
    setWidth( PaintSettings().pix2doubleWidth(width) );
    setHeight( PaintSettings().pix2doubleHeight(height) );
    setLeft( PaintSettings().pix2doubleX (x) ); 
    setTop( PaintSettings().pix2doubleY (y) );
    this.filled = filled;
  }

  /** Zeichnet das Rechteck auf Graphics g neu 
    * @param Graphics Der zu verwendende Context
    */
  public void paint (Graphics g) {
     if (PaintSettings()==null) { return; }

     g.setColor(Pen().color);
     if (filled) {
        g.fillRect ( PaintSettings().double2pixX(getLeft()), PaintSettings().double2pixY(getTop()), PaintSettings().double2pixWidth(getWidth()), PaintSettings().double2pixHeight(getHeight()) );
     } else {
        g.drawRect ( PaintSettings().double2pixX(getLeft()), PaintSettings().double2pixY(getTop()), PaintSettings().double2pixWidth(getWidth()), PaintSettings().double2pixHeight(getHeight()) );
     }

     paintSelected (g);
  }

  /** Liefert TRUE wenn die übergebene Koordinate innerhalb des gezeichneten Bereiches liegt */
  public boolean isInside( int sx, int sy ) {
    double tolerance = PaintSettings().getToleranz();
    double x = PaintSettings().pix2doubleX(sx); 
    double y = PaintSettings().pix2doubleY(sy); 

    boolean result = false;

    // Die Werte sortieren
    double _x, _y, _width, _height;
    _x = getLeft();
    _width = getWidth();
    _y = getTop();
    _height = getHeight();
    if (_height < 0) { _height = -1 * _height; _y = _y - _height + 1; }
    if (_width < 0) { _width = -1 * _width; _x = _x - _width + 1; }

    result = isHigher(x,_x,tolerance) && isLower(x, _x+_width,tolerance) && isHigher(y,_y,tolerance) && isLower(y, _y+_height,tolerance);
  
    return result;
  }

  /** Gefüllt oder nicht gefüllt 
      @serial
   */
  public boolean filled;
}
