Results 1 to 9 of 9
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Double Buffering an Application?

    So I used to create applets and I would use double buffering method for those. It seems like if you are using JFrames and JPanels that there may be a different way to double buffer things.

    For example, I can not for the love of god figure out how to double buffer this one JPanel I have. There are 5 buttons on it, and almost everytime the repaint method is called the button names (labels) change. And this causes a flickering. I assume it is because the names are being drawn directly to the screen, and not some offscreen image that I can throw straight up.

    Can someone send me a link on how to double buffer something like this or an example?

    Java Code:
    	private static void myPaint(Graphics g){
    
    		buddiesOnline.setBackground(Color.white);
    		buddiesOnline.setText(null);
    		for(int i=0; i<5; i++)
    			buttonBuddies[i].setText(null);
    		for(int i=0; i<counter; i++){
    			buddiesOnline.append(buddies[i] + "\n");
    			buttonBuddies[i].setText(buddies[i]);
    		}
    	}
    I'm using a Frame that has a mainPanel in it. The code above is a snapshot of what is causing the flicker. I'm removing all text in the buttons (because sometimes a button needs to be blank, and then fill in the buttons 1 at a time with an array that constantly changes.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you post a small compilable program that demonstrates your problem? Seeing the Graphics object as a parameter gives me the shivers and makes me worry about certain issues, but I won't know for sure without seeing code that I can compile and run on my own box. Also, are you calling Thread.sleep() anywhere?

    Much luck!

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    There are no sleep methods within this class. I'm not exactly sure how I can provide a compilable version of this code either as its part of a large structure. I'll see what I can come up with though. Give me a few minutes

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yeah, we'll not want to see the whole program, nor would we likely have time to go through it all. Your best bet is to condense your question/problem into a single small class that is compilable by any and all of us, and demonstrates your problem. In other words, a Short, Self Contained, Correct (Compilable), Example or SSCCE. It would take a bit of effort on your part to create this, but creating and posting one likely offers your best chance of getting a decent answer here.

    Much luck.

  5. #5
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Java Code:
    class Main {
    	public static JButton[] buttonBuddies = new JButton[100];
    	public Timer swingTimer = new Timer(1000, new TimerAction());
    	static String buddies[] = new String[100];
    	private static Dimension preferredSize = new Dimension(200,20);
    	public static int counter=0;
    
    	public Main(){
    		mainPanel.setDoubleBuffered(true);
    		swingTimer.start();
    		for(int i=0; i<5; i++){
    			buttonBuddies[i] = new JButton();
    			buttonBuddies[i].setPreferredSize(preferredSize);
    			buttonBuddies[i].setDoubleBuffered(true);
    			mainPanel.add(buttonBuddies[i]);
    		}
    	}
    	private static void myPaint(Graphics g){
    		for(int i=0; i<5; i++)
    			buttonBuddies[i].setText(null);
    		for(int i=0; i<5; i++){
    			buttonBuddies[i].setText(buddies[i]);
    		}
    	}
    
    	private static JPanel mainPanel = new JPanel() {
    		private static final long serialVersionUID = 1L;
    		protected void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			myPaint(g); // call all drawing code from this method in the body of this class
    		}
    	};
    	public Component getJPanel(){
    		return mainPanel;
    	}
    	private static void createAndShowGUI() {
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		JFrame frame = new JFrame("Buddy List");
    		frame.getContentPane().add(new Main().getJPanel());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setResizable(true);
    		frame.setSize(200,600);
    		frame.setVisible(true);
    		frame.setLocation((int) (screenSize.getWidth()-frame.getSize().getWidth()), 0);
    	}
    
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    	private class TimerAction implements ActionListener {
    		// the swingTimer will call this method every TIMER_DELAY mSecs
    		public void actionPerformed(ActionEvent arg0) {
    			for(int i=0; i<5; i++)
    				buddies[i]=String.valueOf((int)(Math.random()*100));
    			mainPanel.repaint();
    		}
    	}
    }
    Ok so the above should be simulating my problem. The difference is that on my SwingTimer I generate random numbers that change every repaint. This should simulate a list that comes to my client that gets updated contantly.

    As you can see, I have to set all the buttons labels to null before painting the labels with the correct names. This is because sometimes my list size can change.

    Again, this method seems to work, but with a lot of flickering. I've tried to set the components to doublebuffering(true) but the problem still seems to persist.

    Thanks
    Last edited by Moncleared; 09-14-2009 at 05:04 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that one of your problems is your changing button text from within a paint called method. Don't do that.

    Why not call the setText methods from within the timer itself?
    Last edited by Fubarable; 09-14-2009 at 05:24 AM.

  7. #7
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Hmm, see another problem is on my macosx i don't see this issue. But a friend on a windows box sees it =/

    Can you confirm you saw flickering and have you tested making the changes in the timer?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Yea my vista machine nor macosx do not see flickering as long as the setNames are done in the timer

Similar Threads

  1. run java application from desktop with double click
    By Rose88 in forum Advanced Java
    Replies: 4
    Last Post: 09-29-2009, 07:32 PM
  2. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 04:26 PM
  3. [SOLVED] Double Buffering
    By Zosden in forum AWT / Swing
    Replies: 1
    Last Post: 07-24-2008, 03:28 AM
  4. Replies: 0
    Last Post: 06-28-2008, 10:23 PM
  5. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •