Example usage for java.awt.geom Rectangle2D getWidth

List of usage examples for java.awt.geom Rectangle2D getWidth

Introduction

In this page you can find the example usage for java.awt.geom Rectangle2D getWidth.

Prototype

public abstract double getWidth();

Source Link

Document

Returns the width of the framing rectangle in double precision.

Usage

From source file:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *//*from www  .  java  2  s .  co m*/
public static void drawMessage(Graphics2D g2, String message, Rectangle2D box) {
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
    float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
    float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);

    g2.drawString(message, x, y);
}

From source file:ShapeTransform.java

/**
 * Translates the given shape. The shape is translated to the origin supplied
 * in <code>point</code>. If scaling is requested, the shape will also be
 * scaled using an AffineTransform.//  ww  w  .  j a  v a  2  s. c  om
 * 
 * @param s
 *          the shape that should be transformed
 * @param scale
 *          true, if the shape should be scaled, false otherwise
 * @param keepAR
 *          true, if the scaled shape should keep the aspect ratio
 * @param width
 *          the target width.
 * @param height
 *          the target height.
 * @return the transformed shape
 */
public static Shape transformShape(final Shape s, final boolean scale, final boolean keepAR, final double width,
        final double height) {
    /**
     * Always scale to the maximum bounds ...
     */
    if (scale) {

        final Rectangle2D boundsShape = s.getBounds2D();
        final double w = boundsShape.getWidth();
        final double h = boundsShape.getHeight();
        double scaleX = 1;

        if (w != 0) {
            scaleX = width / w;
        }

        double scaleY = 1;
        if (h != 0) {
            scaleY = height / h;
        }

        if (scaleX != 1 || scaleY != 1) {
            if (s instanceof RectangularShape) {
                return ShapeTransform.resizeRect((RectangularShape) s, w * scaleX, h * scaleY);
            }
            if (s instanceof Line2D) {
                return ShapeTransform.resizeLine((Line2D) s, w * scaleX, h * scaleY);
            }

            if (keepAR) {
                final double scaleFact = Math.min(scaleX, scaleY);
                return performDefaultTransformation(s, scaleFact, scaleFact);
            } else {
                return performDefaultTransformation(s, scaleX, scaleY);
            }
        }
    }
    return s;
}

From source file:com.t_oster.visicut.misc.Helper.java

/**
 * Returns an AffineTransform, which transformes src to dest
 * and constists of a scale and translate component
 * @param src//from   w w  w . j  a  v  a2 s  .  c  o  m
 * @param dest
 * @return
 */
public static AffineTransform getTransform(Rectangle2D src, Rectangle2D dest) {
    AffineTransform scale = AffineTransform.getScaleInstance(dest.getWidth() / src.getWidth(),
            dest.getHeight() / src.getHeight());
    Point2D scaled = scale.transform(new Point.Double(src.getX(), src.getY()), null);
    AffineTransform result = AffineTransform.getTranslateInstance(dest.getX() - scaled.getX(),
            dest.getY() - scaled.getY());
    result.concatenate(scale);
    return result;
}

From source file:de.bund.bfr.knime.gis.views.canvas.CanvasUtils.java

public static Transform getTransformForBounds(Dimension canvasSize, Rectangle2D bounds, Double zoomStep) {
    double widthRatio = canvasSize.width / bounds.getWidth();
    double heightRatio = canvasSize.height / bounds.getHeight();
    double canvasCenterX = canvasSize.width / 2.0;
    double canvasCenterY = canvasSize.height / 2.0;
    double centerX = bounds.getCenterX();
    double centerY = bounds.getCenterY();

    double scale = Math.min(widthRatio, heightRatio);

    if (zoomStep != null) {
        int zoom = (int) (Math.log(scale) / Math.log(2.0));

        scale = Math.pow(2.0, zoom);
    }/*from  w  w  w.ja va  2s. c  o  m*/

    double scaleX = scale;
    double scaleY = scale;
    double translationX = canvasCenterX - centerX * scaleX;
    double translationY = canvasCenterY - centerY * scaleY;

    return new Transform(scaleX, scaleY, translationX, translationY);
}

From source file:org.jfree.experimental.chart.plot.dial.DialPlot.java

/**
 * A utility method that computes a rectangle using relative radius values.
 * // w  w  w . j  a v  a  2  s  .  c o  m
 * @param rect  the reference rectangle.
 * @param radiusW  the width radius (must be > 0.0)
 * @param radiusH  the height radius.
 * 
 * @return A new rectangle.
 */
public static Rectangle2D rectangleByRadius(Rectangle2D rect, double radiusW, double radiusH) {
    double x = rect.getCenterX();
    double y = rect.getCenterY();
    double w = rect.getWidth() * radiusW;
    double h = rect.getHeight() * radiusH;
    return new Rectangle2D.Double(x - w / 2.0, y - h / 2.0, w, h);
}

From source file:net.sf.jasperreports.charts.util.ChartUtil.java

/**
 * //from w w  w .  j  av  a  2  s  .c  o  m
 */
