Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1726 times)
0 Members and 1 Guest are viewing this topic.
Offline wiesi

Senior Newbie




Java rulez!


« Posted 2003-05-25 14:10:56 »

Hy!

Can somebody please explain
me how to use double-buffering
in a Canvas or how to use the
GameCanvas?

Wiesi
Offline Binary_Chester

Junior Newbie




Mobile Java games rock!


« Reply #1 - Posted 2003-06-02 10:20:33 »

Normaly you should do something like this in your Canvas-Class:

// declare some member:
private Image scene;
private Graphics g;


Now you do all your painting on your self created g-Context, like this:

public void paint(Graphics gr) {

   scene = Image.createImage(getWidth(), getHeight());
   g = scene.getGraphics();

   g.setStrokeStyle(Graphics.DOTTED);
   g.drawLine( getWidth() / 2, 0, getWidth()/2, getHeight() -1);
   g.drawLine( 0, (int)line, getWidth(), (int)line );

   enemyList.paint( g );

   player.paint( g );

   // draw scene:
   gr.drawImage(scene,0,0, Graphics.TOP | Graphics.LEFT );
 }



Hope this helps!
Offline darcone

Junior Member




Size matters


« Reply #2 - Posted 2004-03-04 04:22:16 »

Is double buffering really needed in MIDP? The sun emulator gives me a OK result when I dont use double buffering...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Binary_Chester

Junior Newbie




Mobile Java games rock!


« Reply #3 - Posted 2004-03-04 04:40:57 »

This depends on the mobile! Siemens up to the SL55, for example, doesn't implement DoubleBuffering, so you have to do it on your own.

Ciao, Chess
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #4 - Posted 2004-03-04 05:32:30 »

it's not all so simple as that.
All Nokia devices are double buffered.
Some devies are not.

Fine, so you just double buffer all devices, and everythings good then? - NO, because this is where the fun begins Wink SOME devices are faulty in the most bizarre way.

Take some (all?) of the Series 60 devices from Nokia (6600, 3650...) do not support colors (defaults to black) on strings written to a buffered image.

So you basically have to write a game that works in buffered and unbuffered mode.

There are countless other bugs, which is why pro games on a phone can be rather tricky (unless you only support 1 phone).

Offline shmoove

Junior Member




Doh!


« Reply #5 - Posted 2004-03-04 14:17:36 »

In Binary_Chester's code he is creating a new image for the backbuffer on every frame. That's a big no-no. The backbuffer should be created once on start-up and cleared each frame with a big fillRect().

And about the whole "do I use double-buffering or not" issue. It's true that on most devices double-buffering is automatic, and implementing it yourself might even do some damage. But there is the isDoubleBuffered() method, so you could easily write code that handles both cases:
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  
26  
class MyCanvas extends Canvas {
  Image backbuffer;
  Graphics gBackbuffer;
  MyCanvas() {
    if (!isDoubleBuffered()) {
      try {
        backbuffer = Image.createImage(getWidth(),getHeight());
        gBackbuffer = backbuffer.getGraphics();
      }
      catch (Exception e) {
        // handle the exception as you see fit
     }
    }
  }
  public void paint(Graphics g) {
    Graphics gReal;
    if (gBackbuffer != null) {
      gReal = g;
      g = gBackbuffer;
    }
    // do your drawing on g
   if (gBackbuffer != null) {
      gReal.drawImage(backbuffer,0,0,Graphics.TOP|Graphics.LEFT);
    }
  }
}

It's still a bit dodgy on some device, but that's usually because those devices are bug-ridden so you'll need to write special workarounds for them in any case.

shmoove

[edit]
Oh my, what's wrong with the formatting in code tags?
[/edit]
Offline Binary_Chester

Junior Newbie




Mobile Java games rock!


« Reply #6 - Posted 2004-03-04 14:23:57 »

Quote
In Binary_Chester's code he is creating a new image for the backbuffer on every frame. That's a big no-no. The backbuffer should be created once on start-up and cleared each frame with a big fillRect().


Yes, you are right. I typed just the code in and assumed that someone who is familar with java sees the WAY how it works. Of course it is not a very good way to create with each paint a new Image... ;)
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.

Dwinin (29 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (39 views)
2014-09-09 02:24:56

mitcheeb (58 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!