/*******************************************************************************
* Copyright (c) 2006-2012
* Software Technology Group, Dresden University of Technology
* DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Software Technology Group - TU Dresden, Germany;
* DevBoost GmbH - Berlin, Germany
* - initial API and implementation
******************************************************************************/
/*
* @(#)RectangleFigure.java 2.3 2006-12-23
*
* Copyright (c) 1996-2006 by the original authors of JHotDraw
* and all its contributors.
* All rights reserved.
*
* The copyright of this software is owned by the authors and
* contributors of the JHotDraw project ("the copyright holders").
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* the copyright holders. For details see accompanying license terms.
*/
package org.jhotdraw.draw;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import static org.jhotdraw.draw.AttributeKeys.*;
import org.jhotdraw.geom.Geom;
/**
* RectangleFigure.
*
* @author Werner Randelshofer
* @version 2.3 2006-12-23 Made rectangle protected.
* <br>2.2 2006-03-23 Take stroke size into account in method contains.
* <br>2.1 2006-03-22 Method getFigureDrawBounds added.
* <br>2.0 2006-01-14 Changed to support double precison coordinates.
* <br>1.0 2003-12-01 Derived from JHotDraw 5.4b1.
*/
public class RectangleFigure extends AbstractAttributedFigure {
protected Rectangle2D.Double rectangle;
/** Creates a new instance. */
public RectangleFigure() {
this(0, 0, 0, 0);
}
public RectangleFigure(double x, double y, double width, double height) {
rectangle = new Rectangle2D.Double(x, y, width, height);
}
// DRAWING
protected void drawFill(Graphics2D g) {
Rectangle2D.Double r = (Rectangle2D.Double) rectangle.clone();
double grow = AttributeKeys.getPerpendicularFillGrowth(this);
Geom.grow(r, grow, grow);
g.fill(r);
}
protected void drawStroke(Graphics2D g) {
Rectangle2D.Double r = (Rectangle2D.Double) rectangle.clone();
double grow = AttributeKeys.getPerpendicularDrawGrowth(this);
Geom.grow(r, grow, grow);
g.draw(r);
}
// SHAPE AND BOUNDS
public Rectangle2D.Double getBounds() {
Rectangle2D.Double bounds = (Rectangle2D.Double) rectangle.clone();
return bounds;
}
@Override public Rectangle2D.Double getDrawingArea() {
Rectangle2D.Double r = (Rectangle2D.Double) rectangle.clone();
double grow = AttributeKeys.getPerpendicularHitGrowth(this) + 1d;
Geom.grow(r, grow, grow);
return r;
}
/**
* Checks if a Point2D.Double is inside the figure.
*/
public boolean contains(Point2D.Double p) {
Rectangle2D.Double r = (Rectangle2D.Double) rectangle.clone();
double grow = AttributeKeys.getPerpendicularHitGrowth(this) + 1d;
Geom.grow(r, grow, grow);
return r.contains(p);
}
public void setBounds(Point2D.Double anchor, Point2D.Double lead) {
rectangle.x = Math.min(anchor.x, lead.x);
rectangle.y = Math.min(anchor.y , lead.y);
rectangle.width = Math.max(0.1, Math.abs(lead.x - anchor.x));
rectangle.height = Math.max(0.1, Math.abs(lead.y - anchor.y));
}
/**
* Moves the Figure to a new location.
* @param tx the transformation matrix.
*/
public void transform(AffineTransform tx) {
Point2D.Double anchor = getStartPoint();
Point2D.Double lead = getEndPoint();
setBounds(
(Point2D.Double) tx.transform(anchor, anchor),
(Point2D.Double) tx.transform(lead, lead)
);
}
public void restoreTransformTo(Object geometry) {
Rectangle2D.Double r = (Rectangle2D.Double) geometry;
rectangle.x = r.x;
rectangle.y = r.y;
rectangle.width = r.width;
rectangle.height = r.height;
}
public Object getTransformRestoreData() {
return rectangle.clone();
}
// ATTRIBUTES
// EDITING
// CONNECTING
// COMPOSITE FIGURES
// CLONING
public RectangleFigure clone() {
RectangleFigure that = (RectangleFigure) super.clone();
that.rectangle = (Rectangle2D.Double) this.rectangle.clone();
return that;
}
// EVENT HANDLING
}