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

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());// w w  w.ja v  a2s  .  co m
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    calendar.setTime(new Date()); // get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());
    if (isDigital) {
        String time = "" + hrs + ":" + min;
        g.setFont(getFont());
        FontMetrics fm = g.getFontMetrics();
        int y = (getHeight() + fm.getAscent()) / 2;
        int x = (getWidth() - fm.stringWidth(time)) / 2;
        g.drawString(time, x, y);
    } else {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        int rh = getHeight() / 4;
        int rm = getHeight() / 3;

        double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
        double am = min / 30.0 * Math.PI;

        g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
        g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
    }

    g.setColor(colorRetainer);
}

From source file:sagan.projects.support.VersionBadgeService.java

private byte[] createSvgBadge(BadgeSvg svgDocument, List<Path> paths, String label, String version) {

    List<GraphicElement> graphicElements = svgDocument.getGraphicElements();
    graphicElements.get(0).setText(label);
    graphicElements.get(1).setText(label);

    graphicElements.get(2).setText(version);
    graphicElements.get(3).setText(version);

    int labelMarginLeft = 6;
    int labelMarginRight = 4;

    int versionMarginLeft = 4;
    int versionMarginRight = 6;

    int labelTextWidth;
    int versionTextWidth;

    synchronized (graphics) {
        FontMetrics fontMetrics = graphics.getFontMetrics();
        labelTextWidth = fontMetrics.stringWidth(label);
        versionTextWidth = fontMetrics.stringWidth(version);
    }//w  ww. j  a  va 2  s . c  o m

    int labelWidth = labelTextWidth + labelMarginLeft + labelMarginRight;
    int versionWidth = versionTextWidth + versionMarginLeft + versionMarginRight;

    svgDocument.setWidth(labelWidth + versionWidth);
    svgDocument.getMask().setWidth(labelWidth + versionWidth);

    graphicElements.get(0).setXPosition(labelMarginLeft + (labelTextWidth / 2));
    graphicElements.get(1).setXPosition(labelMarginLeft + (labelTextWidth / 2));

    graphicElements.get(2).setXPosition(labelWidth + versionMarginLeft + (versionTextWidth / 2));
    graphicElements.get(3).setXPosition(labelWidth + versionMarginLeft + (versionTextWidth / 2));

    paths.get(0).setDraw(String.format("M0 0h%dv20H0z", labelWidth));
    paths.get(1).setDraw(String.format("M%d 0h%dv20H%dz", labelWidth, versionWidth, labelWidth));
    paths.get(2).setDraw(String.format("M0 0h%dv20H0z", labelWidth + versionWidth));

    return svgDocument.toString().getBytes();
}

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  va2 s  . c  om*/
    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:JapaneseCalendar.java

public void paintComponent(Graphics g) {
    int width = 400;
    int height = 400;

    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(new Date());

    String header = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);
    header += " " + cal.get(Calendar.YEAR);

    FontMetrics fm = g.getFontMetrics();
    Insets insets = getInsets();/*from   www  .j av a  2s  . c om*/
    g.setColor(Color.black);
    g.drawString(header, (width - fm.stringWidth(header)) / 2, insets.top + fm.getHeight());

    DateFormatSymbols dfs = new DateFormatSymbols(locale);
    String[] weekdayNames = dfs.getShortWeekdays();
    int fieldWidth = (width - insets.left - insets.right) / 7;
    g.drawString(weekdayNames[Calendar.SUNDAY],
            insets.left + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SUNDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.MONDAY],
            insets.left + fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.MONDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.TUESDAY],
            insets.left + 2 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.TUESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.WEDNESDAY],
            insets.left + 3 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.WEDNESDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.THURSDAY],
            insets.left + 4 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.THURSDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.FRIDAY],
            insets.left + 5 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.FRIDAY])) / 2,
            insets.top + 3 * fm.getHeight());
    g.drawString(weekdayNames[Calendar.SATURDAY],
            insets.left + 6 * fieldWidth + (fieldWidth - fm.stringWidth(weekdayNames[Calendar.SATURDAY])) / 2,
            insets.top + 3 * fm.getHeight());

    int dom = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int col = 0;
    switch (cal.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY:
        col = 1;
        break;

    case Calendar.TUESDAY:
        col = 2;
        break;

    case Calendar.WEDNESDAY:
        col = 3;
        break;

    case Calendar.THURSDAY:
        col = 4;
        break;

    case Calendar.FRIDAY:
        col = 5;
        break;

    case Calendar.SATURDAY:
        col = 6;
    }
    cal.set(Calendar.DAY_OF_MONTH, dom);

    int row = 5 * fm.getHeight();
    for (int i = 1; i <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
        g.drawString("" + i, insets.left + fieldWidth * col + (fieldWidth - fm.stringWidth("" + i)) / 2, row);
        if (++col > 6) {
            col = 0;
            row += fm.getHeight();
        }
    }
}

From source file:edu.kit.dama.ui.components.TextImage.java

/**
 * Get the bytes of the final image./*w w w.  j ava 2 s. c o  m*/
 *
 * @return The byte array containing the bytes of the resulting image.
 *
 * @throws IOException if creating the image fails.
 */
public byte[] getBytes() throws IOException {
    Image transparentImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(
            new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB).getSource(), new RGBImageFilter() {
                @Override
                public final int filterRGB(int x, int y, int rgb) {
                    return (rgb << 8) & 0xFF000000;
                }
            }));

    //create the actual image and overlay it by the transparent background
    BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = outputImage.createGraphics();
    g2d.drawImage(transparentImage, 0, 0, null);
    //draw the remaining stuff
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setColor(color);
    g2d.fillRoundRect(0, 0, size, size, 20, 20);
    g2d.setColor(new Color(Math.round((float) color.getRed() * .9f), Math.round((float) color.getGreen() * .9f),
            Math.round((float) color.getBlue() * .9f)));
    g2d.drawRoundRect(0, 0, size - 1, size - 1, 20, 20);

    Font font = new Font("Dialog", Font.BOLD, size - 4);
    g2d.setFont(font);
    g2d.setColor(Color.WHITE);

    String s = text.toUpperCase().substring(0, 1);
    FontMetrics fm = g2d.getFontMetrics();
    float x = ((float) size - (float) fm.stringWidth(s)) / 2f;
    float y = ((float) fm.getAscent()
            + (float) ((float) size - ((float) fm.getAscent() + (float) fm.getDescent())) / 2f) - 1f;
    g2d.drawString(s, x, y);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ImageIO.write(outputImage, "png", bout);
    g2d.dispose();
    return bout.toByteArray();
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*from w w  w. j a va 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 Short) {
        Short val = (Short) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else 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 ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*from w  w  w  .  ja va2  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 Integer) {
        Integer val = (Integer) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else 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 ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*from   w w  w  . ja  v a 2  s  .c om*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof Long) {
        Long val = (Long) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else 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 ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;//w w w  .  j  a v  a 2s. 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 Double) {
        Double val = (Double) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else 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 ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*www. 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 Float) {
        Float val = (Float) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } else 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 ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}