Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Double buffering  (Read 1962 times)
0 Members and 1 Guest are viewing this topic.
Offline emprog

Junior Newbie





« Posted 2006-04-08 05:10:52 »

I am new to java and am trying to turn some of my C++ game projects into java applets. My double buffering is not working, I am getting a lot of flicker when I try to scroll the background. Can someone please tell me what I am doing wrong? I don't see how I am getting flicker since the map is being drawn to the buffer image, then to the screen.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void init( ) {
    tile[SKY] = getImage(getDocumentBase(), "sky.png");
    tile[CLOUD_1] = getImage(getDocumentBase(), "cloud_1.png");
    tile[CLOUD_2] = getImage(getDocumentBase(), "cloud_2.png");
    tile[RED_BRICKS] = getImage(getDocumentBase(), "red_bricks.png");
    buffer = createImage(APPLET_WIDTH, APPLET_HEIGHT);
    bufferg = buffer.getGraphics();
    addKeyListener(this);
  }
 
  public void paint(Graphics g) {
   
    if(buffer != null) {
      int tile_id;
      for(int y=0;y<MAP_ROWS;y++) {
        for(int x=0;x<MAP_COLS;x++) {
          tile_id = map[y][x];
          bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
        }
      }
    }
 
    g.drawImage(buffer, screenX, screenY, this);
  }
Offline DylanScott

Junior Newbie





« Reply #1 - Posted 2006-04-08 06:20:10 »

The way I always do DoubleBuffering is overriding update(); Here's how I do it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
private Image dbImage;
private Graphics dbg;
public void update(Graphics g)
{
   if(dbImage == null)
   {
      dbImage = createImage(this.getSize().width, this.getSize().height);
      dbg = dbImage.getGraphics();
   }
   dbg.setColor (getBackground ());
   dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
   dbg.setColor (getForeground());
   paint (dbg);

   g.drawImage (dbImage, 0, 0, this);
}
Offline emprog

Junior Newbie





« Reply #2 - Posted 2006-04-08 06:51:09 »

Now do I need to call update() explicitly? Or is it called automatically?

Right now, I am calling repaint() if certain keys are being pressed. Based on which keys are pressed I am either incrementing/decrementing from my maps world screen coordinates.

Would updating the buffer inside paint() cause the applet window to flicker?

Erick
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DylanScott

Junior Newbie





« Reply #3 - Posted 2006-04-08 07:08:25 »

update() is called automatically.  I know it's called in repaint(), but it might be called automatically in paint() to.
Offline emprog

Junior Newbie





« Reply #4 - Posted 2006-04-08 07:10:55 »

Nice, I got it to double buffer correctly, thanks for the information.

Erick
Offline noblemaster

JGO Ninja


Medals: 19
Projects: 9


Age of Conquest makes your day!


« Reply #5 - Posted 2006-04-08 07:28:53 »

update() is called automatically. Then update() clears the screen and calls paint(). The flicker comes from update() clearing the screen. So add the following code:

1  
2  
3  
4  
public void update(Graphics g) {
  // overrides update to prevent clearing of the screen
 paint(g);
}

Offline ENC

Junior Member





« Reply #6 - Posted 2006-07-10 10:16:15 »

is it possible to do double buffering on a JPanel?
Offline leisenstein

Innocent Bystander





« Reply #7 - Posted 2006-07-28 14:09:20 »

Here's a great article describing double buffering

http://www.developer.com/tech/article.php/626541
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #8 - Posted 2006-07-28 23:50:50 »

ENC, JPanels are double buffered by Swing. You can set a BufferStrategy for the panel's ancestral JFrame and do active rendering, but this implies that all the panels of the JFrame use that BufferStrategy. My solution is to use a java.awt.Canvas which can have its own BufferStrategy completely independent of other components. Then you have good control of the rendering process on the Canvas without having to worry about any other components.

However there are texts which advise against using AWT components since they are 'heavyweight' . Not that I have had performance problems with this.
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.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!