Skip to main content

Swing is painting thrice

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2013-12-14

I have written a simple program where I am drawing a coloured rectangle on a JPanel placed inside a JFrame, and I wish to change its colour by user interaction such as mouse click.

However, I observed that as soon as the program runs, at the beginning itself the repaint() method of JPanel is called thrice, although I have called it only once in my program.

I want the swing to paint it only once, because I don't wish to change colour of the rectangle automatically at the start of the program.

I want to change it by user interaction.

I searched the net to find out that you don't have any control over how many times repaint() can get called.
In that case, how do I write my logic?

Any help is highly appreciated...
Thanks in advance.