Results 1 to 6 of 6
Thread: flickering problem
- 06-01-2010, 11:33 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
flickering problem
I write a game (Lunar lander) and I've got a problem with flickering, but when I put it in double buffer, everything becomes screwed - it just doesn't show or flickers so badly that nothing is visible.
I paint in a panel, it looks like this:
full code in attachment, binaries in rapidshare:Java Code:Graphics g = bf.getDrawGraphics(); super.paint(g); RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); ((Graphics2D) g).setRenderingHints(rh); Graphics2D g2d = (Graphics2D)g; currentLevel.draw(g2d); if (craft.isVisible()) g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this); else if (craft.isCrashing()) g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this); if (ingame) { craft.drawClocks(g2d); } else if (timeBuffer < 0){ String msg = "Game over"; setBackground(Color.BLACK); g.setColor(Color.WHITE); if(craft.isVisible()) { msg = "Congratulations!"; setBackground(Color.WHITE); g.setColor(Color.BLACK); } Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g.setFont(small); g.drawString(msg, (getWidth() - metr.stringWidth(msg)) / 2, getHeight() / 2); } g.dispose(); bf.show(); Toolkit.getDefaultToolkit().sync();
RapidShare: 1-CLICK Web hosting - Easy Filehosting
- 06-01-2010, 09:34 PM #2
All drawing should be done within the overwritten paintComponent(...) method of your JPanel.
My Hobby Project: LegacyClone
- 06-02-2010, 02:25 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
using paintComponent() unfortunately did nothing to help :/
[edit] I put setDoubleBuffered(true) in a constructor, but I don't know if it ensures doublebuffering. I need it only to be, because it's my semester project on Java.Last edited by dimril; 06-03-2010 at 03:07 PM.
- 06-09-2010, 02:35 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
Take that code snippet you posted and place it in the paintComponent method.
Delete the first lineJava Code:public void paintComponent(Graphics g) { ... }
Change the next line toJava Code:Graphics g = bf.getDrawGraphics();
And delete the last three lines,Java Code:super.paintComponent(g);
Basically, get rid or your BufferStrategy. You're not doing active rendering so it's not needed. And it's intertwining with Swing's own repaint mechanism to cause the flicker.Java Code:g.dispose(); bf.show(); Toolkit.getDefaultToolkit().sync();
- 06-09-2010, 04:44 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I'll just add that Swing is double buffered by default.
- 06-11-2010, 08:52 AM #6
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
SWT ExpandBar flickering
By Tomasz Wroniak in forum SWT / JFaceReplies: 0Last Post: 12-02-2009, 12:23 PM -
Java Swing GUI Windows XP Flickering Probelm
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 12-12-2008, 02:07 PM -
Applet flickering
By samson in forum Java 2DReplies: 3Last Post: 09-21-2007, 10:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks