Results 1 to 3 of 3
  1. #1
    smithywill is offline Member
    Join Date
    Jul 2009
    Rep Power

    Default Java Game Timer Issue! Help

    Hey guys

    Got a bit of a problem, I have created this little pong pong style Java app and I thought everything was working fine however when I tried it at the Universitys computers it was a lot slower then normal.

    I have a feeling it has to do with the timer which calls the repaint() method every 3 milliseconds:

    Java Code:
    public void actionPerformed(ActionEvent e)
    Any ideas why the speed of the game varies from PC to PC?



  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    San Jose, CA, USA
    Rep Power


    You cannot guarantee constant intervals of time. You need to take the delta time (the difference in time) to determine how far to move something.

    Lets say you want to move 5 pixels every 100ms and you try to update every 20ms.

    Time for each Tick

    tick 0: 19ms;
    tick 1: 25ms;
    tick 2: 35ms;
    tick 3: 20ms;

    Java Code:
    double standardTimeInterval = 100.0;   //100ms
    double pos = 0.0; //position of your object
    double velocity = 5.0;
    long lastTime = 0;
    if(lastTime == 0) 
       lastTime = System.currentTimeMillis();
    double dt = (System.currentTimeMillis()-lastTime)/standardTimeInterval;
    lastTime = System.currentTimeMillis();
    pos +=  velocity * dt;

    Distance traveled after each tick

    tick-1: 0 pixels
    tick 0: 0.95 pixels
    tick 1: 2.2 pixels
    tick 2: 3.95 pixels
    tick 3: 4.95 pixels
    Last edited by mrmatt1111; 03-04-2010 at 10:15 PM.
    My Hobby Project: LegacyClone

  3. #3
    Klauspinto is offline Member
    Join Date
    Mar 2010
    Rep Power


    The resolution of Java timers is not very accurate because Java doesn't play well with Windows processor in a multiple processor architecture. It seems that Windows will sleep processes in different areas of the processor or even in different processors effectively blacking out the timer.

Similar Threads

  1. Java Timer
    By sasi25 in forum Advanced Java
    Replies: 4
    Last Post: 01-30-2010, 10:19 AM
  2. Dice game issue- any Java gamblers ableto help?:P
    By mambalamba in forum New To Java
    Replies: 2
    Last Post: 12-17-2009, 07:49 PM
  3. Timer in java
    By manhit45 in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 03:27 PM
  4. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 PM
  5. Replies: 0
    Last Post: 04-04-2008, 03:46 PM

Posting Permissions

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