Example usage for java.awt Font getStringBounds

List of usage examples for java.awt Font getStringBounds

Introduction

In this page you can find the example usage for java.awt Font getStringBounds.

Prototype

public Rectangle2D getStringBounds(String str, FontRenderContext frc) 

Source Link

Document

Returns the logical bounds of the specified String in the specified FontRenderContext .

Usage

From source file:Main.java

public static int getFontSize(String text, boolean getWidth) {
    if (text != null && text.length() > 0) {
        Font defaultFont = new Font("Montserrat", Font.PLAIN, 13);

        AffineTransform affinetransform = new AffineTransform();
        FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
        int textWidth = (int) (defaultFont.getStringBounds(text, frc) != null
                ? defaultFont.getStringBounds(text, frc).getWidth()
                : 0) + 10;//from www  . j a va  2 s . c o  m
        int textHeight = (int) (defaultFont.getStringBounds(text, frc) != null
                ? defaultFont.getStringBounds(text, frc).getHeight()
                : 0);
        return getWidth ? textWidth : textHeight;
    }
    return 0;
}

From source file:Main.java

/**
 * Gets the text width./* w  w  w  .ja  va2  s.co m*/
 *
 * @param text
 *            the text
 * @param font
 *            the font
 * @return the text width
 */
public static int getTextWidth(final String text, final Font font) {
    // final JLabel lbl = new JLabel(text);
    // lbl.setBorder(g);
    // lbl.setFont(font);
    // return (int) lbl.getPreferredSize().getWidth();
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    int textwidth = (int) (font.getStringBounds(text, frc).getWidth());
    int textheight = (int) (font.getStringBounds(text, frc).getHeight());
    return textwidth;
}

From source file:com.github.fritaly.dualcommander.Utils.java

public static int getTimestampRenderWidth() {
    final String text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    final Font font = Utils.getDefaultFont();
    final FontRenderContext context = new FontRenderContext(new AffineTransform(), true, true);

    return (int) font.getStringBounds(text, context).getWidth();
}

From source file:CheckFonts.java

/**
 * ?  ??  dialog base unit'./*from ww w.  j av a2s . com*/
 * 
 * ? :   - ?  ????  ,   - ??  ????
 *  .
 */
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:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);/*from w ww.j a v a2s.c  o m*/

    String s = "www.java2s.com";
    float x = 50, y = 150;

    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.lightGray);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:FontTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    String message = "Hello, World!";

    Font f = new Font("Serif", Font.BOLD, 36);
    g2.setFont(f);//from www  .j a v a 2s . c om

    // measure the size of the message

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = f.getStringBounds(message, context);

    // set (x,y) = top left corner of text

    double x = (getWidth() - bounds.getWidth()) / 2;
    double y = (getHeight() - bounds.getHeight()) / 2;

    // add ascent to y to reach the baseline

    double ascent = -bounds.getY();
    double baseY = y + ascent;

    // draw the message

    g2.drawString(message, (int) x, (int) baseY);

    g2.setPaint(Color.LIGHT_GRAY);

    // draw the baseline

    g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));

    // draw the enclosing rectangle

    Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
    g2.draw(rect);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);//from   w  w w.  ja  va 2  s. c  om

    String s = "this is a test";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:LineMetricsIllustration.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);//from  w w w . j av  a  2 s.  c om

    String s = "Java Source and Support";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:org.fhaes.fhrecorder.view.GraphSummaryOverlay.java

/**
 * @return the approximate width of the label in pixels corresponding to the largest value of the overlay graph's y axis.
 *//*w w w  . j  av a 2s . c  om*/
public int getMaximumRangeLabelWidth() {

    String maxRangeString = Integer.toString(maxRange);
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
    Font font = new Font("SansSerif", Font.PLAIN, 10);
    return (int) (font.getStringBounds(maxRangeString, frc).getWidth() + 6);
}

From source file:ch.rgw.tools.StringTool.java

/**
 * return the bounds of a Rectangle around a String
 * /* www.  j  av a  2  s.com*/
 * @deprecated this ist a dependency to Swing
 */
@Deprecated
public static Rectangle2D getStringBounds(final String s, final Graphics g) {
    if (isNothing(s)) {
        return new Rectangle(0, 0);
    }
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Font fnt = g.getFont();
    Rectangle2D r = fnt.getStringBounds(s, frc);
    return r;
}