简体   繁体   English

用Java制作动画的最佳方法?

[英]Best way to animate in Java?

I'm currently using an animation engine I designed that takes objects of type Drawable and adds them to a List. 我目前正在使用设计的动画引擎,该引擎采用Drawable类型的对象并将其添加到列表中。 Drawable is an interface that has one method: Drawable是具有一种方法的接口:

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. 扩展动画管理器遍历此列表,并在每个对象上调用draw()方法,并传递从Swing组件获取的Graphics2D对象。

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. 仅注册了两个Drawable,并且两个绘图图像都显示在屏幕上,我看到30-60秒后有点闪烁。

Is there a way to optimize this method? 有没有一种方法可以优化这种方法? It currently calls upon the AWT thread (invokeLater) to handle all of the tasks. 当前,它调用AWT线程(invokeLater)处理所有任务。 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. 并发绘图并不是真正的选择,因为这几乎总是会在Swing / AWT中引起问题,这在很大程度上是由于图形未同步。

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. 我无法使用repaint(),因为我的引擎已经调用AWT线程来绘画内容。 If I call invokeLater from the AWT thread, the image never gets painted for some reason. 如果我从AWT线程调用invokeLater,则由于某种原因该图像永远不会被绘制。

I should also add that I'm using a system of ticks and fps. 我还应该补充一点,我使用的是刻度和fps系统。 60 ticks @ 120 fps. 60次滴答@ 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? 我应该只使用FPS而不是滴答吗?

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. 我认为覆盖paintComponent(Graphics g)并定期在JPanel上或使用Timer绘制的任何对象上调用repaint方法会更合适。 Your problems may be due to to you trying to draw and then Swing doing it's own draw. 您的问题可能是由于您尝试绘制然后由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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM