title: How to click and drag something without it deselecting
body: <p>The program is an animation that creates a car and/or truck icon on the screen. The way I have it now it isn't working correctly. Specifically, the program is not clicking and dragging right. If one object is not-selected, once clicked on, it will become bolder to show that it is selected. From there we want to be able to drag it and the program will redraw the image wherever the mouse goes. If the image is un-selected, when I click and drag it, it works fine. The problem I am having is if the image is already selected. If the image is already selected, when I move the mouse over to it and click on it in order to move it to a different position, instead of moving, it is deselected instead so no movement occurs. Here is the code for the mousePressed and mouseDragged events. I think that is where the problem is, but I'm not sure what is causing it.</p>
  
  <pre><code>addMouseListener(new
     MouseAdapter()
     {
        public void mousePressed(MouseEvent event)
        {
           mousePoint = event.getPoint();
           for (SceneShape s : shapes)
           {
              if (s.contains(mousePoint))
                 s.setSelected(!s.isSelected());
           }
           repaint();
        }
     });
  
  addMouseMotionListener(new
     MouseMotionAdapter()
     {
        public void mouseDragged(MouseEvent event)
        {
           Point lastMousePoint = mousePoint;
           mousePoint = event.getPoint();
           for (SceneShape s : shapes)
           {
              if (s.isSelected())
              {
                 double dx
                       = mousePoint.getX() - lastMousePoint.getX();
                 double dy
                       = mousePoint.getY() - lastMousePoint.getY();
                 s.translate((int) dx, (int) dy);
              }
           }
           repaint();
        }
     });
  </code></pre>
  
  <p>Can someone help explain to me what is causing the program to deselect an already selected image when I drag it instead of moving it and how to fix this problem? Thanks.</p>