Double Buffering

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 2317 Views - Last Post: 20 August 2010 - 11:50 PM Rate Topic: -----

#1 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Double Buffering

Posted 14 August 2010 - 06:23 AM

I've tried making a small applet game and encountered flickering. Thought it wont happen, since there's not much happening on the screen, and it should basically draw one image per click. But then I decided to put a timer in my program and flickering began to happen. No problem, double buffering should solve this, but is it possible to draw images to an image? I've done applets with double buffering many times before but most of the time(actually every time) I've only used g2d and just painted, basically. Can I draw an image to a buffered image somehow? I've already looked at Tut section and other topics here, but it mostly covers g2d and painting. And, if its not possible, what should I do instead (to avoid flickering but still be able to draw images to the screen)?

Is This A Good Question/Topic? 0
  • +

Replies To: Double Buffering

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Double Buffering

Posted 14 August 2010 - 07:15 AM

You can create a buffered image with the contents of an image. Create the buffered image with the same width, hight and transparency of the image:

BufferedImage bufferedimage = gc.createCompatibleImage(
	            image.getWidth(null), image.getHeight(null), transparency);




Copy image to buffered image and paint the image onto the buffered image:

Graphics g = bimage.createGraphics();

	    g.drawImage(image, 0, 0, null);
	    g.dispose();


Was This Post Helpful? 1
  • +
  • -

#3 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Double Buffering

Posted 14 August 2010 - 10:11 AM

Yeah I get it. Just that I wanted to draw images to a buffered image. Images as ...objects that appear on the screen, not primitives that g2d/g offer.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Double Buffering

Posted 14 August 2010 - 12:03 PM

Not to preach for my parish but:

http://www.dreaminco...way-to-repaint/
Was This Post Helpful? 0
  • +
  • -

#5 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Double Buffering

Posted 15 August 2010 - 06:52 AM

I've already seen that tutorial of yours, Pbl :rolleyes: . Double buffering uses that backimage that doesn't show untill the painting is done, but it is still an image, so you don't actually draw directly to the canvas but to the invisible picture, basically. The thing bothering me is that I need to draw images on the screen(.png, .jpg or whatever...). If I draw em directly to the panel/canvas I get flickering. And I can't draw multiple images to an image, eh? :whistling:
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Double Buffering

Posted 15 August 2010 - 09:06 AM

You can draw multiple images to a BufferedImage, just like you can use image manipulation software to create an image from multiple images. :)
Was This Post Helpful? 0
  • +
  • -

#7 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Double Buffering

Posted 15 August 2010 - 09:33 AM

View Postmacosxnerd101, on 15 August 2010 - 08:06 AM, said:

You can draw multiple images to a BufferedImage, ...

That brings us to the original question... How? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Double Buffering

Posted 15 August 2010 - 10:49 AM

Show up your code. Unless you draw a very complicated images, there are no reasons for flickering
Was This Post Helpful? 1
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Double Buffering

Posted 15 August 2010 - 12:02 PM

View PostMercurial, on 15 August 2010 - 12:33 PM, said:

View Postmacosxnerd101, on 15 August 2010 - 08:06 AM, said:

You can draw multiple images to a BufferedImage, ...

That brings us to the original question... How? :helpsmilie:

Use the Graphics object returned by BufferedImage getGraphics(). It will draw to the BufferedImage.
Was This Post Helpful? 1
  • +
  • -

#10 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Double Buffering

Posted 17 August 2010 - 04:16 AM

public void paint(Graphics g){
		Graphics2D buff;
		buff= (Graphics2D) g;
		buff.setColor(Color.BLACK);
		
			buff.clearRect(0, 0, 800, 600);
			
			buff.setColor(new Color(0x116600)); // Green
			
			buff.fillRect(0, 0, 800, 600); // Background fill
			
			
			if(levelFinished==true){ // Player won?
				g.drawImage(win, 250, 150, this);
			}
			try { // draw all cards
			for (int i=0; i<CARDS_NUM/CARDS_PER_ROW; i++)
				for(int j=0; j<CARDS_PER_ROW; j++){
					
					if(cards[i*CARDS_PER_ROW+j].isExists()) // if card exists, draw it
					g.drawImage(cards[i*CARDS_PER_ROW+j].getActive(), cards[i*CARDS_PER_ROW+j].getX(), cards[i*CARDS_PER_ROW+j].getY(), this);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			
			buff.dispose();
		
	}

Here's the code. I'm not sure if you understood what I asked. I'm not using db here, and thats why I'm encountering flickering. And why am I not using db? Because I need to draw cards on the table. Well, I know how to draw cards to a canvas/panel, but DB uses backimage, which means I'd have to draw 20+ cards to the backimage itself (draw images to an image...).
Was This Post Helpful? 0
  • +
  • -

#11 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Double Buffering

Posted 17 August 2010 - 08:15 AM

Well I have an applet which show 75 ballots for bingo cards and I don't have any problem. So you can check it out:

public void init() {
		
		dim = getSize();
		
		setBackground(Color.black);
		// Create an mainScreen image to draw on
		mainScreen = createImage(dim.width, dim.height);
		// will be written on the mainScreen image.
		bufferGraphics = mainScreen.getGraphics();
	}

	public void paint(Graphics g) {
		bufferGraphics.clearRect(0, 0, dim.width, dim.width);
		bufferGraphics.setColor(Color.red);
			// draw every ballot using an image.
		
		String character = "";
		for (int x = 1; x < 76; x++) {
			if(x < 16){
				character = "B";	
			}else if(x < 31){
				character = "I";	
			}else if(x < 46){
				character = "N";	
			}else if(x < 61){
				character = "G";	
			}else if(x < 76){
				character = "O";	
			}
			if (x != 35) {
				bufferGraphics.drawImage(getImage(character + x + ".png"), 0,
						0, null); 
				// draw the offscreen image to the screen like a normal image.
				g.drawImage(mainScreen, 0, 0, this);
				try {
					Thread.sleep(500); //
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

public static Image getImage(String path) {
		Image image = null;

		try {
			// use ImageIO to read in the image
			image = ImageIO.read(DoubleBuffering.class.getResource(path));
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}

		return image;
	}


This post has been edited by guidojavier: 17 August 2010 - 08:17 AM

Was This Post Helpful? 1
  • +
  • -

#12 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Double Buffering

Posted 18 August 2010 - 12:27 PM

I made it work (kinda sure I've done the same thing I did the first time when it failed...). Oh well... Thanks guys
Was This Post Helpful? 0
  • +
  • -

#13 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Double Buffering

Posted 18 August 2010 - 03:13 PM

Would be so much easier to use JLabels for your cards...make a Card class that extends JLabel and take care of all appearance related things in there.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Double Buffering

Posted 18 August 2010 - 04:49 PM

Except that they are two different abstractions. When working with UIs, better to keep the UI separate from your data. So Card should be a class, and maybe you have a custom JLabel that paints based on a Card it has.
Was This Post Helpful? 0
  • +
  • -

#15 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Double Buffering

Posted 18 August 2010 - 04:51 PM

View Postmacosxnerd101, on 18 August 2010 - 03:49 PM, said:

Except that they are two different abstractions. When working with UIs, better to keep the UI separate from your data. So Card should be a class, and maybe you have a custom JLabel that paints based on a Card it has.

Alright will keep in mind :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3