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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:05:57  来源:igfitidea点击:

Difference between paint() and repaint()

javaswing

提问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 Componentclass:

假设您指的void paint(Graphics g)是在Component类中声明的方法:

This paintmethod 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 painton 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 Graphicsobject 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 Graphicsobject is only valid during the paintmethod).

paint每当需要绘制(部分)组件时,都会自动调用此方法。例如,当窗口被另一个窗口挡住然后又变得可见时:窗口管理器将确定这一点,并调用paint顶层组件(例如框架),此调用将向下到达实际的“底部”组件(例如按钮)。Graphics传递给此方法的对象由窗口管理器提供,对应于屏幕上应绘制组件的区域。(并且此Graphics对象仅在paint方法期间有效)。

In contrast to that, repaint()just triggersa new painting process. It is telling the system: "Please call painton this component as soon as possible". You can call this method manually. And you can call it freuqently: The calls to repaintare coalesced. That means that when you issue manycalls to repaintin 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 paintmethod 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 repaintmethod 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() 过程。如果您希望组件重新绘制自身或更改其外观(但不是大小),则应调用此方法。