Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: java swing timer not precise enough?

  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default java swing timer not precise enough?

    hello java forums users. :p

    recently been testing some more gui stuff. :p

    and it came to my attention that the java swing timer isn't really that accurate...
    i used a little program to test it... and it starts pretty good, but after running for a 20ish seconds there is starting to become a delay on the timer... :s (see code below)

    now my question is is there a timer class implemented in the java library that is more accurate then the java swing timer?
    if so could you guys tell me wich? :o

    this was the output of the program:
    chaning print for 162 time at 1346351773766
    chaning print for 163 time at 1346351774766
    chaning print for 164 time at 1346351775780
    chaning print for 165 time at 1346351776779
    chaning print for 166 time at 1346351777793
    chaning print for 167 time at 1346351778792
    chaning print for 168 time at 1346351779806
    chaning print for 169 time at 1346351780806

    Java Code:
    package gui;
    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.Timer;
    
    import domein.DomeinController;
    
    public class MainFrame extends JFrame{
    	private static final long serialVersionUID = -8026416994513756565L;
    	private DomeinController domCntrl;
    	private Timer timer;
    	private int calcVisual;
    	
    	public MainFrame(DomeinController domCntrl){
    		this.domCntrl = domCntrl;
    		this.setTitle("StartFrame");
    		this.setSize(800, 500);
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		init();
    	}
    	
    	private void init(){
    		timer = new Timer(999, new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				calcVisual++;
    				repaint();
    			}		
    		});
    		timer.start();
    	}
    	
    	public void paint(Graphics g){
    		super.paintComponents(g);
    		System.out.printf("chaning print for %d time at %d\n", calcVisual, System.currentTimeMillis());
    		g.drawLine(10, 250, 790, 250);
    		if(calcVisual % 2 == 0){
    			paintCircles(g);
    		}else{
    			paintRectangles(g);
    		}
    	}
    	
    	private void paintRectangles(Graphics g){
    		g.setColor(Color.red);
    		g.drawRect(375, 225, 50, 50);
    		g.setColor(Color.blue);
    		g.drawRect(350 , 200, 100, 100);
    		g.setColor(Color.green);
    		g.drawRect(325 , 175, 150, 150);
    		g.setColor(Color.CYAN);
    		g.drawRect(300 , 150, 200, 200);
    		g.setColor(Color.MAGENTA);
    		g.drawRect(275 , 125, 250, 250);
    		g.setColor(Color.orange);
    		g.drawRect(250 , 100, 300, 300);
    		g.setColor(Color.YELLOW);
    	}
    	
    	private void paintCircles(Graphics g){
    		g.setColor(Color.red);
    		g.drawOval(350 , 200, 100, 100);
    		g.setColor(Color.blue);
    		g.drawOval(340 , 190, 120, 120);
    		g.setColor(Color.green);
    		g.drawOval(330 , 180, 140, 140);
    		g.setColor(Color.CYAN);
    		g.drawOval(320 , 170, 160, 160);
    		g.setColor(Color.MAGENTA);
    		g.drawOval(310 , 160, 180, 180);
    		g.setColor(Color.orange);
    		g.drawOval(300 , 150, 200, 200);
    		g.setColor(Color.YELLOW);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: java swing timer not precise enough?

    You can use a Swing Timer for a simple game loop, but I'd change the state of the game based on changes in the system clock. For instance if I were trying to animate the movement of something, I'd use the delta-time from the clock in msecs to calculate the change in distance based on the velocity of the item. This can all be done within the Swing Timer.
    pbrockway2 likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: java swing timer not precise enough?

    Your printing is done in the repainting process and so will include imprecision over and above what is inherent in the hardware and Timer software. Every 999ms you put an event into the queue: but the painting occurs when that event is subsequently processed. There may be lots of other things in the queue that get done first, or there may be nothing.

    So using the delta time from some arbitrary starting point will tell you when the printing is being done. Alternatively ActionEvent does have a getWhen() method that tells you about the time of the event (I've never used it.)

    As far as I know Windows has a "granularity" of 1000ms/64 == 16ms. So the computer may be keeping wonderful time, but at any instant it only tells you that time with a precision of 16ms. I mention that because your differences from 999ms are neatly within that range. You don't say, but if you're using Windows it might be fun to run the same program on a real OS and see what the outcome is.
    Last edited by pbrockway2; 08-30-2012 at 09:58 PM.

Similar Threads

  1. Thread v swing timer
    By musico in forum New To Java
    Replies: 1
    Last Post: 06-23-2011, 09:51 PM
  2. Proper swing timer syntax
    By snaquetime in forum New To Java
    Replies: 5
    Last Post: 06-04-2011, 02:13 AM
  3. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  4. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 03:48 PM
  5. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •