简体   繁体   中英

Java Timers with jSliders

I have a question on timers with Sliders in java. My code essentially is a GUI with a moving animation panel in the center. This animation panel, which is in a separate class than the other classes that create the actual frame, has the timer itself.

timer = new Timer(5, new Listener());
timer.start();

This timer fires and goes to a listener which executes the methods for the movement of the ball etc.

I need to create a slider that can adjust the speed of the timer to make the ball move very fast or very slow. How do I do this with the slider in one class and the actual timer in the class with the animation segment. This is the slider and its respective listener:

slider = new JSlider(JSlider.HORIZONTAL,1,15,5);
slider.setMajorTickSpacing(5);
slider.setPaintTicks(true);
panel.add(slider, BorderLayout.SOUTH);

private class event implements ChangeListener
  {
     public void stateChanged(ChangeEvent e)
     {
        //int speed = slider.getValue();
            //JSlider altSlider = (JSlider) e.getSource();


         if(slider.getValueIsAdjusting()) 
            {
                speed = slider.getValue();
                //bumperPanel.changeTimer(speed);
                //bumperPanel.changeX(speed);
            }

     }
}

Is there any way to adjust the speed of the timer for significant changes:

I commented some bumperPanel.changeX / changeTimer methods. These methods were not working so I still need some help.

Thanks in advance!!

How do you mesure speed? In milliseconds perhaps? If so, you must convert slider.getValue() to milliseconds and then call the proper method from 'bumberPanel' !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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