Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MouseDrag, x, y coordinates prob.

Status
Not open for further replies.

matthewking

Programmer
Jul 15, 2002
75
ES
Hi,

I have a java applet which allows you to drag n drop shapes.

at the moment the drag code is as follows:

public boolean mouseDrag(Event evt, int x, int y)
{
if (selectedObject != null)
{
selectedObject.x = x;
selectedObject.y = y;
}

repaint();
return true;
}

This isnt exactly what im wanting because if you click on the center of the shape, when you begin to drag, the top left corner is moved to the mouse coordinates.

So to fix this I wanted to calculate the offset, so the mouse stays in the position I clicked while I drag. I've tried a few calculations and I cant seem to get it right.

Any help would be much appreciated.

Matt.
 
I had done, and calculated by finding the offset from the object.x and the mouse.x, but the problem was that I was calculating the offset in the drag event, rather then the click event, so obviously it didnt move.

Nailed it now though.
 

There is so posted code about how to get the coordinates, look for the drag and drop operation with the images. Also, I think you dont really need that boolean return type, you have no (shown) code to process a false statement, therefore I believe the boolean is pointless and you can just put a void return type.

Thats my two cents.
 
you need to calculate the correct offset in mousePress. you should then be able to change your mouseDrag code to something like:

if (selectedObject != null)
{
selectedObject.x = x - offset.x;
selectedObject.y = y - offset.y;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top