Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 769
Timofei Davydik Avatar asked Dec 12 '25 16:12

Timofei Davydik


2 Answers

I think what you are looking for is double buffering.

like image 123
jornb87 Avatar answered Dec 14 '25 06:12

jornb87


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.

like image 37
camickr Avatar answered Dec 14 '25 04:12

camickr