Example usage for java.awt FontMetrics stringWidth

List of usage examples for java.awt FontMetrics stringWidth

Introduction

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

Prototype

public int stringWidth(String str) 

Source Link

Document

Returns the total advance width for showing the specified String in this Font .

Usage

From source file:org.mskcc.cbio.portal.util.MakeOncoPrint.java

/**
 * Constructs the OncoPrint case set description.
 *
  * @param caseSetId String/* www.  j a  va  2 s . com*/
 * @param caseSets List<CaseList>
 *
 * @return String
 */
static String getCaseSetDescription(String caseSetId, List<CaseList> caseSets) {

    String toReturn = new String();
    StringBuilder builder = new StringBuilder();
    for (CaseList caseSet : caseSets) {
        if (caseSetId.equals(caseSet.getStableId())) {
            builder.append(CASE_SET_DESCRIPTION_LABEL + caseSet.getName() + ": " + caseSet.getDescription());
        }
    }
    // insert newlines as needed
    int descriptionLength = 0;
    String[] tokens = builder.toString().split(" ");
    FontMetrics fontMetrics = getFontMetrics();
    for (int lc = 0; lc < tokens.length; lc++) {
        int newLength = fontMetrics.stringWidth(tokens[lc]);
        // we do not have enough space to add token to this line, append newline, reset accumulator
        if ((descriptionLength + newLength) > HEADER_CANVAS_WIDTH_PIXELS) {
            if (toReturn.endsWith(" ")) {
                toReturn = toReturn.trim();
            }
            toReturn += "\\n" + tokens[lc] + " ";
            descriptionLength = fontMetrics.stringWidth(tokens[lc] + " " + CASE_SET_DESCRIPTION_LABEL);
        } else {
            // this token fits on line, add it to return string, add length to accumulator
            toReturn += tokens[lc];
            descriptionLength += newLength;
            // if this is not the last token in description, append space
            if (lc <= tokens.length - 1) {
                newLength = fontMetrics.stringWidth(" ");
                // if we can't fit space, append newline, reset accumulator
                if ((descriptionLength + newLength) > HEADER_CANVAS_WIDTH_PIXELS) {
                    toReturn += "\\n";
                    descriptionLength = fontMetrics.stringWidth(CASE_SET_DESCRIPTION_LABEL);
                }
                // we can fit space on this line, add it to return string, add length to accumulator
                else {
                    toReturn += " ";
                    descriptionLength += newLength;
                }
            }
        }
    }

    // we need to replace " in string with \" otherwise javascript will puke
    return (toReturn.indexOf("\"") != -1) ? toReturn.replace("\"", "\\\"") : toReturn;
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFHeaderColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from   ww w.j a  v  a  2 s.  c o m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - width) / 2;
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:ala.soils2sat.DrawingUtils.java

/**
 * Draws the text by creating a rectange centered around x, y
 *
 * @param g/*from w w  w .j  a v a 2  s .co  m*/
 * @param font
 * @param x
 * @param y
 * @return The rectangle that bounds the text
 */
public static Rectangle drawCentredText(Graphics2D g, Font font, String text, int x, int y) {
    g.setFont(font);
    FontMetrics fm = g.getFontMetrics(font);

    setPreferredAliasingMode(g);

    Rectangle ret = new Rectangle(x, y, 0, 0);

    if (text == null) {
        return ret;
    }
    String[] alines = text.split("\\n");
    ArrayList<String> lines = new ArrayList<String>();
    for (String s : alines) {
        if (s.length() > 0) {
            lines.add(s);
        }
    }
    int numlines = lines.size();
    if (numlines > 0) {
        int maxwidth = 0;
        int totalheight = 0;
        for (int idx = 0; idx < numlines; ++idx) {
            String line = lines.get(idx);
            int stringWidth = fm.stringWidth(line);
            if (stringWidth > maxwidth) {
                maxwidth = stringWidth;
            }
            totalheight += fm.getAscent() + fm.getDescent();
        }
        ret.width = maxwidth;
        ret.height = totalheight;
        ret.x = x - (maxwidth / 2);
        ret.y = y - (totalheight / 2);

        drawString(g, font, text, ret, TEXT_ALIGN_CENTER);

        return ret;
    }
    return ret;

}

