title: Java 6: how to drag/move a line?
body: <p>I have this piece of code, which is supposed to move some pixels on release of mousebutton:</p>
  
  <pre><code>if (selected != -1) {
      Point to = e.getPoint();
      int dx = start.x - to.x;
      int dy = start.y - to.y;
      for (Point p: store.get(selected)) {
          int px = (int) p.getX();
          int py = (int) p.getY();
          p.move(px - dx, py - dy);
      }
  
      validate();
  </code></pre>
  
  <p>Use of debugger shows that Point p's indeed get a new value, but visuals don't get updated. Please help me out. </p>
  
  <p>Below is the whole code of my program.  </p>
  
  <pre><code>public class Pisi extends JFrame implements MouseMotionListener, MouseListener {
  ArrayList&lt;ArrayList&lt;Point&gt;&gt; store = new ArrayList&lt;ArrayList&lt;Point&gt;&gt;();
  ArrayList&lt;Point&gt; pts = new ArrayList&lt;Point&gt;();
  Point start;
  static int xsize = 450;
  static int ysize = 300;
  int listNumber = 0;
  int selected = -1;
  
  
  
  public static void main(String[] args) {
      Pisi d = new Pisi();
      d.setSize(xsize, ysize);
      d.setLocationRelativeTo(null);
      d.addMouseMotionListener(d);
      d.addMouseListener(d);
      d.setResizable(false);
      d.setVisible(true);
  }
  
  @Override
  public void update(Graphics g) {
      paint(g);
  }
  @Override
  public void paint(Graphics g) {
      Point last = null;
  
      for (Point p : pts) {
  
          if (last == null) {
              last = p;
              continue;
          }
          g.drawLine(last.x, last.y, p.x, p.y);
          last = p;
      }
  }
  
  @Override
  public void mouseDragged(MouseEvent e) {
  
      if (e.getButton() == MouseEvent.BUTTON1) {
          pts.add(e.getPoint());
          repaint();
      }
  
  }
  
  @Override
  public void mouseMoved(MouseEvent e) {
      //To change body of implemented methods use File | Settings | File Templates.
  }
  
  @Override
  public void mouseClicked(MouseEvent e) {
      //To change body of implemented methods use File | Settings | File Templates.
  }
  
  @Override
  public void mousePressed(MouseEvent e) {
      Point point = e.getPoint();
      start = null;
      selected = -1;
      for (ArrayList&lt;Point&gt; points: store) {
          for (Point p : points) {
              double dist = point.distanceSq(p);
              if (dist &lt; 10) {
                  selected = store.indexOf(points);
              }
          }
      }
      if (selected != -1) {
          start = e.getPoint();
      }
      System.out.println(selected);
  }
  
  
  @Override
  public void mouseReleased(MouseEvent e) {
      if (selected != -1) {
          Point to = e.getPoint();
          int dx = start.x - to.x;
          int dy = start.y - to.y;
          for (Point p: store.get(selected)) {
              int px = (int) p.getX();
              int py = (int) p.getY();
              p.move(px - dx, py - dy);
          }
  
          validate();
      } else if (e.getButton() == MouseEvent.BUTTON1 &amp;&amp; pts.size() != 0) {
          store.add(new ArrayList&lt;Point&gt;(listNumber));
          for (int i = 0; i &lt; pts.size(); i++) {
              store.get(listNumber).add(pts.get(i));
          }
          listNumber++;
      }
      pts.clear();
  
  }
  
  @Override
  public void mouseEntered(MouseEvent e) {
      //To change body of implemented methods use File | Settings | File Templates.
  }
  
  @Override
  public void mouseExited(MouseEvent mouseEvent) {
      //To change body of implemented methods use File | Settings | File Templates.
  }
  }
  </code></pre>