title: How to draw a line from a jpanel to another jpanel
body: <p>I have two JPanels and i want to click on the first panel then on the second and a straight line to be drawn between the two panels. Also the line must persist between the two panels after it is placed. Can someone give me a tip where to start from ? The two panels are placed on a third panel they are the blue rectangles on the image below. Thank you in advance.
  <a href="https://i.stack.imgur.com/SvggL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SvggL.png" alt="enter image description here"></a>
  // Parent JPanels</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.util.ArrayList;
  
  
              class WorkflowPanel extends JPanel {
                  private volatile int screenX = 0;
                  private volatile int screenY = 0;
                  private static final int RADIUS = 35;
                  private int radius = RADIUS;
                  private ArrayList&lt;ModelView&gt; relationship;
                  WorkflowPanel() {
                      relationship = new ArrayList&lt;&gt;();
                      relationship.add(new ModelView());
                      relationship.add(new ModelView());
                      add(relationship.get(0));
                      add(relationship.get(1));
                      setLayout(null);
                      setVisible(true);
                  }
  
                  @Override
                  protected void paintChildren(Graphics g) {
                          for (int i = 0; i &lt; relationship.size(); i += 2) {
                          ModelView one = relationship.get(i);
                          ModelView two = relationship.get(i + 1);
                          Point p1 = new Point(one.getLocation().x + one.getWidth() / 2, one.getLocation().y + one.getHeight() / 2);
                          Point p2 = new Point(two.getLocation().x + two.getWidth() / 2, two.getLocation().y + two.getHeight() / 2);
                          g.drawLine(p1.x, p1.y, p2.x, p2.y);
                          this.repaint();
                      }
                      super.paintChildren(g);
                  }
  
  
                  public ArrayList&lt;ModelView&gt; getRelationship() {
                      return relationship;
                  }
              }
  
      // Draggable Jpanels
  
      import javax.swing.*;
      import javax.swing.border.*;
      class ModelView extends JPanel {
              private volatile int screenX = 0;
              private volatile int screenY = 0;
              private volatile int myX = 0;
              private volatile int myY = 0;
              private JLabel modelLbl;
  
  
              public ModelView() {
                  setBorder(new LineBorder(new Color(128, 149, 255), 2));
                  setBackground(Color.WHITE);
                  setBounds(0, 0, 150, 75);
                  setOpaque(false);
  
  
                  GridBagConstraints gbc = new GridBagConstraints();
                  gbc.anchor = GridBagConstraints.FIRST_LINE_START;
                  gbc.fill = GridBagConstraints.HORIZONTAL;
  
                  modelLbl = getLabel("DecisionTree");
  
  
  
                  addMouseMotionListener(new MouseMotionListener() {
                      @Override
                      public void mouseDragged(MouseEvent e) {
  
                      }
  
                      @Override
                      public void mouseMoved(MouseEvent e) {
  
                      }
                  });
  
                  addMouseListener(new MouseListener() {
  
                      @Override
                      public void mouseClicked(MouseEvent e) { }
  
                      @Override
                      public void mousePressed(MouseEvent e) {
                          screenX = e.getXOnScreen();
                          screenY = e.getYOnScreen();
  
                          myX = getX();
                          myY = getY();
                      }
  
                      @Override
                      public void mouseReleased(MouseEvent e) {
  
                      }
  
                      @Override
                      public void mouseEntered(MouseEvent e) { }
  
                      @Override
                      public void mouseExited(MouseEvent e) { }
  
                  });
                  addMouseMotionListener(new MouseMotionListener() {
  
                      @Override
                      public void mouseDragged(MouseEvent e) {
                          int deltaX = e.getXOnScreen() - screenX;
                          int deltaY = e.getYOnScreen() - screenY;
  
                          setLocation(myX + deltaX, myY + deltaY);
                          e.getComponent().repaint();   
                          // Repaint the parent component
                          workflowPanel.repaint();
                      }
  
  
  
                      @Override
                      public void mouseMoved(MouseEvent e) { }
  
                  });
              }
  
              private JLabel getLabel(String labelText) {
  
                  JLabel label = new JLabel(labelText);
                  label.setFont(new Font("Verdana", Font.PLAIN, 10));
                  return label;
              }
  
              public JPanel getPanel() {
                  return this;
              }
          }
  </code></pre>