{
height = Math.max(y2, y1) - Math.min(y2, y1);
}
// create the bounds as specified by the user
final Rectangle2D bounds =
new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), width, height);
if (x1 == x2)
{
// assume that we have a vertical line
final VerticalLineElementFactory elementFactory = new VerticalLineElementFactory();
elementFactory.setName(name);
elementFactory.setColor(c);
elementFactory.setStroke(stroke);
elementFactory.setX(new Float(bounds.getX()));
elementFactory.setY(new Float(bounds.getY()));
elementFactory.setMinimumWidth(new Float(bounds.getWidth()));
elementFactory.setMinimumHeight(new Float(bounds.getHeight()));
elementFactory.setScale(Boolean.TRUE);
elementFactory.setKeepAspectRatio(Boolean.FALSE);
elementFactory.setShouldDraw(Boolean.TRUE);
element = elementFactory.createElement();
}
else if (y1 == y2)
{
// assume that we have a horizontal line
final HorizontalLineElementFactory elementFactory = new HorizontalLineElementFactory();
elementFactory.setName(name);
elementFactory.setColor(c);
elementFactory.setStroke(stroke);
elementFactory.setX(new Float(bounds.getX()));
elementFactory.setY(new Float(bounds.getY()));
elementFactory.setMinimumWidth(new Float(bounds.getWidth()));
elementFactory.setMinimumHeight(new Float(bounds.getHeight()));
elementFactory.setScale(Boolean.TRUE);
elementFactory.setKeepAspectRatio(Boolean.FALSE);
elementFactory.setShouldDraw(Boolean.TRUE);
element = elementFactory.createElement();
}
else
{
// here comes the magic - we transform the line into the absolute space;
// this should preserve the general appearance. Heck, and if not, then
// it is part of the users reponsibility to resolve that. Magic does not
// solve all problems, you know.
final Line2D line = new Line2D.Float
(Math.abs(x1), Math.abs(y1), Math.abs(x2), Math.abs(y2));
final Rectangle2D shapeBounds = line.getBounds2D();
final Shape transformedShape =
ShapeTransform.translateShape(line, -shapeBounds.getX(), -shapeBounds.getY());
// and use that shape with the user's bounds to create the element.
final ContentElementFactory elementFactory = new ContentElementFactory();
elementFactory.setName(name);
elementFactory.setColor(c);
elementFactory.setStroke(stroke);
elementFactory.setX(new Float(shapeBounds.getX()));
elementFactory.setY(new Float(shapeBounds.getY()));
elementFactory.setMinimumWidth(new Float(shapeBounds.getWidth()));
elementFactory.setMinimumHeight(new Float(shapeBounds.getHeight()));
elementFactory.setContent(transformedShape);
elementFactory.setScale(Boolean.TRUE);
elementFactory.setKeepAspectRatio(Boolean.FALSE);
elementFactory.setShouldDraw(Boolean.TRUE);
element = elementFactory.createElement();