Java Code Examples for java.awt.FontMetrics#charsWidth()

The following examples show how to use java.awt.FontMetrics#charsWidth() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: SmartIM   File: PopupPanel.java    License: Apache License 2.0 6 votes vote down vote up
void setLabelText(JLabel label, String text) {
    StringBuilder builder = new StringBuilder("<html>");
    char[] chars = text.toCharArray();
    FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
    int start = 0;
    int len = 0;
    while (start + len < text.length()) {
        while (true) {
            len++;
            if (start + len > text.length())
                break;
            if (fontMetrics.charsWidth(chars, start, len) > label.getWidth()) {
                break;
            }
        }
        builder.append(chars, start, len - 1).append("<br/>");
        start = start + len - 1;
        len = 0;
    }
    builder.append(chars, start, text.length() - start);
    builder.append("</html>");
    label.setText(builder.toString());
}
 
Example 2
Source Project: Bytecoder   File: SwingUtilities2.java    License: Apache License 2.0 6 votes vote down vote up
public static float getFontCharsWidth(char[] data, int offset, int len,
                                      FontMetrics fm,
                                      boolean useFPAPI)
{
    if (len == 0) {
       return 0;
    }
    if (useFPAPI) {
        Rectangle2D bounds = fm.getFont().
                                 getStringBounds(data, offset, offset + len,
                                                 fm.getFontRenderContext());
        return (float) bounds.getWidth();
    } else {
        return fm.charsWidth(data, offset, len);
    }
}
 
Example 3
Source Project: maven-framework-project   File: TextImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 生成电子邮件图片
 * @param email
 * @param out
 * @throws IOException
 */
public static void MakeEmailImage(String email, OutputStream out) throws IOException {
    int height = 22;
    BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);       
    Graphics2D g = (Graphics2D)bi.getGraphics();
    Font mFont = new Font("Verdana", Font.PLAIN, 14);
    g.setFont(mFont);
    g.drawString(email, 2, 19);
    FontMetrics fm = g.getFontMetrics();
    int new_width = fm.charsWidth(email.toCharArray(), 0, email.length()) + 4;
    int new_height = fm.getHeight();
    BufferedImage nbi = new BufferedImage(new_width, new_height,
        BufferedImage.TYPE_BYTE_INDEXED, icm);
    Graphics2D g2 = (Graphics2D)nbi.getGraphics();
    g2.setColor(new Color(0,0,0,0));//透明
    g2.fillRect(0,0,new_width,new_height);
    g2.setFont(mFont);
    g2.setColor(new Color(200,0,0));
    g2.drawString(email, 2, new_height-4);
 
    ImageIO.write(nbi, "png", out);
}
 
Example 4
Source Project: maven-framework-project   File: TextImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 生成电话号码图片
 * @param phone
 * @param out
 * @throws IOException
 */
public static void MakePhoneImage(String phone, OutputStream out) throws IOException {
    int height = 22;
    BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);       
    Graphics2D g = (Graphics2D)bi.getGraphics();
    Font mFont = new Font("Verdana", Font.BOLD, 20);
    g.setFont(mFont);
    g.drawString(phone, 2, 19);
    FontMetrics fm = g.getFontMetrics();
    int new_width = fm.charsWidth(phone.toCharArray(), 0, phone.length()) + 4;
    int new_height = fm.getHeight();
    BufferedImage nbi = new BufferedImage(new_width, new_height,
        BufferedImage.TYPE_BYTE_INDEXED, icm);
    Graphics2D g2 = (Graphics2D)nbi.getGraphics();
    g2.setColor(new Color(0,0,0,0));//透明
    g2.fillRect(0,0,new_width,new_height);
    g2.setFont(mFont);
    g2.setColor(new Color(200,0,0));
    g2.drawString(phone, 2, new_height-4);     
    ImageIO.write(nbi, "png", out);
}
 
Example 5
Source Project: maven-framework-project   File: TextImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 生成产品关键特征
 * @param attribute
 * @param out
 * @throws IOException
 */
public static void MakeProductAttribute(String attribute, OutputStream out) throws IOException{
    int height = 22;
    BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);       
    Graphics2D g = (Graphics2D)bi.getGraphics();
    Font mFont = new Font("宋体", Font.BOLD, 13);
    g.setFont(mFont);
    g.drawString(new String(attribute), 2, 19);
    FontMetrics fm = g.getFontMetrics();
    int new_width = fm.charsWidth(attribute.toCharArray(), 0, attribute.length()) + 4;
    int new_height = fm.getHeight();
    BufferedImage nbi = new BufferedImage(new_width, new_height,
       BufferedImage.TYPE_BYTE_INDEXED, icm);
    Graphics2D g2 = (Graphics2D)nbi.getGraphics();
    g2.setColor(new Color(0,0,0,0));//透明
    g2.fillRect(0,0,new_width,new_height);
    g2.setFont(mFont);
    g2.setColor(new Color(200,0,0));
    g2.drawString(attribute, 2, new_height-4);
    ImageIO.write(nbi, "png", out);
}
 
Example 6
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 7
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 8
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 9
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 10
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 11
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 12
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 13
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 14
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 15
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 16
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 17
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 18
public void runTest(Object ctx, int numReps) {
    SWContext swctx = (SWContext)ctx;
    FontMetrics fm = swctx.fm;
    char[] chars = swctx.chars;
    int wid = 0;
    do {
        wid += fm.charsWidth(chars, 0, chars.length);
    } while (--numReps >= 0);
}
 
Example 19
@Test
public void imageEmail() throws Exception{
	
	String email = "[email protected]";

	int height = 22;
	BufferedImage bi = new BufferedImage(255, height,
			BufferedImage.TYPE_INT_RGB);
	Graphics2D g = (Graphics2D) bi.getGraphics();
	Font mFont = new Font("Verdana", Font.PLAIN, 14);
	g.setFont(mFont);
	g.drawString(email, 2, 19);
	FontMetrics fm = g.getFontMetrics();
	int new_width = fm.charsWidth(email.toCharArray(), 0, email.length()) + 4;
	int new_height = fm.getHeight();
	BufferedImage nbi = new BufferedImage(new_width, new_height,
			BufferedImage.TYPE_BYTE_INDEXED, createIndexColorModel());
	Graphics2D g2 = (Graphics2D) nbi.getGraphics();
	g2.setColor(new Color(0, 0, 0, 0));// 透明
	g2.fillRect(0, 0, new_width, new_height);
	g2.setFont(mFont);
	g2.setColor(new Color(200, 0, 0));
	g2.drawString(email, 2, new_height - 4);

	ImageIO.write(nbi, "gif", new FileOutputStream(
			"target/[email protected]"));
}