Example usage for java.awt.geom Rectangle2D getX

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

Introduction

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

Prototype

public abstract double getX();

Source Link

Document

Returns the X coordinate of the upper-left corner of the framing rectangle in double precision.

Usage

From source file:Main.java

public static Rectangle calculateBoundsForComponent(JComponent comp, String text, int xOffset, int yOffset) {

    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    Rectangle2D bounds = fm.getStringBounds(text, comp.getGraphics());

    return new Rectangle(xOffset + (int) bounds.getX(), yOffset,
            (int) Math.ceil(bounds.getWidth()) + (PADDING * 2),
            (int) Math.ceil(bounds.getHeight() + fm.getDescent()) + (PADDING * 2));
}

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 {/* w  w w . j  a va2  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;//  ww w  .  j a v  a 2s.  c o m
}

From source file:Main.java

/**
 * Checks, whether the given rectangle1 fully contains rectangle 2
 * (even if rectangle 2 has a height or width of zero!).
 *
 * @param rect1  the first rectangle.//from   w ww . ja  v  a 2s.  c o m
 * @param rect2  the second rectangle.
 *
 * @return A boolean.
 */
public static boolean contains(final Rectangle2D rect1, final Rectangle2D rect2) {

    final double x0 = rect1.getX();
    final double y0 = rect1.getY();
    final double x = rect2.getX();
    final double y = rect2.getY();
    final double w = rect2.getWidth();
    final double h = rect2.getHeight();

    return ((x >= x0) && (y >= y0) && ((x + w) <= (x0 + rect1.getWidth()))
            && ((y + h) <= (y0 + rect1.getHeight())));

}

From source file:Utils.java

public static Shape generatePolygon(int sides, int outsideRadius, int insideRadius, boolean normalize) {
    Shape shape = generatePolygon(sides, outsideRadius, insideRadius);
    if (normalize) {
        Rectangle2D bounds = shape.getBounds2D();
        GeneralPath path = new GeneralPath(shape);
        shape = path//w  w w.  j  av a 2s. c om
                .createTransformedShape(AffineTransform.getTranslateInstance(-bounds.getX(), -bounds.getY()));
    }
    return shape;
}

From source file:Main.java

/**
 * Serialises a <code>Shape</code> object.
 *
 * @param shape  the shape object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*  w w  w.  j  av  a 2s.  c  o m*/
public static void writeShape(final Shape shape, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            final Line2D line = (Line2D) shape;
            stream.writeObject(Line2D.class);
            stream.writeDouble(line.getX1());
            stream.writeDouble(line.getY1());
            stream.writeDouble(line.getX2());
            stream.writeDouble(line.getY2());
        } else if (shape instanceof Rectangle2D) {
            final Rectangle2D rectangle = (Rectangle2D) shape;
            stream.writeObject(Rectangle2D.class);
            stream.writeDouble(rectangle.getX());
            stream.writeDouble(rectangle.getY());
            stream.writeDouble(rectangle.getWidth());
            stream.writeDouble(rectangle.getHeight());
        } else if (shape instanceof Ellipse2D) {
            final Ellipse2D ellipse = (Ellipse2D) shape;
            stream.writeObject(Ellipse2D.class);
            stream.writeDouble(ellipse.getX());
            stream.writeDouble(ellipse.getY());
            stream.writeDouble(ellipse.getWidth());
            stream.writeDouble(ellipse.getHeight());
        } else if (shape instanceof Arc2D) {
            final Arc2D arc = (Arc2D) shape;
            stream.writeObject(Arc2D.class);
            stream.writeDouble(arc.getX());
            stream.writeDouble(arc.getY());
            stream.writeDouble(arc.getWidth());
            stream.writeDouble(arc.getHeight());
            stream.writeDouble(arc.getAngleStart());
            stream.writeDouble(arc.getAngleExtent());
            stream.writeInt(arc.getArcType());
        } else if (shape instanceof GeneralPath) {
            stream.writeObject(GeneralPath.class);
            final PathIterator pi = shape.getPathIterator(null);
            final float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                final int type = pi.currentSegment(args);
                stream.writeInt(type);
                // TODO: could write this to only stream the values
                // required for the segment type
                for (int i = 0; i < 6; i++) {
                    stream.writeFloat(args[i]);
                }
                stream.writeInt(pi.getWindingRule());
                pi.next();
                stream.writeBoolean(pi.isDone());
            }
        } else {
            stream.writeObject(shape.getClass());
            stream.writeObject(shape);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:org.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *///from   w w  w  .  j  a va2 s  .  c o  m
public static void drawCentred(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:org.jax.maanova.plot.MaanovaChartPanel.java

/**
 * Convert a rectangle with negative width or height to one with
 * positive width and height/* w ww.  j a v  a2 s .  co  m*/
 * @param rectangle
 *          the rectangle that might have negatives
 * @return
 *          the positive version, or the same instance that was passed
 *          in if it's already positive
 */
protected static Rectangle2D toNonNegativeWidthHeightRectangle(Rectangle2D rectangle) {
    if (rectangle.getWidth() < 0 || rectangle.getHeight() < 0) {
        final double x;
        final double y;
        final double width;
        final double height;

        if (rectangle.getWidth() < 0) {
            width = -rectangle.getWidth();
            x = rectangle.getX() + rectangle.getWidth();
        } else {
            width = rectangle.getWidth();
            x = rectangle.getX();
        }

        if (rectangle.getHeight() < 0) {
            height = -rectangle.getHeight();
            y = rectangle.getY() + rectangle.getHeight();
        } else {
            height = rectangle.getHeight();
            y = rectangle.getY();
        }

        return new Rectangle2D.Double(x, y, width, height);
    } else {
        // the rectangle that we have is OK
        return rectangle;
    }
}

From source file:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *//* w  ww .  j  ava  2  s.  c o 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:com.t_oster.visicut.misc.Helper.java

/**
 * Returns the distance between two Rectangles
 * @param r first rectangle/* www .  ja  va2s . c  o m*/
 * @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);
}