*
The moose likes Game Development and the fly likes Problem rendering with BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Problem rendering with BufferedImage" Watch "Problem rendering with BufferedImage" New topic
Author

Problem rendering with BufferedImage

Janis Strautins
Greenhorn

Joined: Dec 12, 2012
Posts: 18
Hi!
I'm using a buffered image to render a grid of squares, which is stored as an 2D array, each index contains it's coordinates to where it should be rendered and a boolean if it should be rendered. When the program starts up, one of the squares is rendered and i can move it with the arrow keys. For example, if i move left, then the current square is set to render=false and the one on the left is set to render true. The problem is, that the squares with render false keep being rendered, although that should not happen, sometimes even a half or a quarter keeps on rendering. If i check the values, they are correct. If you need any more code please ask, thanks in advance!

Render image to buffer:


Paint to screen:
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 432
    
    5
hi Janis,

well, your gameRender() method looks fine, as far as I can tell (just that line 12 is superfluous, given line 14).

It's your paintScreen() method that looks suspicious. In line 04 (this.getGraphics), what is this "this"? A JPanel?
And what is in your paintComponent() method (assuming there is one)?

To me, it looks like the only thing you have to do, after paintRender(), is to call "repaint();" and in
your paintComponent() method, just do a "g.drawImage(buffer, 0, 0, getWidth(), getHeight(), null).

What I suspect is the problem here is the double buffering that Java does behind the screen. So, try the repaint()
method, and let us know if that works.

Greetz,
Piet
Janis Strautins
Greenhorn

Joined: Dec 12, 2012
Posts: 18
Hi.
Thanks for your reply. Line 12 got deleted, don't know how it got there. The "this" is a JPanel, which is embed in a JFrame. I'm reading "Killer game programming in Java" (because of lots of positive feedback) and it's stated that it's better not yo use paint(), because the OS can call it when I really don't need it. So, i have my gameRender() which renders to a buffer,paintScreen() to put it on screen and setIgnoreRepaint(true) on the JFrame. Used this approach to create "The Game Of Life" and everything worked just fine(the loop is almost identical). I dont have a paintComponent method.

If I override paint() with my paintScreen(), then there is no change, but if i override it with my gameRender() then everything works like a charm, except I have no double buffering (except maybe for Javas behind the scene). Tried setting without the set ignore Repaint with the paint component method and repaint after the paintScreen, still no change. I have absolutely no idea why this is not working, maybe my paintComponent was useless so that's added below.

Maybe it's OK to use just paint() and repaint() and a BufferStrategy? The book is a bit old now, maybe it doesn't apply anymore.
Thanks!



UPDATE:
Found the problem, it was with the gameRender(). Added the bolded line and everything is rendering as it should. But the question still persists, what was the reason for that that behavior? The code ran flawlessly in another program.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem rendering with BufferedImage
 
Similar Threads
How to set image as background of JTree component
problems with transparent background
Display an image for a set time
Java swing
Improving Graphics