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:com.ctsim.dmi.MainFrame.java

private void drawPin() {

    //double speed = 100 * (double) x / 1013;
    String speedShow = df.format(App.speed);
    int strWidth;

    AffineTransform restore = g2.getTransform();
    AffineTransform trans = new AffineTransform();
    trans.translate(266, 90);/*from   ww w  . j a  v a  2s  .c  om*/
    trans.rotate(Math.toRadians(getPinAngle(App.speed)), 40, 165);
    g2.setTransform(trans);
    g2.drawImage(speedoPinWhite, 0, 0, this);
    g2.setTransform(restore);

    // Draw actual speed
    g2.setFont(new Font("Loma", Font.BOLD, 30));
    g2.setColor(Color.BLACK);
    FontMetrics metrics = g2.getFontMetrics();
    strWidth = metrics.stringWidth(speedShow);

    g2.drawString(String.valueOf(speedShow), 306 - strWidth / 2, 265);
}

From source file:edu.ku.brc.af.core.NavBox.java

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

    Dimension dim = getSize();/*from   ww  w.  ja va  2 s . c  o m*/

    FontMetrics fm = g.getFontMetrics();
    int strW = fm.stringWidth(name);

    int x = (dim.width - strW) / 2;
    Insets ins = getBorder().getBorderInsets(this);
    int y = 2 + fm.getAscent();

    int lineW = dim.width - ins.left - ins.right;
    x = ins.left;
    int txtY = y;
    y += 3;

    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(x, y, x + lineW, y);
    y++;
    minHeight = y;

    x++;
    g.setColor(Color.LIGHT_GRAY.darker());
    g.drawLine(x, y, x + lineW, y);

    ((Graphics2D) g).setRenderingHints(UIHelper.createTextRenderingHints());
    g.setColor(Color.BLUE.darker());
    g.drawString(name, x, txtY);

    if (collapsableIconOpen != null) {
        if (iconRect == null) {
            iconRect = getBounds();
            iconRect.x = iconRect.width - collapsableIconOpen.getIconWidth();
            iconRect.y = 0;
        }
        g.drawImage(icon.getImage(), iconRect.x, iconRect.y, null);
    }
}

From source file:com.ctsim.dmi.MainFrame.java

private void drawButtonYard(int x, int y, boolean isRequest) {
    FontMetrics metrics = g2.getFontMetrics();
    int strWidth = metrics.stringWidth("Yard");

    if (App.atpStatus == 1) {
        g2.setColor(Color.GREEN);
        g2.fillRect(x, y, bttnWidth, bttnHeight);
        g2.setColor(Color.BLACK);

    } else if (isRequest) {

        if (isFlashOn) {
            g2.setStroke(new BasicStroke(4));
            g2.setColor(Color.YELLOW);
        } else {/*from  w  ww  . j av  a 2  s  . com*/
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.LIGHT_GRAY);
        }

    } else {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.LIGHT_GRAY);
    }

    g2.drawRect(x, y, bttnWidth, bttnHeight);
    g2.drawString("Yard", x + (bttnWidth / 2) - strWidth / 2, y + bttnHeight - 5 - metrics.getHeight() / 2);
}

From source file:com.celements.photo.image.GenerateThumbnail.java

private FontMetrics calcCopyrightFontSize(String copyright, int width, Graphics2D g2d) {
    FontMetrics metrics;
    int fontSize = 16;
    do {/*from   ww  w . j av  a2s . c o  m*/
        metrics = g2d.getFontMetrics(new Font(g2d.getFont().getFontName(), Font.BOLD, fontSize));
        fontSize--;
    } while ((fontSize > 0) && (metrics.stringWidth(copyright) > (width / 3)));
    return metrics;
}

From source file:com.ctsim.dmi.MainFrame.java

private void drawButton(String name, int status, int x, int y, boolean isRequest) {

    FontMetrics metrics = g2.getFontMetrics();
    int strWidth = metrics.stringWidth(name);
    g2.setStroke(new BasicStroke(2));

    if (App.atpStatus == status) {
        g2.setColor(Color.GREEN);
        g2.fillRect(x, y, bttnWidth, bttnHeight);
        g2.setColor(Color.BLACK);

    } else if (isRequest) {
        if (isFlashOn) {
            g2.setStroke(new BasicStroke(4));
            g2.setColor(Color.YELLOW);
        } else {/*from   www .  ja  v  a2 s .c  om*/
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.LIGHT_GRAY);
        }

    } else {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.LIGHT_GRAY);
    }

    g2.drawRect(x, y, bttnWidth, bttnHeight);
    g2.drawString(name, x + (bttnWidth / 2) - strWidth / 2, y + bttnHeight - 5 - metrics.getHeight() / 2);
}

From source file:com.celements.photo.image.GenerateThumbnail.java

private void drawBackground(String copyright, int width, int height, int bottomSpace, int rightSpace,
        int vSpacing, int hSpacing, int rounding, int stringHeight, FontMetrics metrics, Graphics2D g2d) {
    g2d.setColor(new Color(0, 0, 0));
    AlphaComposite transprency = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
    g2d.setComposite(transprency);/*from w ww .  j av  a 2s .c o  m*/
    g2d.fillRoundRect(width - metrics.stringWidth(copyright) - rightSpace - 2 * hSpacing,
            height - stringHeight - bottomSpace - 2 * vSpacing, metrics.stringWidth(copyright) + 2 * hSpacing,
            stringHeight + 2 * vSpacing, rounding, rounding);
}

From source file:Main.java

