B4J Question How to move a rotated node with mouse?

xulihang

Active Member
Licensed User
Longtime User
Solution
Modified a bit to use the view's left and top instead of setTranslateX.

B4X:
Private Sub Pane1_MouseDragged (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    Dim n As Node = Sender
    Dim offsetX, offsetY As Double
    offsetX = event.RunMethod("getSceneX",Null) - pressedX
    offsetY =  event.RunMethod("getSceneY",Null)- pressedY
    n.Left = n.Left + offsetX
    n.Top = n.Top + offsetY
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub

Private Sub Pane1_MousePressed (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub

xulihang

Active Member
Licensed User
Longtime User
Here is a test project.
 

Attachments

  • RotationTest.zip
    2.5 KB · Views: 179
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I did try to move a rotated node in a pane. This works. But I prefer to move it directly.
 
Last edited:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Found this post: https://stackoverflow.com/questions/27080039/proper-way-to-move-a-javafx8-node-around

I modified my code, which works better. The node will not fly away. But still not perfect, I need to drag the center point so that the node will not shift.

B4X:
Private Sub Pane1_MousePressed (EventData As MouseEvent)
    Dim jo As JavaObject = Sender
    pressedX = jo.RunMethod("getLayoutX",Null) + EventData.X
    pressedY = jo.RunMethod("getLayoutY",Null) + EventData.Y
End Sub

Private Sub Pane1_MouseDragged (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    Dim jo As JavaObject = Sender
    
    Dim offsetX, offsetY As Double
    offsetX = event.RunMethod("getSceneX",Null) - pressedX
    offsetY =  event.RunMethod("getSceneY",Null)- pressedY
    jo.RunMethod("setTranslateX",Array(offsetX))
    jo.RunMethod("setTranslateY",Array(offsetY))
End Sub
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Here is the version that works perfectly (based on this: https://stackoverflow.com/questions/33043173/javafx-drag-rotated-node)

B4X:
Private Sub Pane1_MouseDragged (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    Dim jo As JavaObject = Sender
    
    Dim offsetX, offsetY As Double
    offsetX = event.RunMethod("getSceneX",Null) - pressedX
    offsetY =  event.RunMethod("getSceneY",Null)- pressedY
    jo.RunMethod("setTranslateX",Array(jo.RunMethod("getTranslateX",Null)+offsetX))
    jo.RunMethod("setTranslateY",Array(jo.RunMethod("getTranslateY",Null)+offsetY))
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub

Private Sub Pane1_MousePressed (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Modified a bit to use the view's left and top instead of setTranslateX.

B4X:
Private Sub Pane1_MouseDragged (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    Dim n As Node = Sender
    Dim offsetX, offsetY As Double
    offsetX = event.RunMethod("getSceneX",Null) - pressedX
    offsetY =  event.RunMethod("getSceneY",Null)- pressedY
    n.Left = n.Left + offsetX
    n.Top = n.Top + offsetY
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub

Private Sub Pane1_MousePressed (EventData As MouseEvent)
    Dim event As JavaObject = EventData
    pressedX = event.RunMethod("getSceneX",Null)
    pressedY = event.RunMethod("getSceneY",Null)
End Sub
 
Upvote 0
Solution
Top