在Java中动画的最佳方法是什么? [英] Best way to animate in Java?

查看:152
本文介绍了在Java中动画的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用我设计的动画引擎,它带有Drawable类型的对象并将它们添加到列表中。 Drawable是一个有一个方法的接口:

  public void draw(Graphics2D g2d); 

扩展动画管理器遍历该列表,并在每个对象上调用draw()方法,从Swing组件获得的Graphics2D对象。



这种方法起初看起来效果很好,但正如我担心的那样,它似乎无法处理长期运行的多个对象。

只有两个Drawables注册,两个都在屏幕上绘制图像,30-60秒后我看到有点闪烁。



有没有办法优化这个方法?它目前调用AWT线程(invokeLater)来处理所有的任务。并发绘图并不是一个真正的选择,因为这几乎总是会导致Swing / AWT中的问题,很大程度上是由于Graphics不同步。



如果这只是一种不好的动画方式,当你有多个对象都需要用自己的特定变量渲染时,什么是更好的方法咳嗽游戏咳嗽



任何帮助,将不胜感激。谢谢!



编辑:

我无法使用repaint(),因为我的引擎已经调用了AWT线程画东西。如果我从AWT线程调用invokeLater,则图像永远不会被绘制出来。



我还应该补充一点,我使用的是ticks和fps系统。 60个ticks @ 120 fps。

每个刻度更新游戏逻辑,而每个帧渲染调用都在帧管理器上绘制。



这是一个坏主意吗?我应该只是使用FPS而不是滴答声?

解决方案

我认为重写paintComponent(Graphics g)调用JPanel上的repaint方法或者使用定时器绘制的任何东西。你的问题可能是由于你试图绘制,然后Swing做它自己的绘制。

  public class Main {
public static void main(String [] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel(){
public void paintComponent(Graphics g){
// draw here
}
};
panel.setPreferredSize(800,600);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true)

新定时器(16,new ActionListener(){
public void actionPerformed(ActionEvent event){
panel.repaint();
}
})。start();
}
}


I'm currently using an animation engine I designed that takes objects of type Drawable and adds them to a List. Drawable is an interface that has one method:

public void draw(Graphics2D g2d);

The extending animation manager iterates through this list and calls the draw() method on every object, passing the Graphics2D object obtained from the Swing component.

This method seemed to work well at first, but as I feared, it seems to be unable to handle multiple objects in the long run.

With merely two Drawables registered, both drawing images on screen, I'm seeing a bit of flashing after 30-60 seconds.

Is there a way to optimize this method? It currently calls upon the AWT thread (invokeLater) to handle all of the tasks. Concurrent drawing isn't really an option as this nearly always causes issues in Swing/AWT, in large part due to the fact that Graphics isn't synchronized.

If this just simply is a bad way of animating, what is a better method when you have multiple objects that all need things rendered with their own specific variables cough game cough?

Any help would be appreciated. Thanks!

EDIT:

I can't use repaint() beacuse my engine already calls the AWT thread to paint stuff. If I call invokeLater from the AWT thread, the image never gets painted for some reason.

I should also add that I'm using a system of ticks and fps. 60 ticks @ 120 fps.

Each tick updates the game logic, while each frame render calls draw on the frame manager.

Is this a bad idea? Should I just use FPS and not ticks?

解决方案

I think it would be more appropriate to override paintComponent(Graphics g) and regularly call the repaint method on the JPanel or whatever you're drawing on with a Timer. Your problems may be due to to you trying to draw and then Swing doing it's own draw.

public class Main {
public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {
            //draw here
        }
    };
    panel.setPreferredSize(800, 600);
    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true)

    new Timer(16, new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            panel.repaint();
        }
    }).start();
}
}

这篇关于在Java中动画的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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