some says and as the articles say, swing timer is meant for animations or concurrency with swing components(Single tasks), how about for multiple tasks? they say "you should use Swing Timers rather than implementing your class with a Runnable interface, making a Thread
'Sleep' ", but with using Swing Timers, the significance of getting "SLOW" is really obvious..
i have 3 classes, a JPanel child each, AnimatingPanel1, AnimatingPanel2, AnimatingPanel3, running a simple animation using swing timer, these 3 animation panels are used all at the same time in a separate class named AnimationWindow
import java.awt.*; import java.awt.event.*; import javax.swing.Timer; import javax.swing.JPanel; public class AnimatingPanel1 extends JPanel implements ActionListener { private Timer timer; private int x; public AnimatingPanel1() { setPreferredSize(new Dimension(400, 100)); timer = new Timer(5, this); timer.start(); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.WHITE); g.drawRect(x, 5, 100, 50); } public void actionPerformed(ActionEvent e) { if (x >= 200) { x = 0; } x += 5; repaint(); } }
import javax.swing.*; import java.awt.*; public class AnimationWindow { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel framePanel = new JPanel(new BorderLayout()); AnimatingPanel1 ap1 = new AnimatingPanel1(); AnimatingPanel2 ap2 = new AnimatingPanel2(); AnimatingPanel3 ap3 = new AnimatingPanel3(); ap1.setBackground(Color.BLACK); framePanel.add(ap1, BorderLayout.NORTH); ap2.setBackground(Color.GRAY); framePanel.add(ap2, BorderLayout.CENTER); ap3.setBackground(Color.BLACK); framePanel.add(ap3, BorderLayout.SOUTH); frame.getContentPane().add(framePanel); frame.pack(); frame.setVisible(true); } }
to make the posting of codes short, i didnt include AnimatingPanel2 and AnimatingPanel3, they have exactly the same codes as AnimatingPanel1, only the class name differs,
the slowing issue can be resolved with a multi-threaded approach, controlling the working thread with a boolean flag, everyting runs fine
like this one
AnimatingPanel1 modified to a class implementing a Runnable interface
import java.awt.*; import javax.swing.JPanel; public class AnimatingPanel1 extends JPanel implements Runnable { private Thread timer; private int x; private boolean stop; public AnimatingPanel1() { setPreferredSize(new Dimension(400, 100)); timer = new Thread(this); timer.start(); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.WHITE); g.drawRect(x, 5, 100, 50); } public void run() { while (!stop) { if (x >= 200) { x = 0; } x += 5; repaint(); try { Thread.sleep(5); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }
1.) if they say a swing timer should be used for swing animations, but how about these samples, multiple timers in different classes seems to work REALLY SLOW than making each AnimatingPanel implement a Runnable interface?
2.) what is the best or better approach? a multi-threaded one? or multiple-swing timer tasking and how can it be correctly done?
im totally confused, a simple question even typing "Multiple Swing Timers" with google doesnt seem to give a definitive answer, am i missing something here?.. please enlighten me. thanks in advance