我需要在屏幕上绘制一些东西,但这需要字体度量才能正确绘制。我还需要使用repaint()或其他工具来立即绘制到屏幕上。
如果我有一个paintComponent(Graphics)方法,我可以通过g.getFontMetrics(g.getFont())正确地检索字体度量。问题是,我不能告诉它去画它自己。它只在发生某些事情时才会这样做,比如调整组件的大小。
然后,如果我只使用普通的paint(Graphics),我可能会在我想要的时候使用repaint()来绘制,但是调用g.getFontMetrics(g.getFont())不会返回正确的值。有什么想法吗?
repaint();//I need to call repaint() or something similar to draw to the screen when I want it to
public void paint(Graphics g){
FontMetrics metrics = g.getFontMetrics(g.getFont());//Returns different metrics than that of paintComponent(Graphics)
}
public void paintComponent(Graphics g){
FontMetrics metrics = g.getFontMetrics(g.getFont());//Returns different metrics than that of paint(Graphics)
}发布于 2014-10-01 21:09:36
问题是,我不能告诉它去画它自己。
。
您只需在组件上使用repaint()。repaint()方法将调用paint(),而paint()又调用paintComponent()。有关详细信息,请参阅A Closer Look at the Paint Mechanism。
我将其设置为变量并使用该字体对象,而不是使用g.getFont()
从上面给出的教程链接中,你会发现你应该重写paintComponent()而不是paint(),所以这不是问题。
https://stackoverflow.com/questions/26140595
复制相似问题