Example usage for java.awt.geom Rectangle2D getHeight

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

Introduction

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

Prototype

public abstract double getHeight();

Source Link

Document

Returns the height of the framing rectangle in double precision.

Usage

From source file:SWTUtils.java

/**
 * Transform an awt Rectangle2d instance into a swt one.
 * The coordinates are rounded to integer for the swt object.
 * @param rect2d The awt rectangle to map.
 * @return an swt <code>Rectangle</code> object.
 *//*from   ww w  .  j  a va2s . c  o m*/
public static Rectangle toSwtRectangle(Rectangle2D rect2d) {
    return new Rectangle((int) Math.round(rect2d.getMinX()), (int) Math.round(rect2d.getMinY()),
            (int) Math.round(rect2d.getWidth()), (int) Math.round(rect2d.getHeight()));
}

From source file:CheckFonts.java

/**
 * ?  ??  dialog base unit'.// w  w w. j  a v a  2 s.  c o  m
 * 
 * ? :   - ?  ????  ,   - ??  ????
 *  .
 */
protected static Dimension getDimension(ResourceDialog dialog, String text) throws Exception {
    int fontPixelsSize = (int) Math.round(dialog.pointsize * 96.0 / 72);

    Font f = getDialogFont(dialog).deriveFont(dialog.italic != 0 ? Font.ITALIC : 0, fontPixelsSize);

    // BufferedImage img = new BufferedImage(1024, 512, BufferedImage.TYPE_INT_RGB);
    // Graphics gr = img.getGraphics();
    // FontMetrics fm = gr.getFontMetrics(f);
    // r = fm.stringWidth(text);

    FontRenderContext frc = new FontRenderContext(null, false, false);
    Rectangle2D strDialogUnits = f.getStringBounds("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", frc);
    // ? ???   - http://support.microsoft.com/kb/125681
    double fullWidth = strDialogUnits.getWidth();
    double fullHeight = strDialogUnits.getHeight();
    long avgWidth = Math.round(Math.ceil(fullWidth) / 26 + 1) / 2;
    long avgHeight = Math.round(Math.ceil(fullHeight));
    double dbuX = avgWidth / 4.0;
    double dbuY = avgHeight / 8.0;

    Rectangle2D strRect = f.getStringBounds(text.replace("&", ""), frc);
    int w = (int) Math.ceil(strRect.getWidth() / dbuX);

    int h = (int) Math.ceil(strRect.getHeight() / dbuY);

    return new Dimension(w, h);
}

From source file:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;//from   w w w  . jav a2  s  . c o  m
    try {
        if (text == null || text.trim().length() == 0) {
            return null;
        }

        BufferedImage stringImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

        g2d = (Graphics2D) stringImage.getGraphics();
        g2d.setFont(ft);

        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D bounds = fm.getStringBounds(text, g2d);

        TextLayout tl = new TextLayout(text, ft, g2d.getFontRenderContext());

        g2d.dispose();
        g2d = null;

        return createRotatedImage(tl, (int) bounds.getWidth(), (int) bounds.getHeight(), angle);
    } catch (Exception e) {
        e.printStackTrace();

        if (g2d != null) {
            g2d.dispose();
        }
    }

    return null;
}

From source file:de.bund.bfr.jung.JungUtils.java

static <V, E> Shape getTransformedEdgeShape(RenderContext<V, E> rc, Layout<V, E> layout, E e) {
    Graph<V, E> graph = layout.getGraph();
    edu.uci.ics.jung.graph.util.Pair<V> endpoints = graph.getEndpoints(e);
    V v1 = endpoints.getFirst();/*from   w  ww.jav a  2 s  .  c o m*/
    V v2 = endpoints.getSecond();

    if (!rc.getEdgeIncludePredicate().evaluate(Context.<Graph<V, E>, E>getInstance(graph, e))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v1))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v2))) {
        return null;
    }

    Point2D p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v1));
    Point2D p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v2));
    float x1 = (float) p1.getX();
    float y1 = (float) p1.getY();
    float x2 = (float) p2.getX();
    float y2 = (float) p2.getY();
    Shape edgeShape = rc.getEdgeShapeTransformer().transform(Context.getInstance(graph, e));
    AffineTransform edgeShapeTransform = AffineTransform.getTranslateInstance(x1, y1);

    if (v1.equals(v2)) {
        Rectangle2D bounds = rc.getVertexShapeTransformer().transform(v1).getBounds2D();

        edgeShapeTransform.scale(bounds.getWidth(), bounds.getHeight());
        edgeShapeTransform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        float dx = x2 - x1;
        float dy = y2 - y1;

        edgeShapeTransform.rotate(Math.atan2(dy, dx));
        edgeShapeTransform.scale(Math.sqrt(dx * dx + dy * dy), 1.0);
    }

    return edgeShapeTransform.createTransformedShape(edgeShape);
}

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

/**
 * Convert a rectangle with negative width or height to one with
 * positive width and height/*from  w ww  . j a va 2s.  c om*/
 * @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:Main.java

/**
 * Get the full screen size recognizing multiple monitor.
 * /* w w  w  . jav a  2 s.c o m*/
 * @return full screen size
 */
public static Dimension getFullScreenSize() {
    Rectangle2D result = new Rectangle2D.Double();
    GraphicsEnvironment localGE = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (GraphicsDevice gd : localGE.getScreenDevices()) {
        for (GraphicsConfiguration graphicsConfiguration : gd.getConfigurations()) {
            Rectangle2D.union(result, graphicsConfiguration.getBounds(), result);
        }
    }
    return new Dimension((int) result.getWidth(), (int) result.getHeight());
}

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

/**
 * Draws a string centred in the given box.
 *//* w w  w  .  j av a2s .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: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.
 *//*from  ww  w . j a v a 2  s  . co  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: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 ava2  s  .co  m
 * 
 * @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/* w ww  . ja va2  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;
}