gpt4 book ai didi

java - 重新绘制() FontMetrics

转载 作者:行者123 更新时间:2023-12-02 05:24:57 24 4
gpt4 key购买 nike

我需要在屏幕上绘制一些内容,但这需要字体规范才能正确绘制。我还需要使用 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)
}

最佳答案

The problem is, I cannot tell it to paint it's self.

您只需在组件上使用repaint()。 repaint() 方法将调用paint(),而paint() 又调用paintComponent()。请参阅A Closer Look at the Paint Mechanism了解更多信息。

I set it to the variable and use that font object instead of using g.getFont()

从上面给出的教程链接中,您将看到您应该重写paintComponent()而不是paint(),所以这不是问题。

关于java - 重新绘制() FontMetrics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26140595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com