Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaFX, rotation

 
Tom Gibbins
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. In the code below, i have two lambda expressions for two different buttons in two different panes. The first expression, btFirkant.setOnAction, creates a square at a random place in the pane with a fixed size. The second expression is supposed to access this rectangle, and rotate it 45 degrees. That does not work with the code below. I'm not sure how to access the rectangle (or any other nodes for that matter) created in one pane and manipulate them in another. Any idea how to do this? I can of course paste more code if necessary.



Thanks!
 
Campbell Ritchie
Sheriff
Posts: 51438
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not look as though it will compile; you are applying the (Rectangle) cast to the rectangleMidPane reference. That is hardly going to be a Rectangle.
It would be possible to go through all the children, but I am not sure which getChildren method you are using. This version seems to return an ObservableList<Node> which extends List<E>, so it would be possible to iterate that List and see which of your Nodes is a Rectangle, but that hardly looks like object‑oriented programming to me. I think you are going to have to keep some sort of reference to that Rectangle somewhere so you can apply the rotation to it.

I haven't tried rotation in JavaFX but I have in Swing, so I know there may be two problems:-
  • 1: The centre of rotation might not be anywhere near your rectangle's location, so it may swing round the corner of your display. You may need to move the Graphics object's centre before rotating anything.
  • 2: You may find it difficult to undo the transform, so take a copy of the Graphics object before the rotation. That way you have an un‑rotated Graphics object to use for everything else. The shear transform causes more problems than rotation.
  • I know those problems occur in Swing, but I don't know whether they occur in JavaFX, too.

    Despite what it says in the books, don't do arithmetic with Math#random. Read this old thread which looks at the different techniques and also looks at some pitfalls.

    I am moving this discussion to the JavaFX forum.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic