Well, a really basic one, at least. The balls are moving smoothly now, and at a firm 60fps instead of the 63-64fps I had been getting for a long time. It only jerks a tiny bit whenever I click away from it, or my anti-virus occasionally scans a file or something. For me, at least I'm doing this for an applet-game, but it can be easily converted to work in a windowed game.

I've tried out several game-loops posted in this forum, and they all seemed to get me some sort of constant jerkiness, odd FPS or they were timestep-loops (separating logic-updates from renders) which I'm not currently interested in. I suppose this could pretty easily be changed into a timestep-loop, though.

This is a result of several days of pondering over posts on this forum, and scouring the Internet for posts on game-loops, so in essence, you were all part of this, and I thank you all so VERY much!

Own thoughts (and questions):A - I have no idea if the timerAccuracyThread will help in this case, being executed in init(). An Applet automatically runs init() when started, and I can't have a Main in an Applet, as far as I know. So where to put it? It doesn't seem to make a perceptible difference. I left it in anyway.

B - Doing Thread-sleep(0) instead of Thread-sleep(1) also seems to iron out a few kinks. This would be because instead of actually waiting any amount of time (1000000ns = 1ms), I just go back through the while-loop "instantly", resulting in higher precision of when to get out of the while-loop, but also higher CPU-load, right? Well, setting it to Thread.sleep(1) yields 58-59fps and jerkiness.

C - Using Thread.yield() seems to give me the same performance that Thread.sleep(0) does. I haven't tested it with tools. I'd like to know which is better. I know yield() supposedly hogs the CPU, but wouldn't Thread.sleep(0) hog it just as much?

D - What do you guys use to test performance of Java Applets and their CPU-usage?

E - Toolkit.getDefaultToolkit().sync() doesn't seem to make a perceptible difference, like it did in my full-screen game (which is running very smoothly).

F - When I export it to a JAR and run it in Firefox through an HTML-document, it seems to run as it should (the movement looks like it's calculated smoothly) but the entire Applet has a spot of constant lag, which isn't there at all when running it from my IDE, AND the sleepTime is +1ms compared to the IDE. Is that just too bad? I'm on Windows 7, dual-core laptop, newest Firefox, newest Java 6 and 7 installed, using Java 6 for building.ANSWER: My laptop sucks My desktop computer is running it perfectly.

Side-note:I'm getting pretty tired of game-loops, and it seems everyone is having trouble with them. In most posts, people throw their code into the air, many other people nitpick at it, then all of a sudden the OP says "Yay, I fixed it using what X and Y said. THANKS!" and buggers off, not bothering to write up the code that solved it all for him. That's getting really tedious. Please post the loop you end up with, so newbies (like me) can see what happened. That would get us much less posts about this subject.

Here's a version of it with a sort of delta-timer implemented. The ball-movements are now using doubles for calculations, and only cast their double positions to int in their draw methods, which now get the deltaTime as a parameter for their calculations. As you can see, the balls now move at very different, but stable, velocities, and are all rendered with smooth movement. I might introduce Math-round() in the draw-calculations of the Ball to make them even more precise, but that's for another day when I make an object that isn't just a testing-ball.

This seems to be exactly what I need for my side-scroller I know that timing precise physics might require me to separate the physics from the render-loop, but having 60 updates per second is more than enough for me at the moment.

How do you see that, young master ra4king? Could you share how you test how much CPU time is being used on a particular application, in this case an Applet?

Well, the difference between my original loop and when I put in your snippet there instead, is that my sleep-time has dropped about 1½ millisecond, which seems to indicate it is doing a lot more thinking. Is this the tradeoff you've been talking about? A little more calculations, a lot less hogging?It still seems equally smooth, though.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org