开发者

Painting in Swing, blinking issue

I have the following problem in swing.

I'm implementing basic drawing operations (lines, shapes). When I'm moving mouse with pressed left button, I need to repaint current shape. So I clear the screen and repaint already drawn shapes and currently being drawn one.

Shapes are drawn in paint() method and on mouse move event I call repaint() (paint() is called automatically). The problem is that the screen is blinking strongly on eac开发者_StackOverflow社区h repaint and it looks really ugly. Please tell me, what I'm doing wrong? Thanks.


I think what you are looking for is double buffering.


Shapes are drawn in paint()

Custom painting should be done in the paintComponent() method and make sure you invoke super.paintComponent() as the first line.

Also custom painting is done on a JPanel (or JComponent), not on the JFrame directly.


I had flickering or blinking problem. I solved it using the following code.

    public void update(Graphics g) {
       paint(g);
    }

    @Override
    public void paint(Graphics g) {

        //super.repaint();
        if (myimg != null) {
            g.drawImage(myimg, 0, 0, this);
        }
        //update(g);
    }


You don't need to clear the screen, you just call repaint() then it's enough. If you have to clear the screen, it'll blink if you don't use synchronization, because the painting job is done in a separate thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