Jump to content

  • Log In with Google      Sign In   
  • Create Account


Drawing pictures


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 21 March 2012 - 03:36 PM

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

Sponsor:

#2 Jebbs   Members   -  Reputation: 280

Like
0Likes
Like

Posted 21 March 2012 - 07:49 PM

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.

#3 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 21 March 2012 - 08:02 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS