java中 paintcomponent,为什么必须使用paintComponent方法在Java中绘制?

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"

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.

There are a number of factors involved...

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.

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.

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.

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...

For more details

And remember "Painting is fun" ;)

Additional Thoughts

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用Java的图形库来绘制图形。Java提供了一个名为AWT(Abstract Window Toolkit)的图形库,以及一个名为Swing的高级图形库。 使用AWT绘制图形的基本步骤如下: 1. 创建一个继承自`java.awt.Canvas`类的自定义画布类。 2. 在自定义画布类重写`paint()`方法,该方法用于绘制图形。 3. 在`paint()`方法使用`java.awt.Graphics`对象提供的方法绘制各种图形,例如`drawLine()`、`drawRect()`、`drawOval()`等。 4. 创建一个继承自`java.awt.Frame`类的窗口类,并将自定义画布类添加到窗口。 5. 在窗口类设置窗口的大小、可见性等属性,并添加关闭窗口的事件处理。 使用Swing绘制图形的基本步骤如下: 1. 创建一个继承自`javax.swing.JPanel`类的自定义面板类。 2. 在自定义面板类重写`paintComponent()`方法,该方法用于绘制图形。 3. 在`paintComponent()`方法使用`java.awt.Graphics`对象提供的方法绘制各种图形,例如`drawLine()`、`drawRect()`、`drawOval()`等。 4. 创建一个继承自`javax.swing.JFrame`类的窗口类,并将自定义面板类添加到窗口。 5. 在窗口类设置窗口的大小、可见性等属性,并添加关闭窗口的事件处理。 需要注意的是,绘制图形时需要在事件分发线程(Event Dispatch Thread)进行,可以使用`javax.swing.SwingUtilities`类的`invokeLater()`方法来确保在事件分发线程执行绘制操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值