title: Components disappear after resizing JPanel
body: <p>I am trying to create JPanel with draggable crosses which appear after mouse clicking. Everything works fine but when I resize the JPanel the crosses disappear. I tried to override the paintComponent method in my JPanel but then all crosses are at coordinates (0,0). How can I fix it? </p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseMotionAdapter;
  import java.util.ArrayList;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class CrossPanel extends JPanel implements MouseListener {
  
  private int orderOfCross = 0;
  private ArrayList&lt;Cross&gt; crosses;
  private int defaultSizeOfCrosses = 10;
  
  CrossPanel() {
      setOpaque(false);
      addMouseListener(this);
      crosses = new ArrayList&lt;Cross&gt;();
  }
  
  @Override
  public void mouseClicked(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      Cross cross = new Cross(orderOfCross++, defaultSizeOfCrosses);
      crosses.add(cross);
      cross.setLocation(x - defaultSizeOfCrosses, y - defaultSizeOfCrosses);
      add(cross);
      repaint();
  }
  
  @Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      //        for (int i = 0; i &lt; crosses.size(); i++) {
      //            crosses.get(i).paint(g);
      //        }
  }
  
  @Override
  public void mousePressed(MouseEvent e) {}
  
  @Override
  public void mouseReleased(MouseEvent e) {}
  
  @Override
  public void mouseEntered(MouseEvent e) {}
  
  @Override
  public void mouseExited(MouseEvent e) {}
  
  public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      CrossPanel crossPane = new CrossPanel();
      f.getContentPane().add(crossPane);
      f.setSize(600, 500);
      f.setLocation(200, 200);
      f.setVisible(true);
  }
  }
  
  class Cross extends JComponent {
  
  private int order;
  protected Cursor draggingCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
  private volatile int draggedAtX, draggedAtY;
  int size;
  
  public Cross(int order, int size) {
      this.order = order;
      this.size = size;
      this.setBounds(0, 0, 4 * size, 3 * size + 10);
      addDragListeners();
      setCursor(draggingCursor);
  }
  
  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.red);
      g2.setStroke(new BasicStroke(3));
      g2.drawLine(0, size, size + size, size);
      g2.drawLine(size, 0, size, size + size);
      Font f = new Font("Monospaced", Font.BOLD, size + 10);
      g2.setFont(f);
      g2.drawString(String.valueOf(order), size - size / 2, 3 * size + 10);
  }
  
  private void addDragListeners() {
      addMouseListener(new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent e) {
              draggedAtX = e.getX();
              draggedAtY = e.getY();
          }
      });
    addMouseMotionListener(new MouseMotionAdapter() {
          @Override
          public void mouseDragged(MouseEvent e) {
              Point newLocation = new Point(e.getX() - draggedAtX + getLocation().x,  e.getY() - draggedAtY + getLocation().y);
              setLocation(newLocation);
          }
      });
  }
  }
  </code></pre>