Timofei Davydik
Timofei Davydik

Reputation: 7303

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 each repaint and it looks really ugly. Please tell me, what I'm doing wrong? Thanks.

Upvotes: 3

Views: 3366

Answers (4)

shuangwhywhy
shuangwhywhy

Reputation: 5625

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.

Upvotes: 0

sayem siam
sayem siam

Reputation: 1311

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);
    }

Upvotes: 0

camickr
camickr

Reputation: 324197

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.

Upvotes: 2

jornb87
jornb87

Reputation: 1461

I think what you are looking for is double buffering.

Upvotes: 8

Related Questions