Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 2092 times)
0 Members and 1 Guest are viewing this topic.
Offline emprog

Junior Newbie





« Posted 2006-04-08 03: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 04: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 04: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 05: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 05:10:55 »

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

Erick
Offline noblemaster

« JGO Spiffy Duke »


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #5 - Posted 2006-04-08 05: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 Devvie





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

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

Innocent Bystander





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

Here's a great article describing double buffering

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

Junior Devvie




Java games rock!


« Reply #8 - Posted 2006-07-28 21: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.

Mr.CodeIt (9 views)
2014-12-27 04:03:04

TheDudeFromCI (13 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!