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

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

问题描述

我是有点困惑的几件事情:

例子code,显示我的问题,这不是编译

  //图像
私人BufferedImage的图像;
私人Graphics2D的图形;
私人changeImage;
。 。 。
//线程循环
而(运行){
   。 。 。
    渲染();
    图形showGraphic =的getGraphics();
    showGraphic.drawImage(图像,0,0,NULL);
    showGraphic.dispose();
    }
 公共无效渲染(){
     如果(changeImage == 1)
     graphic.drawImage(ImageLoader.TREE,0,0,NULL);
     否则,如果(changeImage == 2){
     graphic.drawImage(ImageLoader.HOUSE,0,0,NULL);
      。 。 。
     graphic.fillRect(50,60,30,40);
     }
}
 

我创建一个全局对象 Graphic2D 和我画的东西渲染(),我不叫重绘()游戏圈里,是很好的做法,这样做呢?

我应该使用重绘()我的循环中,而的paintComponent()功能?

一件事,怎么 graphic.dispose()正常工作? ,因为试图删除此行的code,什么都不会发生。

我的理解是如何工作的Dispose()根据Java文档 ,但我还没有发现与处置任何差异()或没有。

我的程序运行得很好,但我有这个dubts。

解决方案
  

这是很好的做法,以做到这一点。

没有,其实这是令人难以置信的可怕,而且容易出错。这是假设,这是组件#的getGraphics 方法。问题是摆采用了被动式的渲染算法,即,摇摆决定什么时候和什么应该被重新绘制并执行这个优化的原因。

这意味着更新不规律,从而真正帮助做动画时,可以发生在任何时间以任何理由数量,其中许多你根本就没有和无法控制的。

由于这些原因,绘画应该在的油漆的方法之一完成的,preferably,的paintComponent 的JComponent 基础类。这样,当秋千决定做一个重画,一,你了解它,并可以相应地更新输出,以及两个,也不会清楚你有什么$ P $使用pviously涂上它的getGraphics 这可能会导致闪烁...

您也遇到了潜在的线程问题,因为你线程试图画到图形的背景下,因此可能会在事件分派线程,并且不会结束pretty的......所有的画应该从美国东部时间范围内进行(用于基于组件的油画)

您可以尝试使用 BufferedStrategy ,这将让你直接控制喷漆工艺,但是这限制了你的AWT库。不知道这是不是你的情况是好是坏的。

  

一件事,怎么graphic.dispose()正常工作? , 因为   试图删除此行的code,什么都不会发生。

处理基本上释放了图形情况下可能持有的任何本地资源。一般的经验法则,如果你没有创建,你不处理它。

所以,如果你使用图形#创建的BufferedImage#的createGraphics 来获得图形的背景下,那么你应该叫处理当你用它做。

在某些系统中调用处理对用于执行绘画组件(如传递给的 Grpahics 背景的paintComponent getGraphcis 获得)可以prevent从被漆成进一步的内容。

同样,没有处置图形您已经创建了可以导致内存泄漏,因为他们将不会被垃圾回收情境...

i' m little bit confused about few things:

Example code,that shows my problem,this isn't compilable

// 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);
     }
}

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?

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

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

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

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.

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

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)

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.

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

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.

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

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.

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天全站免登陆