Example usage for javax.swing JComponent getFontMetrics

List of usage examples for javax.swing JComponent getFontMetrics

Introduction

In this page you can find the example usage for javax.swing JComponent getFontMetrics.

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:Main.java

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

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

/**
 * Returns the FontMetrics for the specified Font. 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  ww w . j av a2s .c  o m
 * This does not necessarily return the FontMetrics from the Graphics.
 *
 * @param c
 *            JComponent requesting FontMetrics, may be null
 * @param g
 *            Graphics Graphics
 * @param font
 *            Font to get FontMetrics for
 */
public static FontMetrics getFontMetrics(final JComponent c, final Graphics g, final Font font) {
    if (c != null) {
        return c.getFontMetrics(font);
    } else {
        return g.getFontMetrics(font);
    }
}

From source file:Main.java

public static String layoutComponent(JComponent c, String text, Icon icon, int iconTextGap,
        int verticalAlignment, int horizontalAlignment, int verticalTextAlignment, int horizontalTextAlignment,
        Rectangle viewRect, Rectangle iconRect, Rectangle textRect) {
    resetRectangles(iconRect, textRect);

    return SwingUtilities.layoutCompoundLabel(c, c.getFontMetrics(c.getFont()), text, icon, verticalAlignment,
            horizontalAlignment, verticalTextAlignment, horizontalTextAlignment, viewRect, iconRect, textRect,
            iconTextGap);/*from   w ww . j  a  v a 2s . c o  m*/
}

From source file:ala.soils2sat.DrawingUtils.java

/**
 * Returns the FontMetrics for the specified Font. 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  ww w . ja v a 2  s.  c  o  m
 * Callers should pass in a non-null JComonent, 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 c
 *            Graphics Graphics
 * @param font
 *            Font to get FontMetrics for
 */
@SuppressWarnings("deprecation")
public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font font) {
    if (c != null) {
        // Note: We assume that we're using the FontMetrics
        // from the widget to layout out text, otherwise we can get
        // mismatches when printing.
        return c.getFontMetrics(font);
    }
    return Toolkit.getDefaultToolkit().getFontMetrics(font);
}

From source file:Main.java

License:asdf

public void paint(Graphics g, JComponent c) {
    FontMetrics metrics = c.getFontMetrics(g.getFont());
    g.setColor(c.getForeground());// ww  w.java  2 s .  c  o m
    g.drawString(((JToolTip) c).getTipText(), 1, 1);
    g.drawImage(new ImageIcon("yourImage").getImage(), 1, metrics.getHeight(), c);
}

From source file:Main.java

License:asdf

public Dimension getPreferredSize(JComponent c) {
    FontMetrics metrics = c.getFontMetrics(c.getFont());
    String tipText = ((JToolTip) c).getTipText();
    if (tipText == null) {
        tipText = "";
    }//w  w  w .ja v a2 s.  c  o m
    Image image = new ImageIcon("yourImage").getImage();
    int width = SwingUtilities.computeStringWidth(metrics, tipText);
    int height = metrics.getHeight() + image.getHeight(c);

    if (width < image.getWidth(c)) {
        width = image.getWidth(c);
    }
    return new Dimension(width, height);
}

From source file:co.id.app.sys.util.StringUtils.java

public static String getMakeLiner(String temp, int widLimitMetric) {
    if (temp == null) {
        return null;
    }/*from   w w  w  .j  a  v a  2s  .c  o m*/
    String strLiner = "";
    Font ft = new Font("Arial, Helvetica, sans-serif", Font.TRUETYPE_FONT, 9);
    JComponent jx = new JComponent() {
        private static final long serialVersionUID = 2792190024722186161L;
    };
    jx.setFont(ft);
    FontMetrics fm = jx.getFontMetrics(ft);
    int strWidMetric = fm.stringWidth(temp);
    while (strWidMetric > 0) {
        char[] ch = temp.toCharArray();
        if (strWidMetric > widLimitMetric) {
            int wChTot = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; wChTot < widLimitMetric; i++) {
                wChTot += fm.charWidth(ch[i]);
                sb.append(ch[i]);
            }

            String strWid = sb.toString();
            int lastChar13 = strWid.lastIndexOf(" ");
            if (lastChar13 != -1) {
                strWid = temp.substring(0, lastChar13);
                strLiner = strLiner + strWid + "<br/>";
                temp = temp.substring(lastChar13 + 1, temp.length());
                strWidMetric = fm.stringWidth(temp);
            } else {
                strWid = temp.substring(0, strWid.length());
                strLiner = strLiner + strWid + "<br/>";
                temp = temp.substring(strWid.length(), temp.length());
                strWidMetric = fm.stringWidth(temp);
            }
        } else {
            strLiner = strLiner + temp;
            temp = "";
            strWidMetric = fm.stringWidth(temp);
        }

    }
    return strLiner;
}

From source file:org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper.java

public static boolean isTextFit(JComponent component, int width, String text) {
    FontMetrics m = component.getFontMetrics(component.getFont());
    int textWidth = m.stringWidth(text);
    return textWidth <= width;

}

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

public void paint(Graphics g, JComponent c) {

    UIUtils.antialias(g);//from w  w  w. j a va 2  s  .  c o m

    Font font = c.getFont();
    FontMetrics metrics = c.getFontMetrics(font);

    Dimension size = c.getSize();
    if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, size.width + 20, size.height);
    }

    JToolTip toolTip = (JToolTip) c;
    String tipText = getTipText(toolTip);

    if (!MiscUtils.isNull(tipText)) {

        Insets insets = c.getInsets();
        Rectangle paintTextR = new Rectangle(insets.left, insets.top, size.width - (insets.left + insets.right),
                size.height - (insets.top + insets.bottom));

        Color foreground = c.getForeground();
        g.setColor(foreground);
        g.setFont(font);

        g.drawString(tipText, paintTextR.x + 3, paintTextR.y + metrics.getAscent());

        String acceleratorString = getAcceleratorStringForRender(toolTip);
        if (StringUtils.isNotBlank(acceleratorString)) {

            Font acceleratorFont = font.deriveFont(font.getSize() - 1f);
            g.setFont(acceleratorFont);
            g.setColor(GUIUtils.getSlightlyBrighter(foreground, 2.0f));

            g.drawString(acceleratorString, paintTextR.x + 6 + metrics.stringWidth(tipText),
                    paintTextR.y + metrics.getAscent());
        }

    }

}