停止在简单的Java动画中闪烁 [英] Stop flickering in simple Java animation

查看:130
本文介绍了停止在简单的Java动画中闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的applet,可沿画布的x轴设置一个矩形动画.问题是它闪烁.我曾尝试向Google搜索此问题,但没有提出任何有用的信息或我理解的信息.

I have a simple applet that animates a rectangle along the x-axis of the canvas. The problem is that it flickers. I have tried to Google this problem, but I didn't come up with anything useful or anything that I understood.

我对Java比较陌生.

I am relatively new to Java.

谢谢!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*; 

public class simpleAnimation extends JApplet implements ActionListener { 
    Timer tm = new Timer(10, this); 
    int x = 0, velX = 2;

    public void actionPerformed(ActionEvent event) { 
        if (x < 0 || x > 550){ 
            velX = -velX; 
        }

        x = x + velX; 
        repaint(); 
    }

    public void paint ( Graphics g ) { 
    super.paint(g); 
    g.setColor(Color.RED); 
    g.fillRect(x, 30, 50, 30); 
    tm.start(); 
    } 
}

** * ** * **** 没有闪烁符的更新代码 * ** * ** * ***

**********UPDATED CODE WITHOUT FLICKER**********

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class simpleAnimation extends JApplet implements ActionListener  
{ 

     Graphics bufferGraphics; 

     Image offscreen; 

     Dimension dim; 

     int x = 3, velX = 2;

     Timer tm = new Timer(10, this);

     public void init()  
     { 

          dim = getSize(); 

          offscreen = createImage(dim.width,dim.height); 

          bufferGraphics = offscreen.getGraphics(); 
     }

      public void paint(Graphics g)  
     { 

          bufferGraphics.clearRect(0,0,dim.width,dim.width); 

          bufferGraphics.setColor(Color.red); 

          bufferGraphics.fillRect(x,50,50,20); 

          g.drawImage(offscreen,0,0,this); 

          tm.start();   

     }

     public void update(Graphics g) 
     { 

          paint(g); 

     } 

    public void actionPerformed(ActionEvent evt) 
    {   

        if ( x < 0 || x > 550){

            velX = -velX;

        }

        x = x + velX;   

        repaint();

    }

 }

我使用了 applet 作为模板.

推荐答案

问题是,像JApplet这样的顶级容器没有被双重缓冲.这意味着更新后,屏幕闪烁,因为每个操作都直接在屏幕上完成.

The problem is, top level containers like JApplet aren't double buffered. This means that when it's updated, the screen flickers as each action is done directly onto the screen.

相反,您应该使用JPanel之类的东西来创建自定义组件,并覆盖其paintComponent方法并在其中执行自定义绘制动作.

Instead, you should create a custom component, using something like a JPanel, and override its paintComponent method and perform your custom painting actions there.

由于Swing组件被双重缓冲,因此绘制动作的结果在绘制到屏幕上之前就被缓冲了,从而使其成为单个动作

Because Swing components are double buffered the results of the paint action are buffered before they are painted to the screen, making it a single action

有关更多详细信息,请参见执行自定义绘画

Take a look at Performing Custom Painting for more details

这篇关于停止在简单的Java动画中闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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