2 Replies - 3045 Views - Last Post: 25 August 2010 - 09:28 AM Rate Topic: ***** 1 Votes

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Java - Double buffering in applications

Posted 25 August 2010 - 08:25 AM

Im trying to doublebuffer an image for an application, NOT applet.
I think im missing a step in the double buffer process.

Errors:
Null pointer exception -> doublebuffer = offscreen.getGraphics();

Class (JPanel)
...
// declare
Image
Image offscreen;
Graphics doublebuffer;

public Board() {
...
// init
Image img = Toolkit.getDefaultToolkit().getImage("CannonBall.png");
offscreen = createImage(800, 600);
doublebuffer = offscreen.getGraphics();
}

public void paint(Graphics g) {
super.paint(g);
...
// clear the past screen
doublebuffer.clearRect(0,0,800,600);
// draw to doublebuffer
doublebuffer.drawImage(img, 0, 0, null); // background
}

Is This A Good Question/Topic? 0
  • +

Replies To: Java - Double buffering in applications

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Java - Double buffering in applications

Posted 25 August 2010 - 08:46 AM

Can you post some more code as well as the Stack trace? I'd be interested to see the createImage() method you define as well.
Was This Post Helpful? 0
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Java - Double buffering in applications

Posted 25 August 2010 - 09:28 AM

View Postmacosxnerd101, on 25 August 2010 - 07:46 AM, said:

Can you post some more code as well as the Stack trace? I'd be interested to see the createImage() method you define as well.

As it turns out i figured out the problem which was easier
than i thought. The double buffer can't be initialized
in the constructor so i moved the code to initialize the
double buffer into the 'paint()' method. First time i
achually got double buffering to work in Java :yes:

The createImage(width, height) method is a built-in
function, im guessing it creates an empty image to
draw on.

http://download.orac...createImage(int,%20int)

Double Buffer
        ...
        Image img = Toolkit.getDefaultToolkit().getImage("CannonBall.png");
	Image offscreen;
	Graphics2D doublebuffer;

        public Board() {
	...
	}
        public void paint(Graphics g) {
		super.paint(g);
	        Graphics2D g2 = (Graphics2D)g; 

                // Initialize doublebuffer
		if (offscreen == null) {
		    offscreen    = createImage(800, 600);
		    doublebuffer = (Graphics2D) offscreen.getGraphics();
		}
                
                ...
                // clear past images off of doublebuffer
                doublebuffer.clearRect(0, 0, 800, 600);

                //draw what you need to draw
                doublebuffer.drawImage(img4, x, y, null); // tank
                ...

                //draw it all the screen
                g.drawImage(offscreen, 0, 0, null);
         }


This post has been edited by Java Student: 25 August 2010 - 09:32 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1