Flickering when repainting a JPanel inside a JScrollPAne

Posted by pR0Ps on Stack Overflow See other posts from Stack Overflow or by pR0Ps
Published on 2010-12-31T21:47:09Z Indexed on 2010/12/31 21:54 UTC
Read the original article Hit count: 214

Filed under:
|

I'm having a problem with repainting a JPanel inside a JScrollPane.

Basically, I'm just trying to 'wrap' my existing EditPanel (it originally extended JPanel) into a JScrollPane.

It seems that the JPanel updates too often (mass flickering). How would I stop this from happening? I tried using the setIgnoreRepaint() but it didn't seem to do anything.

Will this current implementation work or would I need to create another inner class to fine-tune the JPanel I'm using to display graphics?

Skeleton code:

public class MyProgram extends JFrame{

    public MyProgram(){
        super();
        add(new EditPanel());
        pack();
    }
    private class EditPanel extends JScrollPane{

        private JPanel graphicsPanel;

        public EditPanel(){
            graphicsPanel = new JPanel();
        }

        public void paintComponent(Graphics g){
            graphicsPanel.revalidate(); //update the scrollpane to current panel size
            repaint();

            Graphics g2 = graphicsPanel.getGraphics();
            g2.drawImage(imageToDraw, 0, 0, null);
        }
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about graphics