简体   繁体   English

java计时器更改延迟按钮

[英]java timer change delay with button

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. 目前我有一个用于按钮的actionlistener,它可以更改延迟变量的值并重新启动计时器。

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. 这带来了其他问题。

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 这就是Timing Framework的工作原理,我相信Trident遵循类似的设计

在此输入图像描述

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

    }

}

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

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