Java:更好的方法来获取FontMetrics的一个实例 [英] Java: Friendlier way to get an instance of FontMetrics
问题描述
有更好的方式来获取FontMetrics的实例比
FontMetrics fm = Graphics.getFontMetrics(Font);
由于以下示例,我讨厌这样:
如果要在游戏中创建一个菜单,并且您希望屏幕中心的所有菜单都需要fontmetrics。但是,大多数情况下,menuitems是可点击的。所以我创建一个数组矩形
,所有的矩形都围绕项目,所以当鼠标按下时,我可以简单地使用
$ b对于(int i = 0; i
..}
但是要创建rects,我也需要FontMetrics的坐标。所以这意味着我必须在我的菜单的paint-method中构造所有的矩形。
所以我想要一种获取FontMetrics的方法,所以我可以构造矩形在构造函数调用的方法中。
真正正确的答案是使用Toolkit。
Font font = new Font(Courier New,Font.PLAIN,14);
FontMetrics fm = Toolkit.getDefaultToolkit()。getFontMetrics(font);
Is there a friendlier way to get an instance of FontMetrics than
FontMetrics fm = Graphics.getFontMetrics(Font);
I hate this way because of the following example:
If you want to create in a game a menu and you want all the menuitems in the center of the screen you need fontmetrics. But, mostly, menuitems are clickable. So I create an array of Rectangles
and all the rectangles fits around the items, so when the mouse is pressed, I can simply use
for (int i = 0; i < rects.length; i++)
if (rects[i].contains(mouseX, mouseY)) { ... }
But to create the rects I also need FontMetrics for their coordinates. So this mean that I have to construct all my rectangles in the paint-method of my menu.
So I want a way to get the FontMetrics so I can construct the Rectangles in a method called by the constructor.
The really correct answer is to use Toolkit.
Font font = new Font("Courier New", Font.PLAIN, 14);
FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
这篇关于Java:更好的方法来获取FontMetrics的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!