Example usage for java.awt FontMetrics getAscent

List of usage examples for java.awt FontMetrics getAscent

Introduction

In this page you can find the example usage for java.awt FontMetrics getAscent.

Prototype

public int getAscent() 

Source Link

Document

Determines the font ascent of the Font described by this FontMetrics object.

Usage

From source file:WriteImageType.java

static public void main(String args[]) throws Exception {
    try {//from  w w w. j a va 2s .c  o m
        int width = 200, height = 200;

        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
        // into integer pixels
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D ig2 = bi.createGraphics();

        Font font = new Font("TimesRoman", Font.BOLD, 20);
        ig2.setFont(font);
        String message = "www.java2s.com!";
        FontMetrics fontMetrics = ig2.getFontMetrics();
        int stringWidth = fontMetrics.stringWidth(message);
        int stringHeight = fontMetrics.getAscent();
        ig2.setPaint(Color.black);
        ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

        ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
        ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
        ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
        ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));

    } catch (IOException ie) {
        ie.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String text = "java2s.com";

    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 48);
    g2d.setFont(font);//from   w w  w  .  j a va2  s.  c om
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();

    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    ImageIO.write(img, "png", new File("Text.png"));

}

From source file:Main.java

public static void drawCenteredString(String string, int width, int height, Graphics graphics) {
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int x = (width - fontMetrics.stringWidth(string)) / 2;
    int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2);
    graphics.drawString(string, x, y);//w  ww  .j  a  va2  s .  co  m
}

From source file:Main.java

/**
 * Draw a string centered within a rectangle.  The string is drawn using the graphics context's
 * current font and color.//from   ww w  .  ja  v a2s .  co  m
 *
 * @param g the graphics context.
 * @param str the string.
 * @param x the bounding x position.
 * @param y the bounding y position.
 * @param width the bounding width.
 * @param height the bounding height.
 */
public static void drawStringCentered(Graphics g, String str, int x, int y, int width, int height) {
    FontMetrics fm = g.getFontMetrics(g.getFont());
    int xpos = x + ((width - fm.stringWidth(str)) / 2);
    int ypos = y + ((height + fm.getAscent()) / 2);
    g.drawString(str, xpos, ypos);
}

From source file:GraphicsUtil.java

static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
    if (g == null)
        return new Rectangle(x, y, 0, 0);
    FontMetrics mets = g.getFontMetrics();
    int width = mets.stringWidth(text);
    int ascent = mets.getAscent();
    int height = ascent + mets.getDescent();
    Rectangle ret = new Rectangle(x, y, width, height);

    switch (halign) {
    case H_CENTER:
        ret.translate(-(width / 2), 0);//from www  .  j  ava  2  s .c om
        break;
    case H_RIGHT:
        ret.translate(-width, 0);
        break;
    default:
        ;
    }
    switch (valign) {
    case V_TOP:
        break;
    case V_CENTER:
        ret.translate(0, -(ascent / 2));
        break;
    case V_BASELINE:
        ret.translate(0, -ascent);
        break;
    case V_BOTTOM:
        ret.translate(0, -height);
        break;
    default:
        ;
    }
    return ret;
}

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//from  w w w.  ja v  a2 s .c om
 * @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.ut.biolab.medsavant.client.util.ClientMiscUtils.java

/**
 * Draws a string centred in the given box.
 *//*from w  w  w .j  av a 2 s.c  om*/
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:edu.ku.brc.ui.GraphicsUtils.java

/**
 * Draws the given <code>String</code>, centered at <code>(x,y)</code>, using
 * the given graphics context.//w  ww . j  a  v  a 2  s .  c  o m
 * 
 * @param s the string
 * @param g the graphics context to draw in
 * @param x the x coordinate for center of the <code>String</code>
 * @param y the y coordinate for center of the <code>String</code>
 */
public static void drawCenteredString(String s, Graphics g, int x, int y) {
    ((Graphics2D) g).addRenderingHints(hints);

    FontMetrics fm = g.getFontMetrics();
    int ht = fm.getAscent() + fm.getDescent();
    int width = fm.stringWidth(s);
    g.drawString(s, x - width / 2, y + (fm.getAscent() - ht / 2));
}

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  . j a  v  a  2 s  .  c  om*/
    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:savant.util.MiscUtils.java

/**
 * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box.
 *///w  ww.  j a  va2  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);
}