Java Paint() 和 repaint() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22501630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between paint() and repaint()
提问by Parth
I am a novice Java programmer and I'm finding quite some difficulty in figuring out the actual difference between the functioning of the paint()
and repaint()
method.
Also in some of the programs that I studied I found paint()
and repaint()
interchangeable.
Can someone please explain the difference? Thank you
我是一名 Java 新手程序员,我发现很难弄清楚paint()
和repaint()
方法的功能之间的实际差异。
此外,在一些节目,我研究,我发现paint()
和repaint()
互换。有人可以解释一下区别吗?谢谢
采纳答案by Marco13
Assuming that you are referring to the void paint(Graphics g)
method that is declared in the Component
class:
假设您指的void paint(Graphics g)
是在Component
类中声明的方法:
This paint
method is called automatically, whenever it is necessary to paint (parts of) the component. For example, when the window was obstructed by another window and then becomes visible again: The window manager will determine this, and call paint
on the top level component (e.g. a Frame) and this call will make its way down to the actual "bottom" components (e.g. a Button). The Graphics
object that is passed to this method is provided by the Window manager, and corresponds to the area on the screen where the component should be painted. (And this Graphics
object is only valid during the paint
method).
paint
每当需要绘制(部分)组件时,都会自动调用此方法。例如,当窗口被另一个窗口挡住然后又变得可见时:窗口管理器将确定这一点,并调用paint
顶层组件(例如框架),此调用将向下到达实际的“底部”组件(例如按钮)。Graphics
传递给此方法的对象由窗口管理器提供,对应于屏幕上应绘制组件的区域。(并且此Graphics
对象仅在paint
方法期间有效)。
In contrast to that, repaint()
just triggersa new painting process. It is telling the system: "Please call paint
on this component as soon as possible". You can call this method manually. And you can call it freuqently: The calls to repaint
are coalesced. That means that when you issue manycalls to repaint
in a short period of time, then these calls may be summarized and may eventually only trigger onecall to paint
.
与此相反,repaint()
只是触发一个新的绘画过程。它告诉系统:“请paint
尽快调用此组件”。您可以手动调用此方法。你可以经常调用它:调用repaint
是合并的。这意味着,当您在短时间内发出多次调用repaint
时,这些调用可能会被汇总,最终可能仅触发一次对 的调用paint
。
回答by AJ.
paint()
get called automatically at runtime. If you want to call paint()
manually(again) then repaint()
is used.
paint()
在运行时自动调用。如果您想paint()
手动(再次)调用, repaint()
则使用。
回答by LaurentG
The paint
method should not be called directly as the javadoc states:
paint
不应直接调用该方法,如 javadoc 所述:
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.
由 Swing 调用以绘制组件。应用程序不应直接调用paint,而应使用repaint 方法来安排组件的重绘。
The repaint
method should be used instead if you want the component to be repainted (redrawn). The javadoc also refers to the following documentation: Painting in AWT and Swing
repaint
如果您希望重新绘制(重绘)组件,则应改用该方法。javadoc 还参考了以下文档:在 AWT 和 Swing 中绘画
回答by Anto
The paint()method contains instructions for painting the specific component.
该涂料()方法中包含了绘画的具体组件的说明。
The repaint()method, which can't be overridden, is more specific: it controls the update() to paint() process. You should call this method if you want a component to repaint itself or to change its look (but not the size).
无法覆盖的repaint()方法更具体:它控制 update() 到paint() 过程。如果您希望组件重新绘制自身或更改其外观(但不是大小),则应调用此方法。