title: Trying to draw multiple polygons results in error
body: <p>Following my previous post <a href="https://stackoverflow.com/questions/15649228/cannot-create-multiple-polygons-in-java-only-one/15649335#15649335">here</a> , I changed the code to  :</p>
  
  <p><strong>PolygonnerJframe.java</strong> </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Point;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseMotionListener;
  import java.util.ArrayList;
  import java.util.Iterator;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  
  /**
   * 
   * @author X2
   *
   */
  public class PolygonnerJframe
  {
      public static void main (String[] args)
      {
       JFrame frame = new JFrame("Draw polygons");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setContentPane(new DrawingPanel());
       frame.pack();
       frame.setVisible(true);
   }
  }
  </code></pre>
  
  <p>Now , this code results in : <img src="https://i.stack.imgur.com/hisw8.gif" alt="this"></p>
  
  <p>And I can't understand what causes this . </p>
  
  <p>The changes that I've made are : when we're done with a polygon , its coordinates are saved in the arrayList of class <code>Polygon</code> , and each time that I create a new polygon , I take 
  the previous polygons and draw them , while drawing a new polygon . </p>
  
  <p>As you can see above , something went wrong with the drawing and I can't seem to find 
  the problem .</p>
  
  <p>I'd appreciate any help . </p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>After taking into consideration what @StanislavL said , I moved those lines to <code>mouseClicked()</code> , but this time I get a new screen each time that a new polygon is created without the "old" polygons . </p>
  
  <p>Just a new polygon ... without the old ones </p>
  
  <p><img src="https://i.stack.imgur.com/KqFug.gif" alt="enter image description here"></p>