wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java Angle Corrects Itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Angle Corrects Itself" Watch "Java Angle Corrects Itself" New topic
Author

Java Angle Corrects Itself

Christopher McKay
Ranch Hand

Joined: Oct 19, 2013
Posts: 47

I currently have a program where the is a JLabel on a JFrame. The JLabel is invisible unless you hold down the mouse. Aswell as becoming visible when the mouse is clicked, it points and moves towards the mouse. At the moment, this means that if you hold the mouse down and mouse your mouse, the JLabel will move with it. I want to have it find the last angle on the click of the mouse and continue to move in the same direction, ignoring any mouse movements. I have tried this and the JLabel flies off in a random direction every time.So I went back to having it go towards the angle (not the last angle) and it turns out that the JLabel will move backwards (in relative to the mouse) and will quickly correct itself, kind of like a "U" turn. I have tried writing similar code in *cough* scratch *cough* and it seems to work fine and the bug doesn't occur, but for some reason java is different, and if I want to find the last angle, it makes it a lot harder. Could anyone help?

I am calculating the angle by doing this:
And then moving by doing this:

If you need anymore code samples I will post them as a reply because they might not be relevant at this point.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 420
    
    5
hi Christopher,

first of all, I'm not sure why you need an angle. Your deltaX and deltaY should be all that you need.
(and I also do not understand why you convert radians to degrees, and use that in the cos and sin part.
cos and sin expect an angle in radians)

Moving the label in an initial fixed direction can be achieved when you only
determine a deltaX and deltaY in your mouseListener routine, when you get a mouse clicked event.
Keep moving the label by using the delta's, in steps of 5 pixels.

But I may have misunderstood your question, though.

Greetings,
Piet
Christopher McKay
Ranch Hand

Joined: Oct 19, 2013
Posts: 47

Piet Souris wrote:hi Christopher,

first of all, I'm not sure why you need an angle. Your deltaX and deltaY should be all that you need.
(and I also do not understand why you convert radians to degrees, and use that in the cos and sin part.
cos and sin expect an angle in radians)

Moving the label in an initial fixed direction can be achieved when you only
determine a deltaX and deltaY in your mouseListener routine, when you get a mouse clicked event.
Keep moving the label by using the delta's, in steps of 5 pixels.

But I may have misunderstood your question, though.

Greetings,
Piet

So like this:
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 420
    
    5
Yes, something like this.

However, there is the question of how fast you want to move your label towards where the mouse click
took place.

I assume that deltaX = Mx - Lx, where Mx is the x-coordinate of the mouse click and Lx the
x-coordinate of your label (and likewise for deltaY).

In your opening post you had:

meaning that your speed was always 5 units long.

If you want to maintain that speed, you should determine a factor F
such that the vector (F * deltaX, F * deltaY) has a length of 5.
From the code you showed I gather you know how to do this.

Greetz,
Piet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Angle Corrects Itself
 
Similar Threads
Have a chess board, need help assigning values so GUI can update
JMenu text color
angle of mouse
Find the last character in a srting.
How do I rotate an image towards a position (x,y)?