在 java 中使用 super.paintComponent(g) 或 getGraphics() [英] use super.paintComponent(g) or getGraphics() in java

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

问题描述

我对一些事情有点困惑:

i' m little bit confused about few things:

示例代码,显示了我的问题,这是不可编译的

// image
private BufferedImage image;
private Graphics2D graphic;
private changeImage;
. . .
//thread loop
while (running) {
   . . .
    render();
    Graphics showGraphic = getGraphics();
    showGraphic.drawImage(image, 0, 0, null);
    showGraphic.dispose();
    }
 public void render(){
     if(changeImage == 1)
     graphic.drawImage(ImageLoader.TREE, 0, 0, null);
     else if(changeImage == 2){
     graphic.drawImage(ImageLoader.HOUSE, 0, 0, null);
      . . .
     graphic.fillRect(50,60,30,40);
     }
}

我创建了一个全局对象 Graphic2D 并在 render() 中绘制东西,我没有在游戏循环中调用 repaint(), 这样做是个好习惯吗?

I create an global object Graphic2D and i draw things in render(), I do not call repaint() inside the game loop, is it good practice to do this?

我应该在循环中使用 repaint()paintComponent() 函数吗?

Should I use repaint() inside my loop , and the paintComponent() function ?

另一件事,graphic.dispose() 如何正确工作?,因为试图删除这行代码,没有任何反应.

one other thing, how graphic.dispose() works correctly? , because trying to remove this line of code, nothing happens.

根据 java 文档,我了解 dispose() 是如何工作的,但我没有注意到 dispose() 与否有任何差异.

I understand how works dispose() according to java docs, but I have not noticed any differences with dispose() or without.

我的程序运行得很好,但我有这个问题.

my program runs very well, but i have this dubts.

推荐答案

这样做是个好习惯吗

is it good practice to do this

不,这实际上非常可怕且容易出错.这假定这是 Component#getGraphics 方法.问题是 Swing 使用被动渲染算法,即 Swing 决定何时以及什么应该重绘,并且出于优化原因这样做.

No, this is actually incredibly horrible and error prone. This assumes that this is the Component#getGraphics method. The problem is Swing uses a passive rendering algorithm, that is, Swing decides when and what should be repainted and does this for optimisation reasons.

这意味着更新不是定期的,这在制作动画时确实很有帮助,并且可能由于多种原因随时发生,其中许多原因您根本无法控制.

This means updates are not regular, which really helps when doing animation, and can happen at any time for any number reasons, many of which you simply don't and can't control.

出于这些原因,绘制应该在一种绘制方法中完成,最好是基于 JComponent 的类的 paintComponent.这样,当 Swing 决定重新绘制时,一是您了解它并可以相应地更新输出,二是它不会清除您之前使用 getGraphics 绘制的内容,这可能会导致闪烁...

For these reasons, painting should be done within one of the paint methods, preferably, paintComponent of a JComponent based class. This way, when Swing decides to do a repaint, one, you know about it and can update the output accordingly, and two, it won't clear what you have previously painted to it using getGraphics which could cause flickering...

您还遇到了潜在的线程问题,因为您的线程正在尝试绘制到 Graphics 上下文,因此事件调度线程也可能会遇到潜在的线程问题,并且不会以漂亮的方式结束......所有绘画应该在 EDT 的上下文中完成(对于基于组件的绘画)

You are also running into potential threading issues, as you thread is trying to paint to the Graphics context, so might the Event Dispatching Thread and that isn't going to end pretty...All painting should be done from within the context of the EDT (for component based painting)

您可以尝试使用 BufferedStrategy,它可以让您直接控制绘制过程,但这会限制您使用 AWT 库.不知道这对你来说是好是坏.

You could try using a BufferedStrategy, which would give you direct control over the painting process, but this limits you to the AWT library. Don't know if this is good or bad in your case.

另一件事,graphic.dispose() 如何正确工作?, 因为试图删除这行代码,没有任何反应.

one other thing, how graphic.dispose() works correctly? , because trying to remove this line of code, nothing happens.

dispose 基本上释放了 Graphics 上下文可能持有的任何本机资源.一般的经验法则,如果你没有创造,你就不会处理它.

dispose basically releases any native resources that the Graphics context might be holding. General rule of thumb, if you didn't create, you don't dispose of it.

因此,如果您使用 Graphics#createBufferedImage#createGraphics 来获取 Graphics 上下文,那么您应该调用 dispose 完成后.

So if you use Graphics#create or BufferedImage#createGraphics to obtain a Graphics context, then you should call dispose when your done with it.

在某些系统上,在用于执行组件绘制的 Grpahics 上下文上调用 dispose(例如传递给 paintComponent 或从 paintComponentcode>getGraphcis) 可以防止进一步的内容被绘制.

On some systems calling dispose on the Grpahics context used to perform painting of components (such as that passed to paintComponent or obtained from getGraphcis) can prevent further content from been painted.

同样,不处理您创建的 Graphics 上下文会导致内存泄漏,因为它们不会被垃圾收集...

Equally, not disposing of Graphics contexts that you have created can lead to memory leaks as they won't get garbage collected...

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

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