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

public static void main(String[] args) {
    SplashScreen splashScreen = SplashScreen.getSplashScreen();
    Dimension size = splashScreen.getSize();
    int borderDim = (int) (size.height * 0.05);
    Graphics g = splashScreen.createGraphics();
    g.setColor(Color.blue);/* w  w  w  .jav  a 2 s . co  m*/
    for (int i = 0; i < borderDim; i++)
        g.drawRect(i, i, size.width - 1 - i * 2, size.height - 1 - i * 2);

    FontMetrics fm = g.getFontMetrics();
    int sWidth = fm.stringWidth("Initializing...");
    int sHeight = fm.getHeight();
    if (sWidth < size.width && 2 * sHeight < size.height) {
        g.setColor(Color.blue);
        g.drawString("Initializing...", (size.width - sWidth) / 2, size.height - 2 * sHeight);
    }

    splashScreen.update();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document(new Rectangle(100, 100));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("sun_tutorial_with_text.pdf"));
    document.open();/*from  w  ww  .j  ava2s.  c  om*/
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 100);
    DefaultFontMapper mapper = new DefaultFontMapper();
    mapper.insertDirectory("c:/windows/fonts");
    String name;
    Map map = mapper.getMapper();
    for (Iterator i = map.keySet().iterator(); i.hasNext();) {
        name = (String) i.next();
        System.out.println(name + ": " + ((DefaultFontMapper.BaseFontParameters) map.get(name)).fontName);
    }
    Graphics2D g2 = tp.createGraphics(100, 100, mapper);
    g2.setColor(Color.black);
    java.awt.Font thisFont = new java.awt.Font("Garamond", java.awt.Font.PLAIN, 18);
    g2.setFont(thisFont);
    String pear = "Pear";
    FontMetrics metrics = g2.getFontMetrics();
    int width = metrics.stringWidth(pear);
    g2.drawString(pear, (100 - width) / 2, 20);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
    document.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String text = "java2s.com";

    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 48);
    g2d.setFont(font);//  w ww.j av a2  s.c  o  m
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();

    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    ImageIO.write(img, "png", new File("Text.png"));

}

From source file:WriteImageType.java

static public void main(String args[]) throws Exception {
    try {// ww w  .  j  a  v a  2s  .  c  o m
        int width = 200, height = 200;

        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
        // into integer pixels
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D ig2 = bi.createGraphics();

        Font font = new Font("TimesRoman", Font.BOLD, 20);
        ig2.setFont(font);
        String message = "www.java2s.com!";
        FontMetrics fontMetrics = ig2.getFontMetrics();
        int stringWidth = fontMetrics.stringWidth(message);
        int stringHeight = fontMetrics.getAscent();
        ig2.setPaint(Color.black);
        ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

        ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
        ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
        ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
        ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));

    } catch (IOException ie) {
        ie.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 300);//from  www. j  a va2  s . c  o  m

    JPanel panel = new JPanel(new GridLayout(3, 1));
    JLabel label = new JLabel();
    JTextField tf = new JTextField();
    JButton b = new JButton("calc sting width");
    b.addActionListener(e -> {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        String text = tf.getText();
        int textWidth = fm.stringWidth(text);
        label.setText("text width for \"" + text + "\": " + textWidth);
    });
    panel.add(label);
    panel.add(tf);
    panel.add(b);
    frame.setContentPane(panel);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Returns the width of the passed in String.
 *
 * @param c JComponent that will display the string, may be null
 * @param fm FontMetrics used to measure the String width
 * @param string String to get the width of
 * @return the int//from w w  w. j a  v a  2  s  . co  m
 */
public static int stringWidth(JComponent c, FontMetrics fm, String string) {
    return fm.stringWidth(string);
}

From source file:Main.java

public static void drawCenteredString(String string, int width, int height, Graphics graphics) {
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int x = (width - fontMetrics.stringWidth(string)) / 2;
    int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2);
    graphics.drawString(string, x, y);/*from w w w. j a v  a 2s  . c  o m*/
}

From source file:Main.java

/**
 * Draw a string centered within a rectangle.  The string is drawn using the graphics context's
 * current font and color.//ww  w.jav a2 s.  c o m
 *
 * @param g the graphics context.
 * @param str the string.
 * @param x the bounding x position.
 * @param y the bounding y position.
 * @param width the bounding width.
 * @param height the bounding height.
 */
public static void drawStringCentered(Graphics g, String str, int x, int y, int width, int height) {
    FontMetrics fm = g.getFontMetrics(g.getFont());
    int xpos = x + ((width - fm.stringWidth(str)) / 2);
    int ypos = y + ((height + fm.getAscent()) / 2);
    g.drawString(str, xpos, ypos);
}

From source file:Main.java

/**
 * Returns the width of the passed in String. If the passed String is null,
 * returns zero./*from  w ww.  ja  va 2  s.co  m*/
 *
 * @param fm
 *            FontMetrics used to measure the String width
 * @param string
 *            String to get the width of
 */
public static int stringWidth(final FontMetrics fm, final String string) {
    if (string == null || string.equals("")) {
        return 0;
    }
    return fm.stringWidth(string);
}

From source file:Main.java

/**
 * Paints string with underlined character at the specified index.
 *
 * @param g               graphics context
 * @param text            painted text/*w w  w  .j a  va2  s.  com*/
 * @param underlinedIndex underlined character index
 * @param x               text X coordinate
 * @param y               text Y coordinate
 */
public static void drawStringUnderlineCharAt(final Graphics g, final String text, final int underlinedIndex,
        final int x, final int y) {
    // Painting string
    drawString(g, text, x, y);

    // Painting character underline
    if (underlinedIndex >= 0 && underlinedIndex < text.length()) {
        final FontMetrics fm = g.getFontMetrics();
        g.fillRect(x + fm.stringWidth(text.substring(0, underlinedIndex)), y + fm.getDescent() - 1,
                fm.charWidth(text.charAt(underlinedIndex)), 1);
    }
}