1

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.

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.

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());
        }
    });
}

}

Random_Guy_a
  • 107
  • 1
  • 2
  • 11
  • Would this question be one of those that would be better asked on GameDev.SE? – Arc676 Oct 25 '15 at 13:43
  • @Arc676: no, I think that this site is an appropriate site for this question, but I fear that in order to answer it, we're going to need more information and working code, preferably a [mcve]. – Hovercraft Full Of Eels Oct 25 '15 at 13:44
  • There isnt much more code to it really, the background is drawn at 0,0 intially and I add on the movement of the mouse to its x and y when the mouseDragged method is called and continue to repaint. , what code would you like to see? – Random_Guy_a Oct 25 '15 at 13:49
  • Again we'd like to see your [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve) where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. – Hovercraft Full Of Eels Oct 25 '15 at 13:59
  • I have edited the post to include the update and paint methods, I dont think theres anything else relevant to the problem, but if you need to see more code let me know – Random_Guy_a Oct 25 '15 at 14:04
  • If you don't get an answer soon, then please check out the [mcve] link as it will tell you precisely what we need. This **will likely** require significant work on your part since we're not going to want to see the whole program if it is more than a page or two in length, which means that you'll likely have to create a new small demo program, but it is work well worth it as it will allow us to be able to compile, run, test and modify your code, giving us a much greater understanding of your problem. – Hovercraft Full Of Eels Oct 25 '15 at 14:08
  • 1
    For [example](http://stackoverflow.com/a/7203419/230513). – trashgod Oct 25 '15 at 14:09
  • I have added a working example that replicates the problem – Random_Guy_a Oct 25 '15 at 16:29

1 Answers1

2

it resets itself to the original location before moving again.

Did you add any debug code to look at your calculation of the x/y position?

@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)) {
        System.out.println(mouseClickX + " : " + evt.getX());
        positionX = mouseClickX - evt.getX();
        positionY = mouseClickY - evt.getY();
    }
}

You set the mouseClickX/Y to the mouse point, then in the mouseDragged you subtract the mouse point from the value. Which means you basically get 0 (well actually 1, since the mouse moved 1 pixel to generate the drag event).

So in the mousePressed logic you can't just reset the mouseClickX/Y to the current mouse point. You need to track that last mouse point as well:

@Override
public void mousePressed(MouseEvent evt) {

    if(SwingUtilities.isRightMouseButton(evt)) {
        //mouseClickX = evt.getX();
        //mouseClickY = evt.getY();
        mouseClickX = positionX + evt.getX();
        mouseClickY = positionY + evt.getY();
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288