【问题标题】:(Java) Screen flickering(Java) 屏幕闪烁
【发布时间】:2013-02-10 04:34:50
【问题描述】:

我已经开始使用 Java(用于游戏)编写一个小而简单的引擎。当我重新粉刷屏幕时,它有时会闪烁。我已经查找了这个问题的答案,并建议使用摇摆计时器的答案(这就是我正在做的)。以下是相关的代码(我在一些 cmets 中添加了):

public class Game extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;

private static Window window;
private static ObjectUpdater objects;
private static Timer timer;

private static boolean init = false;


public Game(){
    timer = new Timer(30,this);
    timer.start();
}

public static void main (String[] self){
    new Game();
}

private static void initialize(){
    //Tests
    //Setting up the window
    window = new Window();

    //Setting up the updater
    objects = new ObjectUpdater();

    new Picture(Path.images+Path.img_black,10);
    // These are just some objects for the game...

    Entity r = new Entity(new Picture(Path.images+Path.img_lapras,0));
    r.setVelocity(0.5,0);
    Entity r2 = new Entity(new Picture(Path.images+Path.img_lapras,1));
    Vector i = new Vector(0,0.5);
    r2.setVelocity(i.values()[0],i.values()[1]);
}
// This is where repaint(); is called.
public void actionPerformed(ActionEvent e) {
    setDoubleBuffered(true);
    if (init == false){
        initialize();
        init = true;

    }

    objects.update(); // This updates all the game object's information
    window.update(); // This updates the window itself, it's literally: repaint();

}
}

这里是窗口对象的代码,因为这也是事情发生的地方。

public class Window extends JFrame{
private static final long serialVersionUID = 1L;

public static int refresh_rate = 25;
public static Picture[] image_list =  new Picture[0]; // All the images I want to render
private static String win_title = "Window"; // The name of the window
private static int[] win_xy = {640,360}; // The size of the window

public Window(){
    //initializes window 
    setTitle(win_title);
    //setUndecorated(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(win_xy[0],win_xy[1]);
    setLocationRelativeTo(null); 
    setVisible(true);
    setResizable(false);

}

public static int[] getWinSize(){
    return win_xy;
}

// Here I'm just "painting" everything in the image_list array...
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    for (Picture i:image_list){
        g2d.drawImage(i.getPic(), i.getXY()[0], i.getXY()[1], null); 
    }
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}


public static void addPicture(Picture element){// This adds images to the image_list
    Picture[] result = Arrays.copyOf(image_list, image_list.length +1);
    result[image_list.length] = element;
    image_list = result;
    Arrays.sort(image_list);

}

public void update() {
    repaint();

}

}

就是这样...谢谢!

【问题讨论】:

  • Window 类的paint 方法中调用super.paint(g) 是导致闪烁的原因,此外,对于基于swing 的应用程序,您应该覆盖paintComponent 而不是paint
  • 我有什么理由应该使用paintComponent 而不是paint?我的意思是更具体?
  • 查看this页面。

标签: java screen repaint flicker


【解决方案1】:

顶级包含(例如JFrame)不是双缓冲的。这是我们不建议从顶级容器扩展的原因之一。

相反,为您自己创建一个自定义组件(例如来自 JPanel)并覆盖它的 paintComponent 方法(不要忘记调用 super.paintComponent

退房

举几个例子

【讨论】:

  • 如果你觉得答案有用,你可以把它标记为正确(右边的小勾号),否则,当你有更多的声誉时记得我;)
猜你喜欢
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 2015-11-05
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多