Example usage for java.awt.geom Rectangle2D getMaxX

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

Introduction

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

Prototype

public double getMaxX() 

Source Link

Document

Returns the largest X coordinate of the framing rectangle of the Shape in double precision.

Usage

From source file:Main.java

/**
 * Returns a point based on (x, y) but constrained to be within the bounds
 * of a given rectangle.//from w w  w.  j a  v  a 2  s. com
 *
 * @param x  the x-coordinate.
 * @param y  the y-coordinate.
 * @param area  the constraining rectangle (<code>null</code> not
 *              permitted).
 *
 * @return A point within the rectangle.
 *
 * @throws NullPointerException if <code>area</code> is <code>null</code>.
 */
public static Point2D getPointInRectangle(double x, double y, final Rectangle2D area) {

    x = Math.max(area.getMinX(), Math.min(x, area.getMaxX()));
    y = Math.max(area.getMinY(), Math.min(y, area.getMaxY()));
    return new Point2D.Double(x, y);

}

From source file:Main.java

public static Rectangle2D.Double fromSheetRect(Rectangle2D r, AffineTransform at) {
    Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
    Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
    Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
    Point2D.Double pScreen = new Point2D.Double();
    Point2D.Double pScreenX = new Point2D.Double();
    Point2D.Double pScreenY = new Point2D.Double();

    try {//from  ww  w  .  j a v a 2  s.c o  m
        at.transform(pSheet, pScreen);
        at.transform(pSX, pScreenX);
        at.transform(pSY, pScreenY);
    } catch (Exception e) {
        System.err.print(e.getMessage());
    }

    Rectangle2D.Double res = new Rectangle2D.Double();
    res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX), pScreen.distance(pScreenY));

    return res;
}

From source file:Main.java

public static Rectangle2D.Double toSheetRect(Rectangle2D r, AffineTransform at) {
    Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(), r.getY()), at);
    Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(), r.getMinY()), at);
    Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(), r.getMaxY()), at);
    Rectangle2D.Double res = new Rectangle2D.Double();
    res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX), pSheet.distance(pSheetY));

    return res;/*from  w  w w.j a v  a  2 s  . co  m*/
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.legend.SmartLegendTitle.java

public static Paint transformLinearGradient(LinearGradientPaint paint, Shape target) {
    Rectangle2D bounds = target.getBounds2D();
    float left = (float) bounds.getMinX();
    float right = (float) bounds.getMaxX();
    LinearGradientPaint newPaint = new LinearGradientPaint(left, 0, right, 0, paint.getFractions(),
            paint.getColors());//from   ww  w  .  j  a  v  a  2 s.co m
    return newPaint;
}

From source file:GraphicsUtil.java

public static void drawImage(Graphics g, BufferedImage img, Rectangle2D bounds, ImageObserver observer) {
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(img, // what to draw
            (int) bounds.getMinX(), // dest left
            (int) bounds.getMinY(), // dest top
            (int) bounds.getMaxX(), // dest right
            (int) bounds.getMaxY(), // dest bottom
            0, // src left
            0, // src top
            img.getWidth(), // src right
            img.getHeight(), // src bottom
            observer // to notify of image updates
    );//ww w . j a v a  2s .  c  om
}

From source file:de.bund.bfr.knime.gis.GisUtils.java

public static Polygon createBorderPolygon(Rectangle2D rect, double d) {
    Coordinate[] outerRing = new Coordinate[] { new Coordinate(rect.getMinX() - d, rect.getMinY() - d),
            new Coordinate(rect.getMaxX() + d, rect.getMinY() - d),
            new Coordinate(rect.getMaxX() + d, rect.getMaxY() + d),
            new Coordinate(rect.getMinX() - d, rect.getMaxY() + d),
            new Coordinate(rect.getMinX() - d, rect.getMinY() - d) };
    Coordinate[] innerRing = new Coordinate[] { new Coordinate(rect.getMinX(), rect.getMinY()),
            new Coordinate(rect.getMaxX(), rect.getMinY()), new Coordinate(rect.getMaxX(), rect.getMaxY()),
            new Coordinate(rect.getMinX(), rect.getMaxY()), new Coordinate(rect.getMinX(), rect.getMinY()) };

    return FACTORY.createPolygon(FACTORY.createLinearRing(outerRing),
            new LinearRing[] { FACTORY.createLinearRing(innerRing) });
}

