Java计时器更改延迟与按钮 [英] java timer change delay with button

查看:78
本文介绍了Java计时器更改延迟与按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动画,希望通过单击按钮来加快和减慢速度。
我四处逛逛,找不到解决方案。目前,我在按钮上有一个动作侦听器,它可以更改延迟变量的值并重新启动计时器。

I have an animation that i would like to speed up and slow down with a button click. I've looked around quite a bit and cant find a solution. Currently I have an actionlistener for the button that changes the value of the delay variable and restarts the timer.

有人可以请教。
谢谢

Can anyone please advise. Thanks

推荐答案

而不是更改计时器(动画的其他部分可能会依赖该计时器),我会更改物体的速度。

Rather then changing the timer, which other parts of your animation might rely on, I would change the speed of the object.

这有点主观。我的示例包含一个能够在相同时间段内改变速度的对象。实际上,您可能需要更改动画播放的时间,这需要更改时间。这带来了其他问题。

This is little subjective. My example has an object capable of changing speeds over the same period of time. You may actually be required to alter how long the animation plays for instead, which will require a change in the time. This brings up other issues.

一种实现此目的的方法是,实际上有一个定时间隔计时的计时器,以及一个对这些计时做出反应的模型。这意味着,如果您调整持续时间,则不会影响心跳,您只需要调整动画中的距离即可。这就是计时框架的工作方式,我相信 Trident 遵循类似的设计

One way to achieve this is, is to actually have a timer that ticks at a regular interval and a model that reacts to those ticks. This means that if you adjust the duration, the heart beat won't be effected, you'd just need to adjust the distance through the animation. This is how the Timing Framework works and I believe Trident follows a similar design

public class TestAnimationSpeed {

    public static void main(String[] args) {
        new TestAnimationSpeed();
    }

    public TestAnimationSpeed() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new AnimationPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class AnimationPane extends JPanel {

        private JSlider slider;
        private BouncyPane bouncyPane;

        public AnimationPane() {
            setLayout(new BorderLayout());
            bouncyPane = new BouncyPane();
            add(bouncyPane, BorderLayout.CENTER);
            slider = new JSlider(1, 11);
            slider.setMajorTickSpacing(2);
            slider.setPaintTicks(true);
            add(slider, BorderLayout.SOUTH);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    bouncyPane.setVerticalSpeed(slider.getValue());
                }
            });
            slider.setValue(4);
        }

    }

    public class BouncyPane extends JPanel {

        private int dy = 4;
        private Ellipse2D ball;

        public BouncyPane() {
            ball = new Ellipse2D.Float(100, 0, 10, 10);
            Timer timer = new Timer(1000/60, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    double y = ball.getY();
                    y += dy;
                    if (y + ball.getHeight() > getHeight()) {
                        y = getHeight() - ball.getHeight();
                        dy *= -1;
                    } else if (y < 0) {
                        y = 0;
                        dy *= -1;
                    }
                    ball.setFrame(ball.getX(), y, ball.getWidth(), ball.getHeight());
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        public void setVerticalSpeed(int speed) {
            if (dy < 0 && speed > 0) {
                dy = -speed;
            } else {
                dy = speed;
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(ball);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

这篇关于Java计时器更改延迟与按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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