为什么必须使用paintComponent方法在Java中绘制? [英] Why does one have to use the paintComponent method to draw in Java?

查看:155
本文介绍了为什么必须使用paintComponent方法在Java中绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个问题不会被视为过于笼统。我理解,要在 JPanel 上绘制,您可以覆盖 paintComponent 方法,并将所有绘图代码放在该方法中。我的问题是为什么!为什么Java似乎不允许/提供使用 panel.drawLine(x1,y1,x2,y2) panel.drawText等方法的绘图(文字,X,Y)?在delphi中这一切都变得如此简单。必须有一个我无法理解的原因。

I hope this question is not going to be regarded as too general. I understand that to draw on a JPanel you override the paintComponent method and place all your drawing code within that method. My question is why! Why does Java not seem to allow/provide for drawing with methods such as panel.drawLine(x1,y1,x2,y2) or panel.drawText(text,x,y)? It was all so much easier in delphi. There must be a reason I just can't figure it out.

推荐答案

那是因为它的工作方式。它是这样设计的。但我想你的问题是关于为什么

That's because that's the way it works. It was designed this way. But I guess your question is about "why"

请记住,Swing在15年前首次问世。其中一个批评是API很慢(事实是,它很慢,因为人们并没有真正理解如何使用它,但这是另一个故事),所以API必须在设计时考虑到性能。

Remember, Swing first came out nearly 15 years ago. One of the criticisms was that the API was slow (the fact was, it was slow because people didn't truly understand how to use it, but that's another story), so the API had to be designed with performance in mind.

涉及多种因素...

Swing使用被动绘画流程,这意味着绘画请求在油漆子系统和时间表(回到EDT)进行处理。 paint子系统决定应该绘制什么,何时以及绘制多少。这是在油漆子系统的亵渎时完成的。

Swing uses a passive paint process, meaning that the paint requests are made to the paint sub system and schedule (back on the EDT) for processing. The paint sub system makes decisions about what, when and how much should be painted. This is done at the desecration of the paint sub system.

这意味着你真的不知道什么时候可以执行油漆循环,所以我们需要一些方法来回应这些要求。

This means that you never really know when a paint cycle may be executed, so we need some way to be able to respond to these requests.

多功能性是另一个因素。 API足够抽象,并且(很多)组件被绘制的地方无关紧要。也就是说,您可能被画到屏幕,打印机甚至图像上。这意味着您不必重复大量的绘制代码,以使其在不同的设备上运行。

Versatility is another factor. The API is abstract enough that it doesn't matter (a lot), where the component is being painted to. That is, you could be being painted to the screen, printer or even a image. This means you don't have to repeat a lot of paint code to make it work on different devices.

您也永远不知道组件何时可显示(即,当它变得附加到本地对等体时)。这意味着图形上下文可能是 null ,因此使用帮助方法实际上可能会导致更多问题。当调用 paintComponent 时,(大多数)保证您有一个有效的图形上下文来绘制。

You also never know when a component will become displayable (that is, when it becomes attached to a native peer). This means that the graphics context may be null, so having "helper" methods may actually cause more problems. When paintComponent is called, you are (mostly) guaranteed to have a valid graphics context to paint to.

可扩展性将是另一个因素。不仅可以轻松覆盖 paintComponent 来改变某些组件的绘制方式,而且绘制系统也可以提供扩展的图形上下文,就像当前的情况一样。当 paintComponent 被调用时(至少由paint子系统调用),它保证 Graphics 上下文将是一个实例 Graphics2D ,是 Graphics 的扩展,为API提供了许多重要的增强功能。

Extendability would be another factor. Not only is it very easy to override the paintComponent to alter the way some component paints, it's also possible for the paint system to provide a extended Graphics context, as is the current case. When paintComponent is called (by the paint sub system at least), it guarantees that the Graphics context will be an instance of Graphics2D, which is an extension to Graphics, providing a number of important enhancements to the API.

这一切都是在不需要更改人们正在使用的基类的情况下完成的,因此如果他们不想使用这些功能,他们就不会受到它们的影响。

This is all done without the need to change the base class which people are using, so if they don't want to use these features, they remain unaffected by them.

您可能需要阅读...

You may want to take a read through...

  • Painting in AWT and Swing
  • Passive vs. Active Rendering

更多详情

请记住绘画很有趣;)

其他想法

其中一个othe需要考虑的因素是 Graphics API是绘画的核心,不仅考虑了UI,还考虑了打印和图像处理。 API与其目标断开连接,允许更大的灵活性,但也具有通用性。

One of the other considerations to take into account is the fact the the Graphics API is central to painting, not just with consideration to the UI, but also printing and image manipulation. The API is disconnected from it's target, allowing a greater deal of flexibility, but also commonality.

这意味着如果您需要打印到打印机或渲染到图像,您可以使用与绘制到屏幕相同的API。

This means that if you need to print to a printer or render to an image, you can use the same API you would for painting to the screen.

这篇关于为什么必须使用paintComponent方法在Java中绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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