paint
方法:
Invoked by Swing to draw components. Applications should not invoke paint directly,
but should instead use the repaint method to schedule the component for redrawing.
This method actually delegates the work of painting to three protected methods:
paintComponent, paintBorder, and paintChildren. They're called in the order listed
to ensure that children appear on top of component itself. Generally speaking,
the component and its children should not paint in the insets area allocated to the
border. Subclasses can just override this method, as always. A subclass that just
wants to specialize the UI (look and feel) delegate's paint method should just
override paintComponent.
Parameters:
g the Graphics context in which to paint
public void paint(Graphics g)
我读过很多次不是要覆盖paint()
,而是要覆盖paintComponent()
。如上所示,paintComponent()
如果您想专门化 UI,文档还建议覆盖。
所以,我想通过代码来了解为什么会这样。
protected void paintComponent(Graphics g)
{
if (ui != null)
{
Graphics scratchGraphics = (g == null) ? null : g.create();
try
{
ui.update(scratchGraphics, this);
}
finally
{
scratchGraphics.dispose();
}
}
}
有很多方法可以追溯,但为了简洁起见,这里是update()
:
public void update(Graphics g, JComponent c)
{
if (c.isOpaque())
{
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
的重载版本paint()
被调用,并且paint()
本身调用paintComponent()
. 所以,我想我只是想弄清楚这里到底发生了什么。我对 Swing 很陌生;有很多类,很难追踪所有类,尤其是在我对使用 GUI 的了解有限的情况下。
这些方法是否不断地相互调用,从而在屏幕上产生图像的错觉?如果是这样,为什么覆盖paint()
而不是那么重要paintComponent
?我想我的逻辑在某种程度上存在缺陷或缺乏,考虑到对于应用程序,文档建议不要paint()
直接调用,而是建议调用repaint()
. 所以,基本上,我只是想深入了解 , , 等之间的paint()
整体paintComponent()
关系repaint()
。