From source file:LineUtilities.java

/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 *//* ww  w  .j  a  v a2 s.c o  m*/
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            } else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            } else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            } else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        } else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            } else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            } else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            } else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true; // the line is visible - if it wasn't, we'd have
                 // returned false from within the while loop above

}

From source file:RectUtils.java

/**
 * Unions the pair of source <code>Rectangle2D</code> objects and puts the
 * result into the returned <code>Rectangle2D</code> object. This method
 * extends the Rectangle2D version by checking for null parameters, the
 * returned value will also be <code>null</code> if the two input
 * rectangles are <code>null</code>
 * //www.jav a  2  s . c o m
 * @param src1
 *            the first of a pair of <code>Rectangle2D</code> objects to
 *            be combined with each other
 * @param src2
 *            the second of a pair of <code>Rectangle2D</code> objects to
 *            be combined with each other
 * 
 */
public static Rectangle2D union(Rectangle2D src1, Rectangle2D src2) {
    Rectangle2D result = null;
    if (src1 == null && src2 == null) {
        result = null;
    } else if (src1 != null && src2 != null) {
        double x1 = Math.min(src1.getMinX(), src2.getMinX());
        double y1 = Math.min(src1.getMinY(), src2.getMinY());
        double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
        double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    } else if (src1 != null) {
        double x1 = src1.getMinX();
        double y1 = src1.getMinY();
        double x2 = src1.getMaxX();
        double y2 = src1.getMaxY();
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    } else {
        // only src2 is non-null
        double x1 = src2.getMinX();
        double y1 = src2.getMinY();
        double x2 = src2.getMaxX();
        double y2 = src2.getMaxY();
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    }
    return result;
}

From source file:org.jax.maanova.plot.PlotUtil.java

/**
 * Rescales the XY plot to match the viewing area
 * @param viewArea/*from  w  w  w .  ja  va 2  s .  c  o m*/
 *          the viewing area (null means we should use autorange)
 * @param plot
 *          the plot to rescale
 */
public static void rescaleXYPlot(final Rectangle2D viewArea, final XYPlot plot) {
    if (viewArea == null) {
        plot.getDomainAxis().setAutoRange(true);
        plot.getRangeAxis().setAutoRange(true);
    } else {
        plot.getDomainAxis().setRange(new Range(viewArea.getMinX(), viewArea.getMaxX()), true, false);
        plot.getRangeAxis().setRange(new Range(viewArea.getMinY(), viewArea.getMaxY()), true, true);
    }
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

public static Abstract2DMask translateChartRectangle(Abstract2DMask mask, Rectangle2D imageArea,
        JFreeChart chart) {// ww w  . jav  a  2  s . c  om
    Rectangle2D bound = mask.getRectangleFrame();
    Point2D start = new Point2D.Double(bound.getMinX(), bound.getMinY());
    Point2D end = new Point2D.Double(bound.getMaxX(), bound.getMaxY());
    Point2D screenStart = translateChartPoint(start, imageArea, chart);
    Point2D screenEnd = translateChartPoint(end, imageArea, chart);
    Abstract2DMask imageMask = mask.clone();
    imageMask.setRectangleFrame(new Rectangle2D.Double(Math.min(screenStart.getX(), screenEnd.getX()),
            Math.min(screenStart.getY(), screenEnd.getY()), Math.abs(screenStart.getX() - screenEnd.getX()),
            Math.abs(screenStart.getY() - screenEnd.getY())));
    return imageMask;
}