Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:42:56+00:00 2026-05-27T18:42:56+00:00

I am making a 2D platform in Java, and I’m using a Swing timer,

  • 0

I am making a 2D platform in Java, and I’m using a Swing timer, it is set at 5 milliseconds interval. However, sometimes it is smooth movement at a good speed, but then randomly it will become super fast and sometimes super slow. What could be the reason for this?

Code:

public Board() {
    addKeyListener(new KeyListener());
    setFocusable(true);
    setBackground(new Color(204,250,255));

    //draws the object off the screen in memory, then brings it in
    setDoubleBuffered(true);

    ...(other code not relevant)...

    timer = new Timer(5, this);
    timer.start();
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    //draw platform
    for(int i = 0; i < platform.length; i++) {
        //g2d.setColor(new Color(0,0,0));
        //g2d.drawRect(platform[i].getX(), platform[i].getY(), platform[i].getWidth(), platform[i].getHeight());
        g2d.drawImage(platform[i].getImage(), platform[i].getX(), platform[i].getY(), this);
    }

    //draw guy
    g2d.drawImage(guy.getImage(), guy.getX(), guy.getY(), this);

    //destroy unneeded process
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

Basically, the cycle function decides whether the character needs to move or not, and moves it if it does.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:42:56+00:00Added an answer on May 27, 2026 at 6:42 pm

    It is hard to make Java work as a real-time systems…. because of unknown factors like when the OS will schedule your process to run and GC. Since you are trying to update the game at 200 fps (if my math is correct) you might want to try the following approach. To make the game smoother I would try to find the time between the current event fired and the last event fired, now theoretically that might be 5ms but sometimes it will be 7ms (going super slow) and sometimes it will be 3ms (going super fast). Ones you have the time between the two event I would calculate where the animation should be, so if the time is 3ms then the animation will be a bit before the 5ms time. Lets take a simple example where your are trying to update the X position of a image 5 px every 5ms.

    //your current code
    newX = X + 5px
    
    //new code
    //this wont always be 5ms
    timeDiff = currentEventTime – lastEventTime;
    
    newX = X + 5px (timeDiff/5ms)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
I am making a 2d platformer in Swing java, and I am wondering how
I am making a free cross platform mock up designer in Java. I have
I'm currently making an IDE for the Java platform. This IDE for education purposes
I am making a basic platform game for the iPhone and I have encountered
I am attempting to integrate an existing payment platform into my webshop. After making
I have a conceptual question... I am making an Intranet application (Web platform) for
I am making a 2D game in Android with Cocos2D, written in Java. Here
I'm making a simple 2d game for the android platform, which works perfectly from
In a cross-platform (Windows, FreeBSD) C++ project I'm working on, I am making use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.