Example usage for java.awt Graphics2D getFontMetrics

List of usage examples for java.awt Graphics2D getFontMetrics

Introduction

In this page you can find the example usage for java.awt Graphics2D getFontMetrics.

Prototype

public FontMetrics getFontMetrics() 

Source Link

Document

Gets the font metrics of the current font.

Usage

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Plot text centered at a point (rather than from the upper left corner as is the default)
 *
 * @param g2       graphics2D object/*  w w w  .  j  a v  a2 s.co m*/
 * @param text     to be plotted
 * @param position of where text will be centered at
 */
private static void drawCenteredString(Graphics2D g2, String text, Point position) {
    FontMetrics fm = g2.getFontMetrics();
    int x2 = position.x - fm.stringWidth(text) / 2;
    int y2 = fm.getAscent() + (position.y - (fm.getAscent() + fm.getDescent()) / 2);
    g2.drawString(text, x2, y2);
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

/**
 * Automatically change the font size to fit in the cell height.
 *
 * @param g           the Graphics2D object
 * @param cellHeight  the cell height/*from  w w w  . ja v  a  2 s . co m*/
 * @param minFontSize the min font size
 * @return Returns true if the new font size fits in the cell height, false if it doesn't fit.
 */
protected static boolean calculateFontSize(Graphics2D g, int cellHeight, int minFontSize) {

    float fontHeight = g.getFontMetrics().getHeight();

    float fontSize = g.getFont().getSize();
    while (fontHeight > (cellHeight - 2) && fontSize > minFontSize) {
        fontSize--;
        g.setFont(g.getFont().deriveFont(fontSize));
        fontHeight = g.getFontMetrics().getHeight();
    }

    return fontHeight <= (cellHeight - 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   w  w  w. j  a  v  a2 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:org.jhotdraw.samples.svg.figures.SVGImage.java

public static SVGImage getLoadingImage() {
    BufferedImage image;/*w  w w  .java 2  s .  c o m*/
    try {
        image = ImageIO.read(SVGImage.class.getResource("loading.jpg"));
    } catch (Exception e) {
        String text = "";
        Font font = new Font("", Font.PLAIN, 18);
        image = new BufferedImage(150, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) image.createGraphics();
        g.setColor(Color.black);
        g.setFont(font);
        int w = g.getFontMetrics().stringWidth(text);
        int h = g.getFontMetrics().getHeight();
        g.drawString(text, 10, h);
        g.dispose();
    }
    return new SVGImage(image);
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static Dimension getFontExtent(String string, Graphics2D context) {
    return new Dimension(context.getFontMetrics().stringWidth(string), context.getFontMetrics().getHeight());
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static float getFontBaseline(Graphics2D context) {
    final FontMetrics fontMetrics = context.getFontMetrics();
    return fontMetrics.getMaxAscent();
}

From source file:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;
    try {//from www .  ja v  a 2 s  .  c  o  m
        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:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *//*from   ww  w .  j  a  v a2 s.com*/
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.piaoyou.util.ImageUtil.java

public static BufferedImage getProductionImage(String productVersion, String productRealeaseDate) {

    String str = productVersion + " on " + productRealeaseDate;
    int IMAGE_WIDTH = 250;
    int IMAGE_HEIGHT = 30;

    BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.setBackground(Color.blue);/*  www . ja v a  2  s . co m*/
    g.setColor(Color.white);
    g.draw3DRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, false);
    FontMetrics fontMetrics = g.getFontMetrics();
    int strWidth = fontMetrics.stringWidth(str);
    int strHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
    g.drawString(str, (IMAGE_WIDTH - strWidth) / 2,
            IMAGE_HEIGHT - ((IMAGE_HEIGHT - strHeight) / 2) - fontMetrics.getDescent());
    g.dispose(); // free resource
    return bufferedImage;
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

protected static void paintCell(Decoration decoration, Color gridColor, int gridSize, int offsetX, int offsetY,
        int width, int height, Graphics2D g, Rectangle box) {

    int y = box.y + offsetY;
    int x = box.x + offsetX;

    g.setColor(decoration.getBgColor());
    g.fillRect(x, y, width, height);//  w  w  w  .j  a  v a 2 s.  c  o m

    g.setColor(gridColor);
    g.fillRect(x, y + height, width, gridSize);

    String text = decoration.getFormatedValue();
    if (!StringUtils.isEmpty(text)) {

        Font font = g.getFont();

        boolean isRotated = decoration.isRotate();

        int fontHeight = (int) font.getSize2D();

        if (fontHeight <= (isRotated ? width : height)) {

            int textWidth = (int) g.getFontMetrics().getStringBounds(text, g).getWidth();
            //TODO: textWidth depends on SuperScript

            if (textWidth < (isRotated ? height : width)) {

                int leftMargin = ((width - textWidth) / 2) + 1;
                int bottomMargin = ((height - fontHeight) / 2) + 1;

                if (isRotated) {
                    leftMargin = ((width - fontHeight) / 2) + 1;
                    bottomMargin = height - (((height - textWidth) / 2));
                }

                g.setColor(Colors.bestForegroundColor(decoration.getBgColor()));
                if (text.matches("[0-9\\.]+e-?[0-9]+")) {
                    int e_pos = text.indexOf("e") + 3;
                    text = text.replaceAll("e(-?[0-9]+)", "10$1");
                    int superscriptEnd = text.length();
                    AttributedString attText = new AttributedString(text);
                    attText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, e_pos,
                            superscriptEnd);
                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(attText.getIterator(), y + height - bottomMargin, -x - leftMargin - 1);
                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
                    }
                } else {

                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(text, y + height - bottomMargin, -x - leftMargin - 1);

                        if ("CoCA-08".equals(text)) {
                            System.out.println("x = " + x + " leftMargin = " + leftMargin + " width = " + width
                                    + " fontHeight = " + fontHeight);
                        }

                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(text, x + leftMargin, y + height - bottomMargin);
                    }
                }
            }
        }
    }

}