Results 1 to 10 of 10
  1. #1
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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 first method, gameRender(), creates the double buffer image (dbImage_) and then draws on its graphics context.
    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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with drawing to a double buffer image

    Then it seems that dbImage_ is null?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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) ?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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 02:17 PM.

  10. #10
    Komposten is offline Member
    Join Date
    Nov 2011
    Location
    In the country in which my username is real word...
    Posts
    13
    Rep Power
    0

    Default 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

  1. Please help with Double Buffer
    By DouboC@gmail.com in forum Java Applets
    Replies: 2
    Last Post: 01-10-2011, 02:57 AM
  2. Double buffer help needed
    By pizzadude223 in forum Java 2D
    Replies: 22
    Last Post: 08-04-2010, 02:06 AM
  3. Double drawing problem with 2D
    By leapinlizard in forum Java 2D
    Replies: 4
    Last Post: 02-12-2010, 01:02 AM
  4. Need Help: Double Buffer
    By rukawa527 in forum AWT / Swing
    Replies: 1
    Last Post: 02-09-2009, 12:39 AM
  5. Can't DOUBLE BUFFER images
    By hunterbdb in forum Java 2D
    Replies: 7
    Last Post: 11-14-2008, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •