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

The following examples show how to use java.awt.FontMetrics#charsWidth() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TextImageUtils.java    From maven-framework-project with 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 2
Source File: TextImageUtils.java    From maven-framework-project with 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 3
Source File: PopupPanel.java    From SmartIM with 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 4
Source File: TextImageUtils.java    From maven-framework-project with 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 5
Source File: SwingUtilities2.java    From Bytecoder with 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 6
Source File: TextMeasureTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: EmpRepositoryTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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]"));
}
 
Example 8
Source File: TextMeasureTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TextMeasureTests.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}