public Component getListCellRendererComponent(final JList list, final Object value, final int index,
        final boolean isSelected, final boolean cellHasFocus) {
    return new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font font = (Font) value;
            String text = font.getFamily();
            FontMetrics fm = g.getFontMetrics(font);
            g.setColor(isSelected ? list.getSelectionBackground() : list.getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(isSelected ? list.getSelectionForeground() : list.getForeground());
            g.setFont(font);//from  w  ww  . ja va  2  s. co m
            g.drawString(text, 0, fm.getAscent());
        }

        public Dimension getPreferredSize() {
            Font font = (Font) value;
            String text = font.getFamily();
            Graphics g = getGraphics();
            FontMetrics fm = g.getFontMetrics(font);
            return new Dimension(fm.stringWidth(text), fm.getHeight());
        }
    };
}

From source file:org.lnicholls.galleon.util.Tools.java

public static String[] layout(int width, FontMetrics metrics, String text) {
    ArrayList lines = new ArrayList();

    if (text != null) {
        String line = "";
        BreakIterator boundary = BreakIterator.getWordInstance();
        boundary.setText(text);// ww w  . ja v  a2  s. c o  m
        int start = boundary.first();
        for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
            String word = text.substring(start, end);
            String trimmed = word.replaceAll(" ", "");
            int metricsWidth = (line + word).length() * 20;
            if (metrics != null)
                metricsWidth = metrics.stringWidth(line + word);

            if (trimmed.equals("\n") || trimmed.equals("\r") || trimmed.equals("\r\n")) {
                lines.add(line.trim());
                line = "";
            } else if (metricsWidth > width) {
                lines.add(line.trim());
                line = word;
            } else
                line = line + word;
        }
        if (line.trim().length() > 0)
            lines.add(line.trim());
    }

    return (String[]) lines.toArray(new String[0]);
}

From source file:org.ut.biolab.medsavant.client.view.component.KeyValuePairPanel.java

public void ellipsifyValues(int width) {
    int maxKeyWidth = 0;
    int[] maxAdditionalColumnsWidth = new int[additionalColumns];

    for (JComponent keyComp : keyKeyComponentMap.values()) {
        maxKeyWidth = Math.max(maxKeyWidth, keyComp.getPreferredSize().width);
    }//from www . j a v  a2  s.  c o m

    for (String k : keyValueComponentMap.keySet()) {
        for (int i = 0; i < additionalColumns; i++) {
            JComponent extraComp = getAdditionalColumn(k, i);
            if (extraComp != null) {
                maxAdditionalColumnsWidth[i] = Math.max(maxAdditionalColumnsWidth[i],
                        extraComp.getPreferredSize().width);
            }
        }
    }

    width -= maxKeyWidth;
    for (int i : maxAdditionalColumnsWidth) {
        width -= i;
    }

    for (String k : keyValueComponentMap.keySet()) {
        JComponent comp = getComponent(k);
        if (comp != null) {
            int avail = width;
            /*for (int i = 0; i < additionalColumns; i++) {
             JComponent extraComp = getAdditionalColumn(k, i);
             if (extraComp != null) {
             avail -= extraComp.getPreferredSize().width;
             }
             }*/

            if (comp instanceof JLabel) {

                while (avail < comp.getPreferredSize().width) {
                    String text = ((JLabel) comp).getText();
                    if (text.endsWith("")) {
                        if (text.length() > 2) {
                            // Already truncated.
                            text = text.substring(0, text.length() - 2);
                        } else {
                            break; // As short as we can get.  Can't truncate any more.
                        }
                    } else {
                        // Reasonable first truncation is to trim off the last word.
                        int spacePos = text.lastIndexOf(' ');
                        if (spacePos > 0) {
                            text = text.substring(0, spacePos);
                        } else {
                            FontMetrics fm = comp.getFontMetrics(comp.getFont());

                            while (fm.stringWidth(text + "") > avail) {
                                //causes StringIndexOutOfBoundsException if text is empty.  
                                if (text == null || text.length() < 2) {
                                    LOG.info("Text is null or empty in KeyValuePairPanel");
                                    break;
                                }
                                text = text.substring(0, text.length() - 2);
                            }
                            //text = text + "";

                            //text = text.substring(0, text.length() - 1);
                        }
                    }
                    ((JLabel) comp).setText(text + "");
                }
            } else {
                // Can't truncate, but we can force the preferred size.
                comp.setMaximumSize(new Dimension(avail, comp.getPreferredSize().height));
            }
        }
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreeRenderer.java

@Override
public Dimension getPreferredSize() {
    //System.out.println("-------------------");
    Dimension dim = super.getPreferredSize();
    FontMetrics fm = this.getFontMetrics(getFont());

    dim.height = IconManager.STD_ICON_SIZE.Std24.size() + 2;

    int width = 10;
    if (img1 != null) {
        width += img1.getIconWidth();/*ww  w .  j  ava2 s .c o  m*/
    }

    if (txt1 != null) {
        width += getIconTextGap();
        width += fm.stringWidth(txt1);
    }

    if (img2 != null) {
        width += iconSep;
        width += img2.getIconWidth() + 1;
    }

    if (txt2 != null) {
        width += getIconTextGap();
        width += fm.stringWidth(txt2);
    }
    dim.width = width;

    // Add Space for Edit Icons
    int numIcons = 0;
    if (isColObj) {
        numIcons = 1;

    } else if (isViewMode) {
        numIcons = 2;

    } else if (isEditable) {
        numIcons = 2;
    }
    dim.width += (IconManager.IconSize.Std16.size() * numIcons) + (iconSep * numIcons);

    //System.out.println(txt1+"/"+txt2+"  "+dim.width+"      "+numIcons);
    //System.out.println("-------------------\n");

    return dim;
}