I am using a custom JPanel with a background image. I have put the background image by overriding the panel's paintComponent() method. The layout of the panel is set to GridBagLayout. Now I am trying to add another JPanel to it on which I am drawing a rectangle. 
When I am trying to do that the rectangle is drawn at arbitrary positions and its duplicates are also being drawn. When I remove the background image of the first panel everything works fine.
How to solve this?