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:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);/* w ww  .  j a  va  2  s.com*/
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

From source file:Main.java

public static boolean isVectorFont(Font font) {
    GlyphVector gv = font.createGlyphVector(new FontRenderContext(null, true, false), "Test");
    Shape fontShape = gv.getOutline();
    Rectangle2D bounds = fontShape.getBounds2D();
    return !(bounds.getWidth() == 0 && bounds.getHeight() == 0);
}

From source file:Main.java

public static void drawCenteredString(Graphics g, String str, int x, int y) {
    FontMetrics metrics = g.getFontMetrics(g.getFont());
    Rectangle2D rect = metrics.getStringBounds(str, g);
    int w = (int) (rect.getWidth());
    int h = (int) (rect.getHeight());
    g.drawString(str, x - w / 2, y + h / 2);
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text//from   www  .j a va 2  s.c  o m
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:Main.java

public static void setJButtonSizesTheSame(final JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException();
    }//from  w ww . ja v  a  2s . com

    final Dimension maxSize = new Dimension(0, 0);
    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        final FontMetrics fm = btn.getFontMetrics(btn.getFont());
        final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
        final int boundsHeight = (int) bounds.getHeight();
        final int boundsWidth = (int) bounds.getWidth();
        maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
        maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
    }

    final Insets insets = btns[0].getInsets();
    maxSize.width += insets.left + insets.right;
    maxSize.height += insets.top + insets.bottom;

    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        btn.setPreferredSize(maxSize);
    }

    initComponentHeight(btns);
}

From source file:GUIUtils.java

/**
 * Change the sizes of all the passed buttons to be the size of the largest
 * one./*from  ww w . ja  v a 2 s . c  o  m*/
 * 
 * @param btns
 *          Array of buttons to eb resized.
 * 
 * @throws IllegalArgumentException
 *           If <TT>btns</TT> is <TT>null</TT>.
 */
public static void setJButtonSizesTheSame(JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException("null JButton[] passed");
    }

    // Get the largest width and height
    final Dimension maxSize = new Dimension(0, 0);
    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        final FontMetrics fm = btn.getFontMetrics(btn.getFont());
        Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
        int boundsHeight = (int) bounds.getHeight();
        int boundsWidth = (int) bounds.getWidth();
        maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
        maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
    }

    Insets insets = btns[0].getInsets();
    maxSize.width += insets.left + insets.right;
    maxSize.height += insets.top + insets.bottom;

    for (int i = 0; i < btns.length; ++i) {
        JButton btn = btns[i];
        btn.setPreferredSize(maxSize);
    }
}

From source file:Main.java

private static float getPercentageOnScreen(Point location, Dimension size, Rectangle screen) {
    Rectangle frameBounds = new Rectangle(location, size);
    Rectangle2D intersection = frameBounds.createIntersection(screen);
    int frameArea = size.width * size.height;
    int intersectionArea = (int) (intersection.getWidth() * intersection.getHeight());
    float percentage = (float) intersectionArea / frameArea;
    return percentage < 0 ? 0 : percentage;
}

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:GraphicsUtil.java

public static void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();/*from   www. j av a 2  s  . c  om*/
    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

/**
 * 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.//ww  w  .j a va2s. 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())));

}