java.awt.FontMetrics#charsWidth ( )源码实例Demo

下面列出了java.awt.FontMetrics#charsWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SmartIM   文件: PopupPanel.java
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());
}
 
源代码2 项目: Bytecoder   文件: SwingUtilities2.java
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);
    }
}
 
源代码3 项目: maven-framework-project   文件: TextImageUtils.java
/**
 * 生成电子邮件图片
 * @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);
}
 
源代码4 项目: maven-framework-project   文件: TextImageUtils.java
/**
 * 生成电话号码图片
 * @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);
}
 
源代码5 项目: maven-framework-project   文件: TextImageUtils.java
/**
 * 生成产品关键特征
 * @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);
}
 
源代码6 项目: dragonwell8_jdk   文件: TextMeasureTests.java
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);
}
 
源代码7 项目: TencentKona-8   文件: TextMeasureTests.java
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);
}
 
源代码8 项目: jdk8u60   文件: TextMeasureTests.java
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);
}
 
源代码9 项目: openjdk-jdk8u   文件: TextMeasureTests.java
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);
}
 
源代码10 项目: openjdk-jdk8u-backup   文件: TextMeasureTests.java
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);
}
 
源代码11 项目: openjdk-jdk9   文件: TextMeasureTests.java
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);
}
 
源代码12 项目: jdk8u-jdk   文件: TextMeasureTests.java
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);
}
 
源代码13 项目: hottub   文件: TextMeasureTests.java
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);
}
 
源代码14 项目: openjdk-8-source   文件: TextMeasureTests.java
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);
}
 
源代码15 项目: openjdk-8   文件: TextMeasureTests.java
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);
}
 
源代码16 项目: jdk8u_jdk   文件: TextMeasureTests.java
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);
}
 
源代码17 项目: jdk8u-jdk   文件: TextMeasureTests.java
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);
}
 
源代码18 项目: jdk8u-dev-jdk   文件: TextMeasureTests.java
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);
}
 
@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]"));
}