7 Replies - 1253 Views - Last Post: 10 December 2011 - 11:59 PM Rate Topic: -----

#1 benjaluken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 11

Double Buffering a JLabel Icon?

Posted 09 December 2011 - 10:32 PM

Hi,

So, I've got my application working great. Now, I'm trying to embed it on a webpage. However, when my program begins the animation, it flashes horrendously, making the program unusable. After some searching, I found that I should probably implement double buffering. So, I set my JLabel.setDoubleBuffered(true), but it doesn't seem to be making a difference.

Below is the code.

public void actionPerformed(ActionEvent e) {	
	        
                timer.start();
	    	s = jtfPSent.getText();
	    	String string2  = s.toLowerCase();
	    	array = string2.toCharArray();
	    	
	    	   switch(array[index]){
	    	   case 'a': keyboard.setIcon(images[0]); break; 
	    	   case 'b': keyboard.setIcon(images[1]); break; 
	    	   case 'c': keyboard.setIcon(images[2]); break;  
	    	   //etc.
	    	  
	    	   }
	 	    
	    repaint();
	
	       ++index;
	       if(index == array.length){
	          timer.stop();
	          index = 0;



I feel like I should probably do something before the repaint() call, but after an hour or so of searching I'm not entirely sure what that might be.

Is This A Good Question/Topic? 0
  • +

Replies To: Double Buffering a JLabel Icon?

#2 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 207
  • Joined: 13-October 10

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 06:10 AM

I don't believe it is only the JLabel who is flashing.
Are all other components doing this also? Then you have to double buffer them too.

Another thing which will work is double buffering the whole applet.
You have to override the default update function.
Here is an much used example:
	private java.awt.image.BufferedImage dbimg; // double buffer image
	private java.awt.Graphics dbimgg; // double buffer image graphics
	@Override
	public void update(java.awt.Graphics g) // default awt function
	{
		int w = getWidth(); // store width in variable, so not to have reload it every time
		int h = getHeight(); // store height in variable, so not to have reload it every time
		if(dbimg==null || dbimg.getWidth()!=w || dbimg.getHeight()!=h){ // if image is null or the size of the image is not equal to the width of pane
			dbimg = new java.awt.image.BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_RGB); // create new image
			dbimgg = dbimg.getGraphics(); // load graphics
		}
		dbimgg.setColor(getBackground()); // set background color
		dbimgg.fillRect(0, 0, w, h); // draw background
		
		dbimgg.setColor(getForeground()); // set foreground color
		paint(dbimgg); // paint it into image
		
		g.drawImage(dbimg, 0, 0, w, h, this); // draw image to screen
	}

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:38 PM

You don't need to call repaint() with JComponent like JLabel
if you change it's property it will take care of repainting itself

What is your Timer delay ?
Was This Post Helpful? 0
  • +
  • -

#4 benjaluken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 11

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:42 PM

My timer delay is 250.

So I can get rid of the repaint() call?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:48 PM

View Postbenjaluken, on 11 December 2011 - 02:42 AM, said:

So I can get rid of the repaint() call?

Yes.. your problem is coming from somewhere else
Was This Post Helpful? 0
  • +
  • -

#6 benjaluken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 11

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:54 PM

Oh, wow. All I had to do was get rid of the repaint() call. Now it works just fine. Haha!

Thank you, guys.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:57 PM

That easy ?
:^:
Was This Post Helpful? 0
  • +
  • -

#8 benjaluken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 05-December 11

Re: Double Buffering a JLabel Icon?

Posted 10 December 2011 - 11:59 PM

Yes! I'm so surprised. Now I've just got to figure out how to get this to work in a browser!

It's for a graduate school work sample, to go along with my application, or I wouldn't even bother with the applet. They're asking for it to be "web-based" though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1