Results 1 to 8 of 8
- 04-10-2011, 05:20 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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?
-
- 04-10-2011, 05:43 AM #3
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);"
- 04-10-2011, 06:04 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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!
-
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.
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.I don't know why but for some reason, JPanel is not being double-buffered for you.
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 06:18 AM.
- 04-10-2011, 06:30 AM #6
Yeah I was just trying to use word play here. But still, calm down with the SSCCE hehe.
-
- 04-10-2011, 06:23 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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
GamePanel classJava 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); }
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
HUD_Status classJava 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); }
And the result looks like this: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(); }

Yah he doesn't have a head... This is a Ragnarok sprite for testing purposes haha.
Similar Threads
-
How do I create a simple stop commmand in an elementary Java program?
By dillmann74 in forum New To JavaReplies: 10Last Post: 08-14-2010, 09:16 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM -
a few questions about JPanel
By v1nsai in forum New To JavaReplies: 6Last Post: 08-22-2009, 05:45 PM -
Blinking stars
By jholtt23 in forum New To JavaReplies: 0Last Post: 02-19-2009, 05:38 AM -
www.javaadvice.com - The one stop resource for all your Java questions and answers
By JAdmin in forum Reviews / AdvertisingReplies: 0Last Post: 01-24-2008, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks