"Stuttering" in windowed mode using Java2D

Hey all,

Just two quick questions that I’m a bit stuck with at the moment.

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:true
MultiBufferAvailable: false
FullScreenRequired: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! :frowning:

My output:

Page flipping:true
Multi Buffer:false
Fullscreen required:false

I have never experienced any stuttering or something like that with Java2D. I’m also using it excactly the same way as you do.
What version of Java are you running and what os do you have?

Can you compile a test version so other people can see if the problem exists for them too?

Ah so the BufferCapabilities are the same then, so no problem there.

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.

Furthermore to my problem earlier, animation appears to run terribly on my laptop (which unfortunately has integrated graphics).

Can anything be done to improve running on integrated graphics to at least make it look smooth and not have lots of “stuttering”?

It runs perfectly full screen on my desktop (which has the graphics card).

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.

Could be related to this this topic:
http://www.java-gaming.org/index.php/topic,22762.0/topicseen.html

Dont’t forget to vote :slight_smile:

[quote=“Fid,post:1,topic:36473”]
What do you mean “going across the screen”? Dragging the window around with the mouse?

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.

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.


long lastTime = System.nanoTime();
int FPS = 60;

while(true) {
	long timeDiff = System.nanoTime()-lastTime;
	lastTime = System.nanoTime();
	
	//Game code
	
	try{
		long sleepTime = (1000000000/FPS) - (System.nanoTime()-lastTime);
		if(sleepTime < 0)
			continue;
		Thread.sleep((int)Math.round(sleepTime/1000000.0));
	}
	catch(Exception exc) {}
}

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.

No, I don’t. Of course not. And there is no performance problem, yielding 70% of the time.

ra4King:

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).

I made an error where I casted (System.nanoTime()-lastTime) to an int. See if that works now.
I use that loop for all my games without any problems ???

EDIT: What FPS are you using?

Using 60 FPS.

And still getting a few stuttering when full screen on my desktop, and quite a bit of stuttering on my laptop. Assuming it is still down to timing, or something else, going to have a look around.

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.

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.

@Fid
What 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).

Thanks for all your help guys, I really appreciate it!

I marvel at how you say the FPS is stable yet you blame the game loop :stuck_out_tongue:
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 :P.

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!)