Sequential Swing timer execution

杀马特。学长 韩版系。学妹 提交于 2019-12-25 16:58:09

问题


I'm using a Swing Timer to execute animations in my program. In many instances, there are several different calls made to the animate() method at once, with a separate Timer created for each. I know that, because of the way Swing timers are designed, these all get executed together - all my animations occur at the same time. However, there are some instances where I need to wait for one animation to complete to execute another.

Is there a way to make Swing timers execute sequentially - one after the other, rather than all at once? Or is there an alternative mechanism to the Swing timer that might better match my use case?

EDIT: I'm afraid I oversimplified my use case a bit. @peeskillet's suggestion would work perfectly if I knew at each "scene transition" what animations would need to be executed, or if the same sequence of animations occurred each time. Unfortunately that's not the case -- each transition requires a different set of animations, with different sets of components moving onto, off of and around on the panel.

What I want is to execute the animations of items off the screen first, and then (after that completes) animate the components on the screen. It's not a problem to distinguish between these "types" of animations at runtime - they're initiated from different methods, and thus its easy to imagine adding them to two different "queues" - a queue of outgoing items and a queue of incoming items. Having done so, I could then implement the basic strategy of calling a

That said - that all only makes sense to me intuitively, heuristically - I haven't figured out how to implement it in practice. What would those "queues" actually be, and what class would hold and later execute them?? Presumably one that implements Runnable, creating a second thread that can execute the animations with tighter control on how they proceed? Or does the event-dispatch thread give me the ample control here, if only I fully grasped how to use it? In which case - please help me do that.

(PS I realize that I've changed the question significantly here, essentially turning it into a new question, and that @peetskillet answered it as previously worded perfectly well, so I accepted that answer and posted a new question here.


回答1:


"Is there a way to make Swing timers execute sequentially - one after the other, rather than all at once? "

Just use a `boolean of some sort, telling when the first timer when it should stop and when the second timer should start. Something like

Timer timer1 = new Timer(delay, null);       <---- initialize
Timer timer2 = new Timer(delay, null);
boolean something = false;

public Constructor() {
    timer1 = new Timer(delay, new Action Listener(){
        public void actionPerformed(ActionEvent e) {
            if (something) {               ------
                timer2.start();                 |
                timer1.stop();                  |---- some code should lead to
            } esle {                            |     `something` being true. Maybe
                animateFirstSomething();        |     another if statement inside the 
            }                                   |     else. Like if x equals y
        }                                  ------     something = true, else,
    });                                               animateFirstSomething()
    timer1.start();

    timer2 = new Timer(delay, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            animationSecondSomething();
        }
    });
}

Here's simple example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestTwoTimers extends JPanel {

    int rectOneX = 0;
    int rectTwoX = 0;

    Timer timer1 = new Timer(100, null);
    Timer timer2 = new Timer(100, null);
    boolean rectOneGo = true;



    public TestTwoTimers() {
        timer1 = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (rectOneGo) {
                    if (rectOneX >= 225) {
                        timer2.start();
                        timer1.stop();
                    } else {
                        rectOneX += 10;
                        repaint();
                    }
                }
            }
        });
        timer2 = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (rectTwoX < 225) {
                    rectTwoX += 10;
                    repaint();
                } else {
                    timer2.stop();
                }
            }
        });

        final JButton button = new JButton("Start First Timer");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                timer1.start();
            }
        });

        setLayout(new BorderLayout());
        add(button, BorderLayout.PAGE_END);
    }

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

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillRect(rectOneX, 50, 75, 75);

        g.setColor(Color.BLUE);
        g.fillRect(rectTwoX, 150, 75, 75);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame("Double Timers");
                frame.add(new TestTwoTimers());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}


来源:https://stackoverflow.com/questions/21298002/sequential-swing-timer-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!