title: paintComponent draws other components on top of my drawing
body: <p>I'm trying to build a simple paint tool. The <code>mouseDrag</code> events creates a new ellipse and causes my <code>JPanel</code> to <code>repaint()</code>.</p>
  
  <p>This works fine so far.
  However, if I press any button (or any other UI component) before firing the <code>mouseDrag</code> event for the first time, the button is painted in the upper left corner of my panel.</p>
  
  <p>I have isolated the code into this test application:</p>
  
  <pre><code>import java.awt.BasicStroke;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Test extends JFrame
  {
      public Test()
      {
          final JPanel paintPanel = new JPanel(){
              @Override
              protected void paintComponent(Graphics g)
              {
                  Graphics2D g2d = (Graphics2D)g;
                  g2d.setPaintMode();
  
                  g2d.setStroke(new BasicStroke(1));
                  g2d.fillRect(100, 100, 10, 10);
              }
          };
  
          paintPanel.setPreferredSize(new Dimension(300,300));
          paintPanel.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseClicked(MouseEvent e)
              {
                  paintPanel.repaint();
              }
          });
  
          this.setLayout(new FlowLayout());
  
          this.add(paintPanel);
          this.add(new JButton("Dummy"));
  
          this.pack();
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(true);
      }
  
      public static void main(String... args)
      {
          new Test();
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/lt8K1.png" alt="TestApp before clicking on the Panel">
  <img src="https://i.stack.imgur.com/k9vqT.png" alt="TestApp after clicking on the Panel"></p>
  
  <p><a href="https://i.stack.imgur.com/Ug4mD.png" rel="nofollow noreferrer">A Screenshot for "seeing" the problem in my Main application</a></p>