Measuring time spent in application / thread

Posted by Adamski on Stack Overflow See other posts from Stack Overflow or by Adamski
Published on 2010-04-14T12:01:53Z Indexed on 2010/04/14 12:03 UTC
Read the original article Hit count: 251

Filed under:
|
|

I am writing a simulation in Java whereby objects act under Newtonian physics. An object may have a force applied to it and the resulting velocity causes it to move across the screen. The nature of the simulation means that objects move in discrete steps depending on the time ellapsed between the current and previous iteration of the animation loop; e.g

public void animationLoop() {
  long prev = System.currentTimeMillis();
  long now;

  while(true) {
    long now = System.currentTimeMillis();
    long deltaMillis = now - prev;
    prev = now;

    if (deltaMillis > 0) { // Some time has passed
      for (Mass m : masses) {
        m.updatePosition(deltaMillis);
      }

      // Do all repaints.
    }
  }
}

A problem arises if the animation thread is delayed in some way causing a large amount of time to ellapse (the classic case being under Windows whereby clicking and holding on minimise / maximise prevents a repaint), which causes objects to move at an alarming rate. My question: Is there a way to determine the time spent in the animation thread rather than the wallclock time, or can anyone suggest a workaround to avoid this problem?

My only thought so far is to contstrain deltaMillis by some upper bound.

© Stack Overflow or respective owner

Related posts about java

Related posts about java-2d

  • java 2D and swing

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Hi, I have trouble understanding a fundamental concept in Java 2D. To give a specific example: One can customize a swing component via implementing it's own version of the method paintComponent(Graphics g) Graphics is available to the body of the method. Question: What is exactly this Graphics… >>> More

  • Java 2D Drawing Framework

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Currently, I am using JHotDraw https://sourceforge.net/projects/jhotdraw/ as Figures Drawing Framework in my application. JHotDraw is a two-dimensional graphics framework for structured drawing editors that is written in Java. It is based on Erich Gamma's JHotDraw, which is copyright 1996… >>> More

  • Java 2D Resize

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I have some old Java 2D code I want to reuse, but was wondering, is this the best way to get the highest quality images? public static BufferedImage getScaled(BufferedImage imgSrc, Dimension dim) { // This code ensures that all the pixels in the image are loaded. Image scaled = imgSrc.getScaledInstance( dim… >>> More

  • Java 2D Game Frameworks

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I have been looking at frameworks for writing 2D games in Java - part of a growing desire to rediscover my roots in writing those simple but addictive video games. I have googled and found some possibilities, but it's hard to judge which is best without investing significant time in each. Does anyone… >>> More

  • Java 2D turn-based game programming: handle 2 mouse clicks per player

    as seen on Stack Overflow - Search for 'Stack Overflow'
    So suppose I'm developing a chess-like program using Java's Swing. I added a MouseListener to handle user input. To make a move the user must click a valid piece and then click a valid place. What's the best way to track the 2 mouse clicks in a turn? I'm thinking in use some kind of variable to record… >>> More