Jump to content
  • Advertisement
Sign in to follow this  
destructivArts

Drawing pictures

This topic is 2314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!