super.paintComponent(g) 有什么作用? [英] What does super.paintComponent(g) do?

查看:78
本文介绍了super.paintComponent(g) 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

super.paintComponent(g) 做了什么(特别是当我们把它放在paintComponent() 方法中时)?我很惊讶我之前在 SO 中没有看到有人问过这个问题.

我翻出了我在 Java Graphics 上的学校笔记,它在这行代码中唯一提到的是不要删除".

然而,这几周我一直在练习和使用 Java 的paintComponent() 方法.到目前为止,我还没有将该行包含在我的代码中,而且一切似乎都运行良好(到目前为止).所以..

问题:

  1. 它有什么作用?
  2. 我们什么时候需要使用它?
  3. 把它写在paintComponent()中给我们带来什么好处?

解决方案

  1. 它有什么作用?

它打印组件就像您没有覆盖 paintComponent 方法一样.例如,如果您设置了背景颜色,则这通常由您要扩展的类绘制.

<块引用>

  1. 我们什么时候需要使用它?

如果您不在整个组件上绘画,则可以使用它.你不涂漆的部分会发光",这意味着你应该让超类来涂刷这些部分.以背景色为例:如果你只是在组件的中间画一个圆圈,super.paintComponent 会确保背景色是围绕着圆圈画的.

如果您确实绘制了组件的整个区域,那么您将绘制在 super.paintComponent 绘制的任何内容之上,因此调用 super.paintComponent 毫无意义.

<块引用>

  1. 把它写在paintComponent()中给我们带来什么好处?

这是唯一合乎逻辑的地方.paintComponent 在组件应该被绘制的时候被调用,而且,如上所述,如果你不自己绘制整个组件,你需要 super.paintComponent 在组件上绘制发光的部分.

文档 paintComponent 说的很好:

<块引用>

[...] 如果你不调用 super 的实现,你必须尊重 opaque 属性,也就是说,如果这个组件是不透明的,你必须用非不透明的颜色完全填充背景.如果您不遵守不透明属性,您可能会看到视觉伪影.

What does super.paintComponent(g) do (especially when we place it inside paintComponent() method)? I am surprised I don't see anyone asking this in SO before.

I dig out my school notes on Java Graphics, the only thing it mentioned on this line of code is "do not delete".

However I have been practicing and tying out on Java paintComponent() method these few weeks. So far I have not included that line into my codes, and everything seems to work well (so far). So..

Questions:

  1. What does it do?
  2. When do we need to use it?
  3. What advantage does it gives us by writing it in paintComponent()?

解决方案

  1. What does it do?

It prints the component as if you hadn't overridden the paintComponent method. If you have a background color set for instance, this is typically painted by the class you're extending.

  1. When do we need to use it?

You use it if you don't paint on the entire component. The parts that you don't paint will "shine through" which means that you should let the super class paint those parts. As with the example of the background color for instance: If you just paint a circle in the middle of the component, super.paintComponent will make sure the background color is painted around the circle.

If you do paint the entire area of your component, then you will paint on top of whatever super.paintComponent paints and thus there's no point in calling super.paintComponent.

  1. What advantage does it gives us by writing it in paintComponent()?

That's the only logical place to put it. paintComponent is called when the component should be painted, and, as mentioned above, if you don't paint the entire component yourself, you need super.paintComponent to paint on the parts that shine through.

The documentation of paintComponent says it pretty well:

[...] if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

这篇关于super.paintComponent(g) 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