Before I say anything else, I do realize that there are numerous threads on this topic, but I haven't been able to fix my problem.

Okay. Well, here's my problem. I'm writing a map editor for the game that I'm working on, and the map is oftentimes much larger than the view area. (I'm working on a zoom feature, but that's a different problem...) I have a diamond shaped vector graphic that I'm drawing, and I need it to be moveable. I can get it to move, but it doesn't move relative to the mouse movement, it only moves the origin of the painting to the mouse location, then moves in the same direction as the mouse, but in increasing large increments...

If anyone knows of a solution for this problem, please let me know, Thank you!

Here's the code I have:

// This is located in the Map class that controls drawing and such
    public java.awt.Point getDrawLocation(){
        return new java.awt.Point(startX, startY);
    }

    Point originalLoc;
    public void setOrigLoc(Point p){
        originalLoc = p;
    }

    public void setDrawLocation(java.awt.Point current){
        startX = (int)current.getX();
        startY = (int)current.getY();
        startX = (int)(current.getX()+(startX - originalLoc.getX()));
        startY = (int)(current.getY()+(startY - originalLoc.getY()));
//        startX = (int)(originalLoc.getX()+(current.getX()-originalLoc.getX()));
//        startY = (int)(originalLoc.getY()+(current.getY()-originalLoc.getY()));
    }

// This is in the EditableMap class which extends Map (It should
// probably be in the Map class though.......)

    Point mouseStart, mouseEnd;

    public void mousePressed(MouseEvent e){
        mouseStart = e.getPoint();
        this.setOrigLoc(mouseStart);
        dragging = true;
    }
    public void mouseReleased(MouseEvent e){
        mouseEnd = e.getPoint();
        dragging = false;

        int a = Math.abs((int)(mouseEnd.getX()-mouseStart.getX()));
        int b = Math.abs((int)(mouseEnd.getY()-mouseStart.getY()));

        if(Math.sqrt(a*a+b*b)>3){
//            this.setDrawLocation(mouseEnd);
        }else{
            this.findAndSelectTile(e.getX(), e.getY());
        }
        e.getComponent().repaint();
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}

    public void mouseDragged(MouseEvent e){
        Point current = e.getPoint();
        this.setDrawLocation(current);
        e.getComponent().repaint();
    }

    public void mouseMoved(MouseEvent e){}

Before I say anything else, I do realize that there are numerous threads on this topic, but I haven't been able to fix my problem.

Okay. Well, here's my problem. I'm writing a map editor for the game that I'm working on, and the map is oftentimes much larger than the view area. (I'm working on a zoom feature, but that's a different problem...) I have a diamond shaped vector graphic that I'm drawing, and I need it to be moveable. I can get it to move, but it doesn't move relative to the mouse movement, it only moves the origin of the painting to the mouse location, then moves in the same direction as the mouse, but in increasing large increments...

If anyone knows of a solution for this problem, please let me know, Thank you!

Here's the code I have:

// This is located in the Map class that controls drawing and such
    public java.awt.Point getDrawLocation(){
        return new java.awt.Point(startX, startY);
    }

    Point originalLoc;
    public void setOrigLoc(Point p){
        originalLoc = p;
    }

    public void setDrawLocation(java.awt.Point current){
        startX = (int)current.getX();
        startY = (int)current.getY();
        startX = (int)(current.getX()+(startX - originalLoc.getX()));
        startY = (int)(current.getY()+(startY - originalLoc.getY()));
//        startX = (int)(originalLoc.getX()+(current.getX()-originalLoc.getX()));
//        startY = (int)(originalLoc.getY()+(current.getY()-originalLoc.getY()));
    }

// This is in the EditableMap class which extends Map (It should
// probably be in the Map class though.......)

    Point mouseStart, mouseEnd;

    public void mousePressed(MouseEvent e){
        mouseStart = e.getPoint();
        this.setOrigLoc(mouseStart);
        dragging = true;
    }
    public void mouseReleased(MouseEvent e){
        mouseEnd = e.getPoint();
        dragging = false;

        int a = Math.abs((int)(mouseEnd.getX()-mouseStart.getX()));
        int b = Math.abs((int)(mouseEnd.getY()-mouseStart.getY()));

        if(Math.sqrt(a*a+b*b)>3){
//            this.setDrawLocation(mouseEnd);
        }else{
            this.findAndSelectTile(e.getX(), e.getY());
        }
        e.getComponent().repaint();
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}

    public void mouseDragged(MouseEvent e){
        Point current = e.getPoint();
        this.setDrawLocation(current);
        e.getComponent().repaint();
    }

    public void mouseMoved(MouseEvent e){}

We can't run it since there's a lot more code that isn't posted, but at the very least, I'd say you have a problem here:

public void setDrawLocation(java.awt.Point current){
        startX = (int)current.getX();
        startY = (int)current.getY();
        startX = (int)(current.getX()+(startX - originalLoc.getX()));
        startY = (int)(current.getY()+(startY - originalLoc.getY()));
//        startX = (int)(originalLoc.getX()+(current.getX()-originalLoc.getX()));
//        startY = (int)(originalLoc.getY()+(current.getY()-originalLoc.getY()));
    }

which turns into this:

public void setDrawLocation(java.awt.Point current){
        startX = (int)(2 * current.getX() - originalLoc.getX());
        startY = (int)(2 * current.getY() - originalLoc.getY());
    }

Why are you multiplying current.getX () and current.getY () by 2?

I'm sorry, I should have clarified. That is the method that I thought was the cause of my problems. I just posted the others in case they were relevant. Also, looking over it again, i realize that there are few to no comments, and no variable description. Sorry >.<

Anyways. I'm not quite sure why I'm multiplying those by 2. It seemed like the logic thing to do when I was thinking the algorithm through.... Well, I took out the x2 part, and it works better, but startX and startY are snapping to 0 and 0 because of the setDrawLocation method.... any ideas for a fix? I know why it's doing that, because current and originalLoc are the same values....

I'm sorry, I should have clarified. That is the method that I thought was the cause of my problems. I just posted the others in case they were relevant. Also, looking over it again, i realize that there are few to no comments, and no variable description. Sorry >.<

Anyways. I'm not quite sure why I'm multiplying those by 2. It seemed like the logic thing to do when I was thinking the algorithm through.... Well, I took out the x2 part, and it works better, but startX and startY are snapping to 0 and 0 because of the setDrawLocation method.... any ideas for a fix? I know why it's doing that, because current and originalLoc are the same values....

If you have something like a square and you draw it based on the upper-left corner, you could have a jerk on the very first mousePressed or mouseDragged if the person doesn't click on the upper-left corner and the mouse click is ASSIGNED to be the upper left corner. Take a square with current upper left corner (5, 5) and side length 20. So the square goes from (5, 5) to (25, 25). Say you have a variable called upperLeftX and upperLeftY and they are assigned to be (5, 5) before the mouse press and your paint is this:

drawRect (upperLeftX, upperLeftY, 20, 20);

The person clicks the center of the square (15, 15).What you should NOT do in mousePressed is this:

upperLeftX = e.getX ();
upperLeftY = e.getY ();
repaint();

There's the initial jerk of (10, 10) to the right and down. You need to calculate the offset in mousePressed and offset needs to be a non-local variable because you'll need it in mouseDragged too.

In mousePressed:

offsetX = e.getX () - upperLeftX;
offsetY = e.getY() - upperLeftY;

Then in mouseDragged:

upperLeftX = e.getX () - offsetX;
upperLeftY =e.getY () - offsetY;
repaint ();

You'll need to change it to your needs, but I think that's the general idea. Variable names like "location" can be a bit vague, because a shape isn't located at a single point, though it can be defined as "relative to" a single point. But don't assume that the user is clicking at the exact spot within the shape that you have defined the shape around. You need an offset to fix that.

SUCCESS! Thanks a lot, I got it working, that has been annoying me for weeks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.