title: Draw, Move shapes In java Swing
body: <p>I am doing a project regarding the movement of shapes drawn in a <code>JPanel</code> but if i implement a <code>mouselistener</code> to my class how can i do if i have multiple shapes drawn? In my example i have drawn two rectangles.
  This is my code:</p>
  <pre><code>import javax.swing.BoxLayout;
  import javax.swing.JFrame;
  import static javax.swing.JFrame.EXIT_ON_CLOSE;
  import javax.swing.JPanel;
  
  public class NewClass1 extends JFrame{
          JPanel panel2;
          
      public NewClass1(){
          JPanel container = new JPanel();
          container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
           panel2 = new jPanel2();
  
          container.add(panel2);
          
          add(container);
          setSize(300, 300);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setVisible(true);
      }
  
      public static void main(String[] args) {
          new NewClass1();
      }
      
      class jPanel2 extends JPanel {
  
      @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          g.drawRect(0, 0, 20, 20);
          g.drawRect(21, 21, 20, 20);
      }}
  }
  </code></pre>