11 Replies - 1982 Views - Last Post: 19 September 2012 - 05:05 PM Rate Topic: -----

#1 amilner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-March 12

Double Buffering

Posted 18 September 2012 - 03:55 PM

I've been looking all over for a tutorial on double buffering. Could you please give me code that works? I've tried a few different methods from the internet, but it didn't seem to work. For an example, I'm trying to drive an animation with images, but when it switches between the images, it flashes just for the first couple times it switches images, and then it works fine. Thank you for your assistance. I would be just as happy with a good, working tutorial. Its just driving me crazy and I haven't been able to find anything.

Is This A Good Question/Topic? 0
  • +

Replies To: Double Buffering

#2 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Double Buffering

Posted 18 September 2012 - 04:33 PM

Swing JPanel implements double buffering for you. You don't even have to worry about it.

If, for learning purpose you want to create JPanel with double buffering turned off and implement your own double buffering mechanism it is another story. Use a Canvas and you can start from here

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

#3 amilner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-March 12

Re: Double Buffering

Posted 19 September 2012 - 06:07 AM

How do you keep the screen from going white for a split second every time you call super.paintComponent(g); ?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Double Buffering

Posted 19 September 2012 - 07:26 AM

Ypou probably don't call it correctly or at the good place
Show us your code
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

Re: Double Buffering

Posted 19 September 2012 - 11:17 AM

How often are you repainting? If you call repaint() every 30-40 milliseconds, you should be good.
Was This Post Helpful? 1
  • +
  • -

#6 amilner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-March 12

Re: Double Buffering

Posted 19 September 2012 - 01:23 PM

Sorry I cant really show you my code, its kind of in a big application.. I think macosxnerd101 was right. I just wasn't calling repaint() often enough.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

Re: Double Buffering

Posted 19 September 2012 - 01:29 PM

It's more the fact that you would have been calling repaint() too often that would have caused the flickering.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Double Buffering

Posted 19 September 2012 - 01:33 PM

View Postamilner, on 19 September 2012 - 04:23 PM, said:

I just wasn't calling repaint() often enough.

Flickering would be caused by calling it too often :)

*Edited: Ninja'd by Mac, I am in the train and the net is slow slow slow

This post has been edited by pbl: 19 September 2012 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#9 amilner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-March 12

Re: Double Buffering

Posted 19 September 2012 - 03:42 PM

Hmm.. maybe that didn't fix it.. I've found out that the flickering happens the first time the program draws the image.. But when it draws it again, its just fine. Any idea why this is happening?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

Re: Double Buffering

Posted 19 September 2012 - 03:57 PM

Not really. We haven't seen your code. :)
Was This Post Helpful? 0
  • +
  • -

#11 amilner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-March 12

Re: Double Buffering

Posted 19 September 2012 - 04:02 PM

Alright I wrote up an application to test my theory, and it has proven to be true. Don't mind my weird image names because I literally just chose a bunch of random pics off my desktop.

import javax.swing.*;
import java.awt.*;


public class Panel extends JPanel{
	//Variables
	ImageGetter i = new ImageGetter();
	JFrame frame = new JFrame("Flicker demo");
	String imagename;
	Image D1;
	Image House;
	Image Village;
	Image Star;
	
	public Panel(){
		frame.setSize(500, 500);
		frame.setContentPane(this);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		
		frame.setVisible(true);
		D1 = i.getImage("D1.png");
		House = i.getImage("House.png");
		Village = i.getImage("SimulatorFF3urvillage.jpeg");
		Star = i.getImage("Star.png");
		AnimationThread at = new AnimationThread();
		at.start();
		repainter();
	}
	
	public void repainter(){
		while(true){
			try{
				repaint();
				Thread.sleep(30);
			}
			catch(Exception e){
				
			}
		}
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(imagename.equals("House")){
			g.drawImage(House, 0, 0, 500, 500, this);
		}
		if(imagename.equals("Village")){
			g.drawImage(Village, 0, 0, 500, 500, this);
		}
		if(imagename.equals("Star")){
			g.drawImage(Star, 0, 0, 500, 500, this);
		}
		if(imagename.equals("D1")){
			g.drawImage(D1, 0, 0, 500, 500, this);
		}
		
	}
	
	
	public class AnimationThread extends Thread{
		public void run(){
			while(true){
				try{
					imagename = "House";
					Thread.sleep(1000);
					imagename = "Village";
					Thread.sleep(1000);
					imagename = "Star";
					Thread.sleep(1000);
					imagename = "D1";
				}
				catch(Exception e){
					
				}
			}
	}
	}
}



And then here is my ImageGetter class..


import java.awt.*;
import java.net.*;

public class ImageGetter {
	
	public Image getImage(String filename){
		Image image;
		URL url = this.getClass().getResource(filename);
		image = Toolkit.getDefaultToolkit().getImage(url);
		return image;
	}
}



and last but not least my main class


public class Flicker {
	public static void main(String [] args){
		new Panel();
	}
}



This post has been edited by amilner: 19 September 2012 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Double Buffering

Posted 19 September 2012 - 05:05 PM

You just can't mix Swing and Thread that way. You should use a Swing Timer rather than a Thread

One way to possibly fix the problem would be to exit from paintComponent() when you have a match

	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(imagename.equals("House")){
			g.drawImage(House, 0, 0, 500, 500, this);
			return;
		}
		if(imagename.equals("Village")){
			g.drawImage(Village, 0, 0, 500, 500, this);
			return;
		}
		if(imagename.equals("Star")){
			g.drawImage(Star, 0, 0, 500, 500, this);
			return;
		}
		if(imagename.equals("D1")){
			g.drawImage(D1, 0, 0, 500, 500, this);
			return;
		}


But the real way would be to have
Image imageToRedraw;
and in the actionPerformed() of the Timer simply assign or House, or Star, or D1 to imageToRedraw;
No if needed in paintComponent() anymore

Happy coding
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1