如何在 java2D 中获取 Font X 偏移宽度?
打印两个单词:“A”和“B”
我需要使用 java 2D字体大小 = 100
; “A”字体系列:Bodoni MT Poster 压缩
“B”字体系列:Arial
我编写了下面的代码来做到这一点:
BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
{//fill bg color
g.setColor(new Color(255,255,255));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
int FONT_SIZE=100;//set font size
{//print A
g.setColor(new Color(0,0,0));
g.setFont(new Font("Bodoni MT Poster Compressed", Font.PLAIN ,FONT_SIZE));
g.drawString("A",0,FONT_SIZE);
}
{//print B
g.setColor(new Color(0,0,0));
g.setFont(new Font("Arial", Font.PLAIN ,FONT_SIZE));
g.drawString("B",FONT_SIZE,FONT_SIZE);
}
g.dispose();
我得到结果图像:
但我需要这样的(由 PhotoShop 制作):
我认为问题在 g.drawString("B",FONT_SIZE,FONT_SIZE);
如何获取字体 X 偏移宽度?
感谢您的帮助:)
I need print two words: "A" and "B" using java 2D
font size = 100;
"A" font family: Bodoni MT Poster Compressed
"B" font family: Arial
I writed below codes to do it:
BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
{//fill bg color
g.setColor(new Color(255,255,255));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
int FONT_SIZE=100;//set font size
{//print A
g.setColor(new Color(0,0,0));
g.setFont(new Font("Bodoni MT Poster Compressed", Font.PLAIN ,FONT_SIZE));
g.drawString("A",0,FONT_SIZE);
}
{//print B
g.setColor(new Color(0,0,0));
g.setFont(new Font("Arial", Font.PLAIN ,FONT_SIZE));
g.drawString("B",FONT_SIZE,FONT_SIZE);
}
g.dispose();
I get the result image:
but I need like this (make by PhotoShop):
I think the question at g.drawString("B",FONT_SIZE,FONT_SIZE);
How can I get the font X offset width?
thanks for help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行 setFont 后,声明一个变量,例如
现在您已经有了字母的所有尺寸,设置它们的位置(px 是从左边缘到字母的距离,py 从顶部到字体基线的距离)
,类似地“B”。希望有帮助,- MS
After you do the setFont, declare a variable like
Now that you have all the dimensions of the letters, set their positions (px is distance from the left edge to the letter, py from the top to the baseline of the font)
And similarly for "B". Hope that helps, - M.S.