Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JFrame Image Flicker problem (Double Buffered)  (Read 3942 times)
0 Members and 1 Guest are viewing this topic.
Offline CoryG89

Senior Newbie





« Posted 2010-12-21 00:57:49 »

Ok, so I have been working on trying to get together some simple 2D demos in Java. I have been looking at using a JFrame/JPanel combination.

I was using a Timer and implementing ActionListener and this was working well, but I saw that most people were using a Thread and implementing Runnable so that the animations would work correctly. I tried doing this myself and have got it mostly working.

What is odd is now that I have implemented double buffering I have some flicker, when without it I was getting no flicker at all. The entire image is not flickering, only when two different images are drawn on top of each other. My entire background doesn't flicker at all. But when I try to draw my character it flickers whether it is moving or not. Almost as if java is redrawing them, but out of z-order sometimes causing a flicker.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public void run() 
   {
      bRunning = true;   // Keep flag set
     
      // Repeat update/animation loop, sleeping a bit
      while (bRunning)
      {
         updateBoard();
         renderBoard();
         repaint();
      }
     
      try
      {
         Thread.sleep(20);   // Sleep 20 millis
      }
      catch(InterruptedException ie)
      {
         System.err.println("Error: Thread exception!");
      }
     
      System.exit(0);   // end of run
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
   private void renderBoard()
   {
      // If null, create the buffer
      if (frameBuffer == null)
      {
         frameBuffer = createImage(WIDTH, HEIGHT);
         if (frameBuffer == null)
         {
            System.err.println("Error: frameBuffer is null");
         }
         else   // otherwise, write to Graphics2D object
            gDevice = frameBuffer.getGraphics();
      }
     
      // Draw background, using correct coordinates
      gDevice.drawImage(background, bX[0], 0, null);
      gDevice.drawImage(background, bX[1], 0, null);
      gDevice.drawImage(background, bX[2], 0, null);
      gDevice.drawImage(background, bX[3], 0, null);
     
      // Draw character on top of background
      // THIS IS WHAT FLICKERS
      gDevice.drawImage(gChar.getImage(), gChar.getX(),
                    gChar.getY(), null);
   }



1  
2  
3  
4  
5  
6  
7  
8  
   public void paintComponent(Graphics gDevice)
   {
      super.paintComponent(gDevice);
      if (frameBuffer != null)
      {
         gDevice.drawImage(frameBuffer, 0, 0, null);
      }
   }


Anyone have any ideas?

Offline Captain Awesome

Junior Devvie


Medals: 2


Hi


« Reply #1 - Posted 2010-12-21 16:16:13 »

Could you give us some screenshots showing the problem?
Offline nsigma
« Reply #2 - Posted 2010-12-21 16:30:27 »

Rough reading of the code you've posted would suggest you're rendering to the buffer in your animation thread and reading from it on the Swing thread without synchronising access, so sometimes you're writing and reading from the image at the same time!  I'd suggest either going back to Timer or reading up on active rendering.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CoryG89

Senior Newbie





« Reply #3 - Posted 2011-01-01 13:16:02 »

Oops forgot about this thread.   I found some articles on Active Rendering myself and I was able to fix my double buffering with no flicker at all, even while running different rates of frame animation on my sprites, and my top level animation loop.  It works perfectly now under all conditions so far. I just need to figure out how I can calculate my fps and output it to a string for debug now.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (62 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!