更新图形时jframe闪烁 [英] jframe flash when updating graphics

查看:250
本文介绍了更新图形时jframe闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面,我的代码是用来制作可更新图形的代码,用于显示生活游戏本身。

  public paint(Graphics g)
{
g.setClip(); //设置
g.setColor(Color.BLACK);

处理中......

while(true)
{
g.clearRect(); //清除

g.drawLine(); //画;

调用SwingWorking准备下一条语句
}
}



看起来像这样做是愚蠢的,因为在运行它时,框架会一直闪烁而迟缓。
我尝试使用 Thread.sleep(),但它只是减慢了flash的频率。



<所以,我的问题是如何做好,避免闪烁。
我没有提到的代码部分都是关于实例变量的数据处理,如果您需要它,请注意我,非常感谢您的帮助。





感谢,@MadProgrammer



这是我得到的,
第一个,闪光是因为当程序调用repaint()和paint(),它会花费太多时间来平滑地进行动画,在这种情况下是clearRect()和drawLine()。



使用双缓冲来解决它。






更新,



谢谢你们,我读过这些例子。我只是使用一个没有任何缓冲区方法的框架来显示图形,这是一个错误。我使用了bufferStrategy来解决它。

解决方案

不是答案,而是示范




制作动画需要花费太多时间

对我而言,这意味着您不了解API如何工作,你可以用Swing做一些复杂的动画。如果你把它放在心上的话。



前一段时间,我创建了一个动画序列引擎,它将一系列图像并在彼此顶部进行动画制作

这个例子使用了5个独立的图片/图层(顶部带有绘画效果)



每个图层都有不同的速度,以便它们以不同的速度移动动画的时间(例如,速度为1将导致图层在序列的基本持续时间内仅旋转一次,在此e xample,20秒)

原始图像全部为1024x256,所以不会考虑大小




添加序列在透明窗口中播放,添加一个alpha效果(在边缘渗出),这不是一个简单的动画。



GIF以粗糙的8fps播放,实际的动画以大约200fps的速度播放

对不起,这段代码非常大,并且使用了很多其他库,比如Timing Framework,所以不可能发布)



我的观点是,问题不在于API(Swing / Graphics ),而在于你如何使用它



更多的例子,你可以看看:



并且大多数这些示例甚至不尝试任何主要优化或其他性能技术



您还应该查看在AWT和Swing中绘画执行自定义绘画关于绘画过程如何运作的更多细节

At below, its my code to make a renewable graphics shows life game itself.

public paint(Graphics g)
{
     g.setClip();//Set              
     g.setColor(Color.BLACK);

     Processing.....

     while(true)
     {
          g.clearRect();//Clear

          g.drawLine(); //Draw;

          Calling SwingWorking to prepare next statement
     }
}

seems it's stupid to do like this way because when running it, the frame keep flashing and laggy. I try to use Thread.sleep(), but it just slow down the frequency of flash.

So, my problem is how to make it well and avoid flashing. The parts of code that I don't put on are all about data processing for instance variables, if you need it,please notice me, much appreciate for help.


Thanks, @MadProgrammer

Here is what I got, first, the flash is because when program calling repaint() and paint(), it cost too much time to make animation smoothly, in this case is clearRect() and drawLine().

I'll use double buffered to solve it.


Update,

Thanks guys, I read those examples. I just use a frame without any buffer method to show graphics before, what a mistake. I used bufferStrategy to solve it.

解决方案

Not an answer, but a demonstration

it cost too much time to make animation smoothly

To me, this means you don't understand how the API works, you can do some complex animations with Swing if you put your mind to it.

A while ago I created an "animated sequence" engine, which took a series of images and animated them on top of each other

This examples uses 5 separate images/layers (with a paint effect put over the top)

Each layer is given a different "speed" so that they move at different speeds through out the base time of the animation (for example, a speed of 1 will cause the layer to rotate only once over the base duration of the sequence, in this example, 20 seconds)

The original images are all 1024x256, so that an inconsiderate size

Add ontop of that the sequence is playing in transparent window with the addition of an alpha effect (bleeding off at the edges), this is not a simple animation.

The gif is playing at rough 8fps, the actual animation runs at roughly 200fps

(Sorry, the code for this is quite large and makes use of a number of other libraries, like the Timing Framework, so it's impossible to post)

My point is, the problem isn't with the API (Swing/Graphics) alone, but with how you are using it

For more examples you could have a look at:

and most of these examples aren't even trying for any major optimization or other performance technqiues

You should also have a look at Painting in AWT and Swing and Performing Custom Painting for more details about how the painting process works

这篇关于更新图形时jframe闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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