下面列出了java.awt.FontMetrics#charsWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
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);
}
}
/**
* 生成电子邮件图片
* @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);
}
/**
* 生成电话号码图片
* @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);
}
/**
* 生成产品关键特征
* @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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
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]"));
}