From source file:DialogSeparator.java

public void paint(Graphics g) {
    g.setColor(getBackground());// w ww . j  a v  a 2s.  com
    g.fillRect(0, 0, getWidth(), getHeight());

    Dimension d = getSize();
    int y = (d.height - 3) / 2;
    g.setColor(Color.white);
    g.drawLine(1, y, d.width - 1, y);
    y++;
    g.drawLine(0, y, 1, y);
    g.setColor(Color.gray);
    g.drawLine(d.width - 1, y, d.width, y);
    y++;
    g.drawLine(1, y, d.width - 1, y);

    String text = getText();
    if (text.length() == 0)
        return;

    g.setFont(getFont());
    FontMetrics fm = g.getFontMetrics();
    y = (d.height + fm.getAscent()) / 2;
    int fontWidth = fm.stringWidth(text);

    g.setColor(getBackground());
    g.fillRect(OFFSET - 5, 0, OFFSET + fontWidth, d.height);

    g.setColor(getForeground());
    g.drawString(text, OFFSET, y);
}

From source file:FontSizeAnimation.java

public void paint(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    Font font = new Font("Dialog", Font.PLAIN, x);
    g2d.setFont(font);/*from   w  w  w. j ava2  s . c  o m*/

    FontMetrics fm = g2d.getFontMetrics();
    String s = "Java";

    int w = (int) getSize().getWidth();
    int h = (int) getSize().getHeight();

    int stringWidth = fm.stringWidth(s);

    g2d.drawString(s, (w - stringWidth) / 2, h / 2);
}

From source file:ClipDemo.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // get damaged region
    Rectangle clipRect = g.getClipBounds();
    int clipx = clipRect.x;
    int clipy = clipRect.y;
    int clipw = clipRect.width;
    int cliph = clipRect.height;

    // fill damaged region only
    g.setColor(Color.white);//from ww w . jav a2 s  .c om
    g.fillRect(clipx, clipy, clipw, cliph);

    if (clipx <= 240 && clipy <= 240) {
        g.setColor(Color.yellow);
        g.fillOval(0, 0, 240, 240);
        System.out.println(" yellow Oval repainted.");
    }

    if (clipx + clipw >= 160 && clipx <= 400 && clipy + cliph >= 160 && clipy <= 400) {
        g.setColor(Color.magenta);
        g.fillOval(160, 160, 240, 240);
        System.out.println(" magenta Oval repainted.");
    }

    int iconWidth = java2sLogo.getIconWidth();
    int iconHeight = java2sLogo.getIconHeight();

    if (clipx + clipw >= 280 - (iconWidth / 2) && clipx <= (280 + (iconWidth / 2))
            && clipy + cliph >= 120 - (iconHeight / 2) && clipy <= (120 + (iconHeight / 2))) {
        java2sLogo.paintIcon(this, g, 280 - (iconWidth / 2), 120 - (iconHeight / 2));
        System.out.println(" logo below blue Rect repainted.");
    }

    if (clipx + clipw >= 120 - (iconWidth / 2) && clipx <= (120 + (iconWidth / 2))
            && clipy + cliph >= 280 - (iconHeight / 2) && clipy <= (280 + (iconHeight / 2))) {
        java2sLogo.paintIcon(this, g, 120 - (iconWidth / 2), 280 - (iconHeight / 2));
        System.out.println(" logo below red Rect repainted.");
    }

    if (clipx + clipw >= 60 && clipx <= 180 && clipy + cliph >= 220 && clipy <= 340) {
        g.setColor(red);
        g.fillRect(60, 220, 120, 120);
        System.out.println(" red Rect repainted.");

    }

    if (clipx + clipw > 140 && clipx < 260 && clipy + cliph > 140 && clipy < 260) {
        g.setColor(green);
        g.fillOval(140, 140, 120, 120);
        System.out.println(" green Oval repainted.");

    }

    if (clipx + clipw > 220 && clipx < 380 && clipy + cliph > 60 && clipy < 180) {
        g.setColor(blue);
        g.fillRect(220, 60, 120, 120);
        System.out.println(" blue Rect repainted.");
    }

    g.setColor(Color.black);

    g.setFont(monoFont);
    FontMetrics fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("Java Source");
    iconHeight = fm.getAscent();
    int d = fm.getDescent();
    if (clipx + clipw > 120 - (iconWidth / 2) && clipx < (120 + (iconWidth / 2))
            && clipy + cliph > (120 + (iconHeight / 4)) - iconHeight && clipy < (120 + (iconHeight / 4)) + d) {
        g.drawString("Java Source", 120 - (iconWidth / 2), 120 + (iconHeight / 4));
        System.out.println(" Java Source repainted.");
    }

    g.setFont(sanFont);
    fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("and");
    iconHeight = fm.getAscent();
    d = fm.getDescent();
    if (clipx + clipw > 200 - (iconWidth / 2) && clipx < (200 + (iconWidth / 2))
            && clipy + cliph > (200 + (iconHeight / 4)) - iconHeight && clipy < (200 + (iconHeight / 4)) + d) {
        g.drawString("and", 200 - (iconWidth / 2), 200 + (iconHeight / 4));
        System.out.println(" and repainted.");
    }

    g.setFont(serifFont);
    fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("Support.");
    iconHeight = fm.getAscent();
    d = fm.getDescent();

    if (clipx + clipw > 280 - (iconWidth / 2) && clipx < (280 + (iconWidth / 2))
            && clipy + cliph > (280 + (iconHeight / 4)) - iconHeight && clipy < (280 + (iconHeight / 4)) + d) {
        g.drawString("Support.", 280 - (iconWidth / 2), 280 + (iconHeight / 4));
        System.out.println(" Support. repainted.");
    }
}

