Results 1 to 8 of 8
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Simple Questions- Stop JPanel Blinking

    I have a JPanel called "display" that i add to the Frame. Originally I drew directly to the Graphics object of "display" and the display did not blink. Now I've realized I will need to have a few layers because I need to utilize some JComponents so I've created a JPanel called "GameDisplay" and added it to the "display" JPanel. JPanel is still added to the frame and "GameDisplay" to "display". Now I draw directly to the graphics object of "GameDisplay". However, now every time paintComponent is called the whole screen blinks. Everything wipes to the frame's background color momentarily then I can see the JPanel drawn (I have a 16bit video card so this is very visible to me). I think there is a way to stop the screen from clearing before it draws again. How is this done?

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

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Fubarable, you're getting quite UN-barable with SSCEE ;)
    The question is perfectly understandable.

    I don't know why but for some reason, JPanel is not being double-buffered for you. Double buffering is when all the graphics is drawn to a hidden buffer then the entire screen is drawn at once so there would be no "flicker" or "blinking".
    Try doing "display.setDoubleBuffered(true);"

  4. #4
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Ah thank you very much. Also, i just realized something that has made the drawing process much much much faster. I originally did all of the drawing process inside the run method but for whatever reason it's much faster inside the paintComponent() method.

    Thank you for your help!

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

    Default

    Quote Originally Posted by ra4king View Post
    Fubarable, you're getting quite UN-barable with SSCEE ;)
    The question is perfectly understandable.
    There's no crime in asking for one. Yes it's wrong to demand it, but if the OP doesn't want to provide one, all he has to do is either say no, or ignore my request. And if my request bothers you, I'm sorry as that certainly wasn't my intention, but you can do the same and ignore it.

    I don't know why but for some reason, JPanel is not being double-buffered for you.
    And I don't know why either -- hence my request for clarification, but often it's because a coder tries to do some time-delaying code in the paintComponent such as a bunch of object creation or file manipulation. If he can reproduce the problem in a small program that we can run, I'll bet you we'd both know, but otherwise all were doing is guessing. The other benefit of an SSCCE is that often the act of trying to create it exposes the problem to the OP in isolation of everything else, and they discover the solution for themselves, which in my book is the best outcome anyone can have in these forums.

    YMMV.

    edit: well never mind in this situation as the OP has solved it. to the OP, glad you've solved it!
    Last edited by Fubarable; 04-10-2011 at 07:18 AM.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yeah I was just trying to use word play here. But still, calm down with the SSCCE hehe.

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

    Default

    Quote Originally Posted by ra4king View Post
    Yeah I was just trying to use word play here. But still, calm down with the SSCCE hehe.
    Sorry, but it's probably not going to happen, as I am an SSCCE fan. I'm not as much of a fanatic about this as camickr or Andrew Thompson are, but then again, I'm not nearly as smart as they are and never will be.

    Cheers.

  8. #8
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Alright well I've got another problem that is related (same effect). It's also another really simple one. So if no one minds I will post it here again instead of making a new topic. I did make a collegiate effort to solve this issue on my own though. Hope I'm not bothering anyone.

    I cannot post a SSCCE version because this program is many classes long and any one piece that is missing would break it. But I will try to explain the problem best I can.

    So like I said before I'm layering the panels now. Each panel has the same width/height and start at 0,0. I'm trying to set HUD to be drawn on top of GP but for some reason it's blinking. I've tried setDoubleBuffered(true) on all the JPanels but it doesn't help.

    Display class

    Java Code:
    public Display(~hidden~ d)
    	{
    		DC = d;
    		GamePanel GP = DC.getGamePanel();
    		HeadsUpDisplay HUD = DC.getHeadsUpDisplay();
    		int width = DC.getClientWidth();
    		int height = DC.getClientHeight();
    		
    		Frame = new JFrame();		
    		Frame.setSize(width, height);
    		Frame.setVisible(true);
    		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Frame.add(this, null);
    		Frame.setResizable(false);
    		
    		Dimension size = Frame.getSize();
    		
    			this.setDoubleBuffered(true);
    			this.setPreferredSize(new Dimension(width, height));
    			this.setLayout(null);
    			GP.setBounds(0, 0, width, height);
    			HUD.setBounds(0, 0, width, height);
    			this.add(GP);
    			this.add(HUD);
    			this.setComponentZOrder(HUD, 0);
    			this.setComponentZOrder(GP, 1);
    			Frame.getContentPane().addMouseListener(this);
    		
    		//accounts for the chrome
    		Insets inset = Frame.getInsets();
    		int insetWidth = inset.left + inset.right;
    		int insetHeight = inset.top + inset.bottom;
    		
    		System.out.println(insetHeight);
    		
    		Frame.setSize(width+insetWidth, height+insetHeight);
    		
    		t = new Thread(this);		
    	}
    GamePanel class

    Java Code:
    public GamePanel(~hidden~ d)
    	{
    		DC = d;
    		int width = DC.getClientWidth();
    		int height = DC.getClientHeight();
    		this.setPreferredSize(new Dimension(width, height));
    		this.setDoubleBuffered(true);
    		t = new Thread(this);
    		t.start();
    	}

    HeadsUpDisplay class

    Java Code:
    public HeadsUpDisplay(~hidden~ d)
    	{
    		DC = d;
    		int width = DC.getClientWidth();
    		int height = DC.getClientHeight();
    		this.setPreferredSize(new Dimension(width, height));
    		this.setDoubleBuffered(true);
    		HS = new HUD_Status(DC);
    		
    		this.setLayout(null);
    		this.add(HS,0);
    		HS.setBounds(0, 0, 152, 54);
    		
    		//for testing purposes
    		JTextArea textArea = new JTextArea();
    		textArea.setPreferredSize(new Dimension(200, 20));
    		this.add(textArea);
    		textArea.setBounds(0, height-20, 200, 20);
    	}
    HUD_Status class

    Java Code:
    public HUD_Status(~hidden~ d)
    	{
    		this.setDoubleBuffered(true);
    		Graphics g;
    		DC = d;
    		image = new BufferedImage(152, 54, BufferedImage.TYPE_INT_ARGB);
    		baseImage = new BufferedImage(152, 54, BufferedImage.TYPE_INT_ARGB);
    		
    		try
    		{
    			hpBarImage = ImageIO.read(new File("src/images/hud/hpbar.gif"));
    			mpBarImage = ImageIO.read(new File("src/images/hud/mpbar.gif"));
    			expBarImage = ImageIO.read(new File("src/images/hud/expbar.gif"));
    			Image img = ImageIO.read(new File("src/images/hud/status.png"));
    			g = (Graphics2D)baseImage.getGraphics();
    			g.drawImage(img, 0, 0, null);
    		}catch(Exception e){}
    		
    		t = new Thread(this);
    		t.start();
    	}
    And the result looks like this:



    Yah he doesn't have a head... This is a Ragnarok sprite for testing purposes haha.

Similar Threads

  1. Replies: 10
    Last Post: 08-14-2010, 10:16 PM
  2. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 01:50 PM
  3. a few questions about JPanel
    By v1nsai in forum New To Java
    Replies: 6
    Last Post: 08-22-2009, 06:45 PM
  4. Blinking stars
    By jholtt23 in forum New To Java
    Replies: 0
    Last Post: 02-19-2009, 06:38 AM
  5. Replies: 0
    Last Post: 01-24-2008, 08:53 PM

Posting Permissions

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