• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Angle Corrects Itself

 
Ranch Hand
Posts: 50
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic