Best way to animate in Java?

后端 未结 1 1544
长发绾君心
长发绾君心 2021-01-07 06:27

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:



        
1条回答
  •  没有蜡笔的小新
    2021-01-07 07:22

    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();
    }
    }
    

    0 讨论(0)
提交回复
热议问题