Example usage for java.awt Graphics getFont

List of usage examples for java.awt Graphics getFont

Introduction

In this page you can find the example usage for java.awt Graphics getFont.

Prototype

public abstract Font getFont();

Source Link

Document

Gets the current font.

Usage

From source file:Main.java

public static FontMetrics getFontMetrics(JComponent c, Graphics g) {
    Font font = g.getFont();
    if (c != null) {
        return c.getFontMetrics(g.getFont());
    }//from   w  w w  . ja  va 2s .  c o  m
    return g.getFontMetrics(font);
}

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

/**
 * Returns the FontMetrics for the current Font of the passed in Graphics.
 * This method is used when a Graphics is available, typically when
 * painting. If a Graphics is not available the JComponent method of the
 * same name should be used./*from ww w .j  a v  a2 s . c o m*/
 * <p/>
 * This does not necessarily return the FontMetrics from the Graphics.
 *
 * @param c
 *            JComponent requesting FontMetrics, may be null
 * @param g
 *            Graphics Graphics
 */
public static FontMetrics getFontMetrics(final JComponent c, final Graphics g) {
    return getFontMetrics(c, g, g.getFont());
}

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.//  www  .  j  av  a 2  s . c o  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 void drawText(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
    Font oldfont = g.getFont();
    if (font != null)
        g.setFont(font);/*from ww w  .  j  av a 2s. com*/
    drawText(g, text, x, y, halign, valign);
    if (font != null)
        g.setFont(oldfont);
}

From source file:Main.java

public static void setLabelTop(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    label.setText(str);//  w w  w  .  java2 s .  c om
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth / 2;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) {
    Graphics g = label.getGraphics();
    label.setText(str);//from www  .  ja  va 2s . c  om
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = x + width / 2 - strWidth / 2;
    int lY = y - g.getFont().getSize();
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelCenter(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    Color c = g.getColor();//  w w  w.j  a va  2  s. co  m
    g.setColor(Color.BLUE);
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
    g.setColor(c);
}

From source file:GraphicsUtil.java

static public Rectangle getTextBounds(Graphics g, Font font, String text, int x, int y, int halign,
        int valign) {
    if (g == null)
        return new Rectangle(x, y, 0, 0);
    Font oldfont = g.getFont();
    if (font != null)
        g.setFont(font);//from   www.  j  a  va  2s.c o  m
    Rectangle ret = getTextBounds(g, text, x, y, halign, valign);
    if (font != null)
        g.setFont(oldfont);
    return ret;
}

From source file:ala.soils2sat.DrawingUtils.java

/**
 * Returns the FontMetrics for the current Font of the passed in Graphics. This method is used when a Graphics is available, typically when painting. If a Graphics is not available the JComponent method of the same name should be used.
 * <p>/*from w ww  . ja v  a2s  .  c o m*/
 * Callers should pass in a non-null JComponent, the exception to this is if a JComponent is not readily available at the time of painting.
 * <p>
 * This does not necessarily return the FontMetrics from the Graphics.
 *
 * @param c
 *            JComponent requesting FontMetrics, may be null
 * @param g
 *            Graphics Graphics
 */
public static FontMetrics getFontMetrics(JComponent c, Graphics g) {
    return getFontMetrics(c, g, g.getFont());
}