Results 1 to 6 of 6
  1. #1
    dimril is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default 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:
    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();
    full code in attachment, binaries in rapidshare:
    RapidShare: 1-CLICK Web hosting - Easy Filehosting
    Attached Files Attached Files

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    All drawing should be done within the overwritten paintComponent(...) method of your JPanel.
    My Hobby Project: LegacyClone

  3. #3
    dimril is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    Take that code snippet you posted and place it in the paintComponent method.
    Java Code:
    public void paintComponent(Graphics g) {
       ...
    }
    Delete the first line
    Java Code:
    Graphics g = bf.getDrawGraphics();
    Change the next line to
    Java Code:
    super.paintComponent(g);
    And delete the last three lines,
    Java Code:
    g.dispose();
    bf.show();
    Toolkit.getDefaultToolkit().sync();
    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.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    I'll just add that Swing is double buffered by default.

  6. #6
    dimril is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. SWT ExpandBar flickering
    By Tomasz Wroniak in forum SWT / JFace
    Replies: 0
    Last Post: 12-02-2009, 12:23 PM
  2. Java Swing GUI Windows XP Flickering Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 12-12-2008, 02:07 PM
  3. Applet flickering
    By samson in forum Java 2D
    Replies: 3
    Last Post: 09-21-2007, 10:51 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
  •