Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: multiple swing timer? how to run it efficiently if not using multi-threading?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default multiple swing timer? how to run it efficiently if not using multi-threading?

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: multiple swing timer? how to run it efficiently if not using multi-threading?

    A swing Timer is just a Thread of its own which dispatches events to the EDT - however this single thread manages all Timers created using a DelayedQueue. The more Timers you place in the queue, and the shorter their fire frequency, the more risk you get of backing up not just the queue but also EDT tasks (and hence you may see delays as a result). Firing the timers every 5 milliseconds seems excessive to me (that's a frame rate of 200). Try something more like firing every 20ms (50fps - and this is probably still too fast). Creating separate Threads will work, as long as you know what you are doing with regards to concurrency - but again firing them too often and having complex painting for multiple panels could still back up the EDT.

    Edit: And I recommend doing your painting by overriding the paintComponent method rather than paint
    Last edited by copeg; January 6th, 2012 at 12:18 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (January 8th, 2012)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: multiple swing timer? how to run it efficiently if not using multi-threading?

    ahh so i still have the freedom to use any between the two without vioalating anything(depending on how i handle concurrency), im already used to multi-threaded, im just a little cautious because of the vast posts i see all over the web between timers and multi-threads. and i never saw a multi-swing timer codes

    And I recommend doing your painting by overriding the paintComponent method rather than paint
    thank you for reminding me.

    ahh becauase paint() does 3 stages, including the paintComponent, so its risky overriding without knowing what youre dealing with..
    Last edited by chronoz13; January 6th, 2012 at 10:58 PM.

Similar Threads

  1. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  2. Timer implementation in swing
    By portem1 in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 19th, 2011, 08:14 AM
  3. Anyone familiar with the swing timer?
    By BigJoe in forum AWT / Java Swing
    Replies: 5
    Last Post: December 28th, 2010, 12:15 PM
  4. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM