Example usage for java.awt.geom Line2D getBounds2D

List of usage examples for java.awt.geom Line2D getBounds2D

Introduction

In this page you can find the example usage for java.awt.geom Line2D getBounds2D.

Prototype

public Rectangle2D getBounds2D();

Source Link

Document

Returns a high precision and more accurate bounding box of the Shape than the getBounds method.

Usage

From source file:org.pentaho.reporting.engine.classic.core.modules.parser.simple.readhandlers.LineReadHandler.java

private void createLineElementFromBounds(final float x1, final float y1, final float x2, final float y2,
        final Stroke stroke, final String name, final Color c, final Rectangle2D bounds) {
    if (x1 == x2) {
        // assume that we have a vertical line
        final VerticalLineElementFactory elementFactory = new VerticalLineElementFactory();
        elementFactory.setName(name);// w  ww  .j  a va2s.c  o m
        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();
    }
}