简体   繁体   English

Java中的paint()和repaint()

[英]paint() and repaint() in Java

I've spent maybe the last two hours browsing and reading up on these methods and the Graphics class, and maybe I'm stupid, haha, but I'm just not understanding them. 我花了最近两个小时浏览和阅读这些方法和Graphics类,也许我很蠢,哈哈,但我只是不理解它们。 What are they for? 它们适用于什么? I understand that they're supposed redraw or update components on the screen, but I have never understood why this is required (I'm new to this). 我知道他们应该在屏幕上重绘或更新组件,但我从未理解为什么这是必需的(我是新手)。 For example, if I'm moving a JLabel around the screen, a call to setLocation() moves it just fine. 例如,如果我在屏幕上移动JLabel,则调用setLocation()会将其移动得很好。 Is that a scenario in which repaint() isn't required? 这是不需要repaint()的场景吗? In which scenarios is it useful, and why? 它在哪些场景中有用,为什么?

Apologies if you feel that this is a question that could be solved using the search function, but for whatever reason I'm not getting it. 如果您认为这是一个可以使用搜索功能解决的问题,请道歉,但无论出于何种原因,我都没有得到它。

Difference between Paint() and Repaint() method Paint()和Repaint()方法之间的区别

Paint(): 涂料():

This method holds instructions to paint this component. 此方法包含绘制此组件的说明。 Actually, in Swing, you should change paintComponent() instead of paint(), as paint calls paintBorder(), paintComponent() and paintChildren(). 实际上,在Swing中,你应该改变paintComponent()而不是paint(),因为paint调用paintBorder(),paintComponent()和paintChildren()。 You shouldn't call this method directly, you should call repaint() instead. 你不应该直接调用这个方法,你应该调用repaint()。

Repaint(): 重绘():

This method can't be overridden. 此方法无法覆盖。 It controls the update() -> paint() cycle. 它控制update() - > paint()循环。 You should call this method to get a component to repaint itself. 您应该调用此方法来获取重新绘制自身的组件。 If you have done anything to change the look of the component, but not its size ( like changing color, animating, etc. ) then call this method. 如果您已经做了任何改变组件外观的事情,而不是它的大小(比如改变颜色,动画等),那么请调用此方法。

The paint() method supports painting via a Graphics object. paint()方法支持通过Graphics对象进行绘制。

The repaint() method is used to cause paint() to be invoked by the AWT painting thread. repaint()方法用于使AWT绘制线程调用paint()

It's not necessary to call repaint unless you need to render something specific onto a component. 除非您需要在组件上呈现特定内容,否则不必调用重绘。 "Something specific" meaning anything that isn't provided internally by the windowing toolkit you're using. “特定的东西”是指您正在使用的窗口工具包内部未提供的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM