FSEM headaches!

Ok, I’ve just started playing around with game animation type stuff. I figured out a lot with applets, using threads and double buffering backgrounds and animating pictures. Now I’m trying to learn the same stuff but with full screen (I don’t like the applet windows :slight_smile:

So I’m using JFrame to write an FSEM application. I’m having trouble figuring out how to do this. Naturally, I wanna use a thread like I did with the applet, but threads require runnable – should I have my FSEM implement runnable? Or will this make it less effecient/harm it in some way?

Also, for getting images from files, I was using getImage(getCodeBase(), “imagename.extension”), but this only works with applets. How do I do this without an applet?

Any help or pointers is appreciated!

http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html Check out this tutorial.

As far as for image loading, use ImageIO.

Good luck.

I really don’t like that tutorial :
I understand what it’s talking about when I read it, but I can’t figure out how to piece things together with actual code from it, and when I look at the example codes provided there, I can’t follow any of it.

Any way, I’ve made some progress, got things to show up and such, but now I’m having a problem with my double buffering maybe? Basically, I have a picture showing up on a blue screen and it changes every x frames (for animation). However, it’s blinking every time it re-draws the scene, any idea what I have wrong here?

public void render() throws java.io.IOException{
if(dbImage==null){
dbImage=createImage(this.getSize().width,this.getSize().height);
dbg=dbImage.getGraphics();
}

	dbg.setColor(getBackground());
	dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
	dbg.drawImage(image,x,y,this);
	
	displayImage(0,0,dbImage);

}

public void displayImage(int a, int b, Image i) {
Graphics g = getBufferStrategy().getDrawGraphics();
try {if(g!=null && i!=null)g.drawImage(i, a, b, null);}
finally {g.dispose();}
}

public void displayImage(Image i) {displayImage(x,y,i);}

Yes, it’s probably a problem with your double buffering. You need to add the following code after disposing the Graphics object:

	//if the buffer wasn't lost, flip the page
	if(!bufferStrategy.contentsLost())
		bufferStrategy.show();

The show method flips the buffer. When you create the BufferStrategy with the createBufferStrategy method, make sure you call it with an argument of 2. That makes it so that it’s double buffered. 1 buffer would just be the regular screen buffer. 2 buffers gives you the regular screen buffer plus 1 that you can switch with. When you draw to the Graphics object, you’re drawing to the off-screen buffer. The show method waits for the vertical retrace and then swaps the two buffers, causing whatever you drew to be displayed without flicker.

You don’t need to do your own double buffering by creating dbImage. You can just draw straight to the Graphics object from the BufferStrategy. The only reason I would ever have something like dbImage is if I had to do some kind of image effect to the screen before I displayed it. You would still need the double buffer then because Java gives you no way to wait for the vertical retrace yourself.

Yep, those were my problems exactly, thanks a bunch!

Oh and your explanation was KILLER! Understand buffer strategy a shitload better now! (compared to almost not at all though, haha)