Firstly, after much researching and fiddling around with my game loop and drawing to the screen, I've managed to get rid of all the tearing and other problems. However one still remains... when windowed, my animation although most of the time smooth, when going across the screen can appear to "stutter".

I won't bother to post my game loop (I can if you think it's important though) but I think it is correct, runs at a solid 60fps and when fullscreen - all animation is as smooth as butter! Therefore it has lead me to believe it is not timing that is an issue, just Java2D itself? Would I be right in this?

Is there *anything* I could possibly do to stop this in Windowed mode?

Secondly, I called three methods with BufferCapabilities to see what was happening, this was the output:

PageFlipping:trueMultiBufferAvailable: falseFullScreenRequired:false

Surely MultiBufferAvailable should be true? I'm using BufferStrategy... set up as... createBufferStrategy(2); and I get the graphics from the buffer, write to buffer, then show buffer. So is that correct or am I missing something?

Any help would be appreciated! (Would be good to know if it is possible to achieve smooth animation in a window, or only full screen at the moment).

P.S. Although I know the solution would be to use LWJGL etc, for now I've stuck with doing it the hard way heh!

I'm using the latest version of Java on Windows 7 x64. I shall also go and try on my laptop to see if I can reproduce it there - if I can, I'll compile a little test version for others to try.

It's hard to describe exactly the stuttering, it's more like when moving across a page every now and again for 0.5 a sec it appears it needs to "catch up"... something that doesn't happen at all when I go full screen.

If by running poorly you mean not fluid, then check your game loop. You should accommodate for slower computers by checking for time spent updating all components and subtracting that from the actual sleep time.

ra4king: That could possibly be the problem, in the loop I do just have a sleep(10) and that remains the same throughout. And yep I mean not fluid, although it seems to run fluidly full screen on my Core 2 Quad machine, on my i3 laptop whether windowed or fullscreen it runs the same - so this does now suggest it isn't tearing or a Java2D bug but some timing error.

Can anyone recommend a good loop, or run through the basics on timing what should happen in a loop? (i.e. what things need to be timed and then subtracted etc)?

Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!

adon_y_coya: Sorry that's my poor explaining, I mean't say we have a square 150x150 image that is supposed to glide (move) from one side of the screen to the other. That is where the stuttering occurs, on that sprite/image.

Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!

Beside the tearing (not my main complaint), sometimes I have occational stuttering for about 0.3 to 1 second. As if the screen gets only 10 updates per second. It is not the GC. Like when I run an Applet in a browser with OS X and Java 1.6: the frame counter says 60fps but obviously the visible area is updated with 5 fps.

Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!

Beside the tearing (not my main complaint), sometimes I have occational stuttering for about 0.3 to 1 second. As if the screen gets only 10 updates per second. It is not the GC. Like when I run an Applet in a browser with OS X and Java 1.6: the frame counter says 60fps but obviously the visible area is updated with 5 fps.

Are you using separate threads for the game loop and redraw? If you are then the frame counter only checks the game loop when your redraw thread could be stuttering.

Tried the example loop and got the stuttering in full screen that I didn't have before... so I'm doing something wrong somewhere and I think it *must* be the timing?

Probably the most confusing bit ever with building a game engine heh!

I'm thinking that it however must definitely be the game loop that is the problem... one loops makes it even worse on my laptop, the other works but still stutters, and the same for my desktop. Just need to get that right, don't think Java2D is the problem here (as I haven't seen any tearing etc).

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.

Probably not related, but it's worth asking. Which datatype do you use for position and motion? int, long, float, double? It's been a while, but I distinctly remember in some cases where objects moved across the screen, that changing from floats to doubles would help motion appear smoother. But that could have been some weird freaky artifact.

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.

It is a very bad design to have the game loop in the constructor. It is advisable to start a new thread for the loop.

@FidWhat kind of stuttering do you see? The FPS not being stable or the character not moving smoothly?

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.

It is a very bad design to have the game loop in the constructor. It is advisable to start a new thread for the loop.

@FidWhat kind of stuttering do you see? The FPS not being stable or the character not moving smoothly?

The FPS is continually stable, the character is just not moving smoothly. That could be because I have something like, if left pressed, x+2.5 and then it would draw the sprite 2.5 pixels to the left - could that be the problem?

I think I'm using doubles (I'll have to check that, I can't remember now if I used float or double and I'm not at my computer to see the code right now heh).

I marvel at how you say the FPS is stable yet you blame the game loop If so, then you probably don't use floating points to store x and y, which cause the character to stutter when moving decimal in values.

Yep I was using Double - moving to Float now see if that works, which I'm guessing it should based on your reply .

And you know probably when I started it probably was down to the game loop (it was incredibly basic and sleep was just a set variable, therefore performed badly on my laptop when compared to desktop. Now that is resolved and it attempts to stick to a certain FPS, it obviously has to be something else (well, I've elimated everything else heh!)

Although I'm confused now ra4king - when I implemented a FPS counter, (tried it in several different ways) it always gave the correct FPS (to what I had set it to) - yet there would still be stuttering now and again.

If it isn't the gameloop then (as the FPS is reported as being 60)... what else could it be? (Or was your comment on the fact that the FPS could have possibly be being reported incorrectly).

Using an external timer seems to be working better really at the moment (unless my eyes have just stopped being fussy and are starting to ignore the slight stutter lol).

when I implemented a FPS counter, (tried it in several different ways) it always gave the correct FPS (to what I had set it to) - yet there would still be stuttering now and again.

I don't know if it will help, but you could try looking more closely at exactly how constant your frame-rate is. (To be honest, I'm a little suspicious when you say the counter always gives you the "correct FPS". You mean it always says 60, never 59.9 or 60.1? What period are you averaging the FPS over?)

For 60 frames-per-second, the value of nanoTime() at the start of the current loop minus the value at the start of the previous loop would ideally be around 1e9/60 (=16,666,666). Try printing out the value if it is significantly different from that (for instance, less than 5,000,000 or more than 30,000,000). Do you see these numbers do anything strange when the game stutters? What are the most extreme numbers that are printed?

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