title: Isometric Game Mouse logic and movement around an environment
body: <p>I have am making an Isometric game environment and I wish to navigate around the environment using a hold Right click and move the mouse to look around the map.</p>
  
  <p>The movement works fine the first time but when i try it the second time it resets itself to the original location before moving again. I know this is to do with mouse position realtive to the background that is moving but ive tried a number of solutions and still the logic eludes me, is there anyone that can help me figuire out what im doing wrong, thanks in advance.</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.EventQueue;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseMotionListener;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.SwingUtilities;
  import javax.swing.Timer;
  
  public class CameraMovementTest extends JPanel{
  
  private Timer timer;
  private int DELAY = 10;
  private  CustomMouseListener mouseListener;
  private int positionX = 0, positionY = 0;
  
  public CameraMovementTest() {
  
      mouseListener  = new CustomMouseListener();
      this.addMouseListener(mouseListener);
      this.addMouseMotionListener(mouseListener);
      this.setSize(500,500);
      this.setVisible(true);
  
      //Swing Timer
      timer = new Timer(DELAY, new ActionListener(){
          @Override
          public void actionPerformed(ActionEvent arg0) {
              update();
              repaint();
              validate(); 
          }
      }); 
      timer.start();
  }
  
  private void update() {
      if(mouseListener!=null){
          positionX = mouseListener.getX();
          positionY = mouseListener.getY();
      }
  }
  
  @Override
  public void paint(Graphics g) {
      super.paint(g);
      g.fillRect( positionX,  positionY, 300,300);
  }
  
  public class CustomMouseListener implements MouseListener, MouseMotionListener {
      private int positionX = 0, positionY = 0;
      private int mouseClickX, mouseClickY;
  
      @Override
      public void mousePressed(MouseEvent evt) {
  
          if(SwingUtilities.isRightMouseButton(evt)) {
  
              mouseClickX = evt.getX();
              mouseClickY = evt.getY();
          }   
      }
      @Override
      public void mouseDragged(MouseEvent evt) {
          if(SwingUtilities.isRightMouseButton(evt)) {
              positionX = mouseClickX - evt.getX();
              positionY = mouseClickY - evt.getY();
          }
      }
      @Override
      public void mouseMoved(MouseEvent arg0) {}
      @Override
      public void mouseClicked(MouseEvent arg0) { }
      @Override
      public void mouseEntered(MouseEvent arg0) { }
      @Override
      public void mouseExited(MouseEvent arg0) {}
  
      @Override
      public void mouseReleased(MouseEvent arg0) {}
      public int getX(){
          return positionX;
      }
      public int getY(){
          return positionY;
      }
  }
  
  
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
              JFrame f = new JFrame();
              f.setVisible(true);
              f.setSize(new Dimension(500,500));
              f.setContentPane(new CameraMovementTest());
          }
      });
  }
  </code></pre>
  
  <p>}</p>