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: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.
 *//*  w ww  .  j a v a 2  s  .  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'.//from  w ww .  j ava  2s . 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 .j  a v a 2 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 ww  w. ja v  a  2 s . com*/
    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   ww w .  j  a v a 2  s .  c o  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:org.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *///from ww w .java  2 s . com
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:GraphicsUtil.java

public static void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();/* ww  w.j  a  v  a  2 s  .  c o  m*/
    if (angle != 0)
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case North:
    case South:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case NorthEast:
    case East:
    case SouthEast:
        x -= (sSize.getWidth());
        break;
    case SouthWest:
    case West:
    case NorthWest:
        break;
    }

    g2.drawString(text, (float) x, (float) y);
    g2.setFont(font);
}

From source file:Main.java

/**
 * Get the full screen size recognizing multiple monitor.
 * //ww  w .  ja  v  a  2s  .  co 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: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 .jav  a2 s  . 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.esa.snap.graphbuilder.rcp.dialogs.support.GraphPanel.java

private static void drawHelp(final Graphics g) {
    final int x = (int) (g.getClipBounds().getWidth() / 2);
    final int y = (int) (g.getClipBounds().getHeight() / 2);

    final FontMetrics metrics = g.getFontMetrics();
    final String name = "Right click here to add an operator";
    final Rectangle2D rect = metrics.getStringBounds(name, g);
    final int stringWidth = (int) rect.getWidth();

    g.setColor(Color.black);/*from   ww w .ja  v a 2s.  c  o m*/
    g.drawString(name, x - stringWidth / 2, y);
}