From source file:WalkingText.java

/** Applet Initializer */
public void init() {
    xloc = 0;/* w  w w.  j  a v  a2  s .  c om*/
    width = getSize().width;
    height = getSize().height;

    if ((mesg = getParameter("text")) == null)
        mesg = "Hello World of Java";

    String pSize = getParameter("fontsize");
    if (pSize == null)
        pSize = "12";

    String fontName = getParameter("fontName");
    if (fontName == null)
        fontName = "Helvetica";

    // System.out.println("Font is " + pSize + " point " + fontName);
    Font f = new Font(fontName, Font.PLAIN, Integer.parseInt(pSize));
    setFont(f);

    FontMetrics fm = getToolkit().getFontMetrics(f);
    textWidth = fm.stringWidth(mesg);
    textHeight = fm.getHeight();
    // System.out.println("TextWidth " + textWidth + ", ht " + textHeight);

    // use textHeight in y coordinate calculation
    yloc = height - ((height - textHeight) / 2);
}

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

public Dimension getPreferredSize(JComponent c) {

    Dimension d = super.getPreferredSize(c);

    JToolTip tip = (JToolTip) c;
    String tipText = getTipText(tip) + getAcceleratorStringForRender(tip);

    if (!MiscUtils.isNull(tipText)) {

        Font font = c.getFont();/* w ww  .j a  v  a2 s. c  om*/
        FontMetrics fm = c.getFontMetrics(font);
        d.width = fm.stringWidth(tipText) + 15;

    } else {

        d.width += 10;
    }

    return d;
}

From source file:MyButtonUI.java

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    Dimension d = b.getSize();/*w ww  .j  av a  2  s  .c om*/

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();

    g.setColor(b.getForeground());
    String caption = b.getText();
    int x = (d.width - fm.stringWidth(caption)) / 2;
    int y = (d.height + fm.getAscent()) / 2;
    g.drawString(caption, x, y);

}

From source file:FontPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Font f = new Font("SansSerif", Font.BOLD, 14);
    Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
    FontMetrics fm = g.getFontMetrics(f);
    FontMetrics fim = g.getFontMetrics(fi);

    String s1 = "Java ";
    String s2 = "Source and Support";
    String s3 = " at www.java2s.com";
    int width1 = fm.stringWidth(s1);
    int width2 = fim.stringWidth(s2);
    int width3 = fm.stringWidth(s3);

    Dimension d = getSize();//  w w  w  .  j  a v  a  2s .  com
    int cx = (d.width - width1 - width2 - width3) / 2;
    int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();

    g.setFont(f);
    g.drawString(s1, cx, cy);
    cx += width1;
    g.setFont(fi);
    g.drawString(s2, cx, cy);
    cx += width2;
    g.setFont(f);
    g.drawString(s3, cx, cy);
}