Drawing pictures

Started by
1 comment, last by destructivArts 12 years ago
I'm writing a 2D side scroller 'engine' (I use this term loosely) in Java.
I have it set inside a class that extends JPanel, and implements Runnable.
I was having trouble with the image flickering, and, upon research, I learned that the repaint() method call is only a request, and may be processed at the CPU's convenience. So I did some more research and I decided to make my own RenderImage method.
The problem is, I'm not getting any errors from it, and yet it won't render to the panel. Both of these methods are called in a run() method once a frame. I'll post the code in question below. Any help is greatly appreciated.

//These are the global variables instantiated in the class outside these methods
public Image dbImage = null;
public Graphics dbg;
public void BufferImage(){
if(dbImage==null){
dbImage = createImage(PWIDTH,PHEIGHT);
dbg = dbImage.getGraphics();
}
//Individual Paint Functions Here
/* These most likely need to check the state first
REMINDER: Implement State Machine before going into all that
*/
dbg.setColor(Color.black);
dbg.fillRect(0,0,PWIDTH,PHEIGHT);
}

public void RenderImage(){
//Draws a pre rendered image of the game to the Panel
Graphics g;
try{
g = this.getGraphics();
if((g!=null)&&(dbImage!=null)){
g.drawImage(dbImage,0,0,this);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}catch(Exception e){
System.out.println("Graphics Context Error: " + e);
}
}


Thanks,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out
Advertisement
Perhaps you could post some more code?

I'm no java expert, but I think seeing the context in which these two are called might provide a little more insight to your problem.
I figured out that problem. You're right though Jebbles, the problem was with my Thread. When I instantiated it, I forgot to put 'this' as an argument.

I love small oversights like that.

Thank you anyways,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

This topic is closed to new replies.

Advertisement