Results 1 to 10 of 10
- 03-04-2012, 08:56 PM #1
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Problem with drawing to a double buffer image
Hello!
I recently bought O'Reilly's Killer Game Programming in Java book.
Yesterday I wrote the first example game in Eclipse to try it out, though something is failing...
I've tracked the fault down to a certain part of the code, and there are two possibilities:
1. The drawing to the double buffer image fails.
2. Drawing the double buffer image on the JPanel graphics fails.
Here's the code, can you find my failure? (The code is identical to the one in the book, which was primarily designed for java 1.4)
Java Code:private void gameRender() { if (dbImage_ == null) { dbImage_ = createImage(WIDTH, HEIGHT); if (dbImage_ == null) { System.out.println("dbImage is null!"); return; } else { dbGraphics_ = dbImage_.getGraphics(); } } dbGraphics_.setColor(Color.black); dbGraphics_.fillRect(0, 0, WIDTH_, HEIGHT_); dbGraphics_.setColor(Color.black); obstacles_.draw(dbGraphics_); worm_.draw(dbGraphics_); if (isGameOver_) gameOverMessage(dbGraphics_); } private void paintScreen() { Graphics g; try { g = this.getGraphics(); if ((g != null) && (dbImage_ != null)) { g.drawImage(dbImage_, 0, 0, null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); } catch (Exception e) { System.out.println("Graphics Context Error - In paintScreen() - " + e); } }
The second method, paintScreen() fetches the JPanel's graphics context and draws the dbImage_ onto it.
Both methods are located in a class extending JPanel.
Thanks in advance, Komposten
- 03-04-2012, 09:55 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Problem with drawing to a double buffer image
Most likely the Graphics object 'g' in line #34 is null; better override the paintComponent(Graphics g) method and get a Graphics object for free,
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-05-2012, 10:29 AM #3
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Re: Problem with drawing to a double buffer image
When tracking the problem I made a lot of tests. One of these tests was whether the if statement at line #35 passed or not. Tested this both by printing a line to System.out, and also by drawing a square straight to 'g'. The line was printed, and the square was drawn to the screen, though 'dbImage_' was not.
- 03-05-2012, 11:01 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Problem with drawing to a double buffer image
Then it seems that dbImage_ is null?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-05-2012, 11:34 AM #5
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Re: Problem with drawing to a double buffer image
But how can dbImage_ be null when it passes the test on line #35? It must be assigned a reference to pass, right?
- 03-05-2012, 11:41 AM #6
Re: Problem with drawing to a double buffer image
You have a synchronicity problem, most likely arising out of calling Swing methods off of the EDT.
Listen to what JosAH says about overriding paintComponent and do your custom painting the correct way. Swing components are double buffered by default, so you don't need to implement double buffering yourself. You appear to be following an old outmoded AWT approach -- are you referring to some book or online resource that has long lost its relevance?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 11:50 AM #7
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Re: Problem with drawing to a double buffer image
As I mentioned in my first post, I've got the code from O'Reilly's Killer Game Programming book, it was written in 2004/2005 and covers Java 1.4 (Java 4) and partly Java 5.
As the code is quite old it might as well be outdated, though I thought Java are built in a backward compatible way?
I will try overriding paintComponent and see if it still works as it was supposed.
- 03-05-2012, 11:56 AM #8
Re: Problem with drawing to a double buffer image
Java is backward compatible, but some of the concurrency issues weren't recognized early on.
Even between Java 6 and 7, the 'thread safe' rider on the API of several Swing methods, including many of the Document related methods of JTextComponents, has been removed following multiple bug reports and a more stringent analysis of program flow.
Have you gone through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) ?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 12:35 PM #9
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Re: Problem with drawing to a double buffer image
The problem with using paintComponent and repaint is that I can not keep track of the time it takes to draw the image, and therefore I would have problems with balancing the FPS/UPS. Or is there a way to know when repaint/paintComponent has finished?
And no, I haven't known about that lesson until now, so I will take some time to read it.
EDIT:
I made some fast changes, like replacing paintScreen() with paintComponent(Graphics g), and the call to it now calls repaint(). I put the drawing inside paintComponent and they are drawn straight to 'g'. I still do not get an image however, and possibly it now fails in invoking paintComponent.
EDIT 2:
I found a minor failure coming from Eclipse's Refactor function, which caused the problem with paintComponent. Now I do get an image! :D
Will try a few other methods, to see what works and what do not.Last edited by Komposten; 03-05-2012 at 03:17 PM.
- 03-05-2012, 06:30 PM #10
Member
- Join Date
- Nov 2011
- Location
- In the country in which my username is real word...
- Posts
- 13
- Rep Power
- 0
Re: Problem with drawing to a double buffer image
Everything seem to be working right now, except for a few exception errors every now and then. I'm going to look into that.
Though, since those bugs got nothing to do with the current topic, and I do not look for help on those issues, I thank you all for your help.
And now I suppose this thread can be marked as closed/solved, or whatever...
/ Komposten
Similar Threads
-
Please help with Double Buffer
By DouboC@gmail.com in forum Java AppletsReplies: 2Last Post: 01-10-2011, 03:57 AM -
Double buffer help needed
By pizzadude223 in forum Java 2DReplies: 22Last Post: 08-04-2010, 03:06 AM -
Double drawing problem with 2D
By leapinlizard in forum Java 2DReplies: 4Last Post: 02-12-2010, 02:02 AM -
Need Help: Double Buffer
By rukawa527 in forum AWT / SwingReplies: 1Last Post: 02-09-2009, 01:39 AM -
Can't DOUBLE BUFFER images
By hunterbdb in forum Java 2DReplies: 7Last Post: 11-14-2008, 09:53 PM
Bookmarks