public static List<JRPrintImageAreaHyperlink> getImageAreaHyperlinks(JFreeChart chart,
        ChartHyperlinkProvider chartHyperlinkProvider, Graphics2D grx, Rectangle2D renderingArea)// throws JRException
{
    List<JRPrintImageAreaHyperlink> areaHyperlinks = null;

    if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) {
        ChartRenderingInfo renderingInfo = new ChartRenderingInfo();

        if (grx == null) {
            chart.createBufferedImage((int) renderingArea.getWidth(), (int) renderingArea.getHeight(),
                    renderingInfo);
        } else {
            chart.draw(grx, renderingArea, renderingInfo);
        }

        EntityCollection entityCollection = renderingInfo.getEntityCollection();
        if (entityCollection != null && entityCollection.getEntityCount() > 0) {
            areaHyperlinks = new ArrayList<JRPrintImageAreaHyperlink>(entityCollection.getEntityCount());

            for (@SuppressWarnings("unchecked")
            Iterator<ChartEntity> it = entityCollection.iterator(); it.hasNext();) {
                ChartEntity entity = it.next();
                JRPrintHyperlink printHyperlink = chartHyperlinkProvider.getEntityHyperlink(entity);
                if (printHyperlink != null) {
                    JRPrintImageArea area = getImageArea(entity);

                    JRPrintImageAreaHyperlink areaHyperlink = new JRPrintImageAreaHyperlink();
                    areaHyperlink.setArea(area);
                    areaHyperlink.setHyperlink(printHyperlink);
                    areaHyperlinks.add(areaHyperlink);
                }
            }
        }
    }

    return areaHyperlinks;
}

From source file:ec.util.chart.swing.JTimeSeriesRendererSupport.java

private static Shape createShape(double x, double y, Rectangle2D hotspot) {
    Area result = new Area(new RoundRectangle2D.Double(hotspot.getX(), hotspot.getY(), hotspot.getWidth(),
            hotspot.getHeight(), 8, 8));

    boolean right = hotspot.getMinX() > x;

    Polygon po = new Polygon();
    po.addPoint(0, 0);//from w  w  w. jav a2 s . c  o  m
    po.addPoint(0, 10);
    po.addPoint(10, 0);
    AffineTransform af = new AffineTransform();
    if (right) {
        af.translate(hotspot.getX() - 7, hotspot.getY() + hotspot.getHeight() / 2);
        af.rotate(-Math.PI / 4);
    } else {
        af.translate(hotspot.getMaxX() + 7, hotspot.getY() + hotspot.getHeight() / 2);
        af.rotate(Math.PI * 3 / 4);
    }

    Shape shape = af.createTransformedShape(po);
    result.add(new Area(shape));
    return result;
}

From source file:com.t_oster.visicut.misc.Helper.java

/**
 * Returns the distance between two Rectangles
 * @param r first rectangle/*from  w w  w.j a  v a 2s  .com*/
 * @param q second rectangle
 * @return Distance between two rectangles
 */
public static double distance(Rectangle2D r, Rectangle2D q) {
    double qx0 = q.getX();
    double qy0 = q.getY();
    double qx1 = q.getX() + q.getWidth();
    double qy1 = q.getY() + q.getHeight();
    double rx0 = r.getX();
    double ry0 = r.getY();
    double rx1 = r.getX() + r.getWidth();
    double ry1 = r.getY() + r.getHeight();
    //Check for Overlap
    if (qx0 <= rx1 && qy0 <= ry1 && rx0 <= qx1 && ry0 <= qy1) {
        return 0;
    }
    double d = 0;
    if (rx0 > qx1) {
        d += (rx0 - qx1) * (rx0 - qx1);
    } else if (qx0 > rx1) {
        d += (qx0 - rx1) * (qx0 - rx1);
    }
    if (ry0 > qy1) {
        d += (ry0 - qy1) * (ry0 - qy1);
    } else if (qy0 > ry1) {
        d += (qy0 - ry1) * (qy0 - ry1);
    }
    return Math.sqrt(d);
}

From source file:com.t_oster.visicut.misc.Helper.java

public static Rectangle toRect(Rectangle2D src) {
    if (src == null) {
        return new Rectangle(0, 0, 0, 0);
    }// w ww  .  ja  v  a2  s .  co m
    return new Rectangle((int) src.getX(), (int) src.getY(), (int) src.getWidth(), (int) src.getHeight());
}

From source file:org.logisticPlanning.utils.graphics.chart.impl.jfree._JFCLineChart2D.java

/**
 * paint a legend//  w  w  w. j a v a2 s  . com
 *
 * @param legend
 *          the legend
 * @param graphics
 *          the graphics
 * @param bounds
 *          the bounds
 */
private static final void __paintLegend(final LegendTitle legend, final Graphics2D graphics,
        final Rectangle2D bounds) {
    final Rectangle2D titleArea;
    final BlockParams p;
    final double ww, hh;
    final RectangleConstraint constraint;
    final Size2D size;

    p = new BlockParams();
    p.setGenerateEntities(false);

    ww = bounds.getWidth();
    if (ww <= 0.0d) {
        return;
    }
    hh = bounds.getHeight();
    if (hh <= 0.0d) {
        return;
    }

    constraint = new RectangleConstraint(ww, new Range(0.0, ww), LengthConstraintType.RANGE, hh,
            new Range(0.0, hh), LengthConstraintType.RANGE);

    size = legend.arrange(graphics, constraint);
    titleArea = _JFCLineChart2D.__createAlignedRectangle2D(size, bounds, legend.getHorizontalAlignment(),
            VerticalAlignment.TOP);
    legend.setMargin(_JFCLineChart2D.LEGEND_MARGIN);
    legend.setPadding(_JFCLineChart2D.CHART_INSETS);
    legend.draw(graphics, titleArea, p);
}