1

I need to rotate the same node in a single HBox to 45, 90, 135 degrees respectively.

I have the following so far but it just returns the complete HBox rotated at 135 degrees.

public HBox getBox() throws Exception {
        HBox hb = new HBox();
        hb.getChildren().clear();

        // Add nodes to hbox
        for (int i = 1; i <= 3; i++) {
            hb.setRotate(45 * i);
            hb.getChildren().add(new ImageView(new Image(new FileInputStream("myFile.png"))));
        }
        return hb;
    }
Reporter
  • 3,897
  • 5
  • 33
  • 47
  • 1
    What would happen if you create within the for loop a new instance of HBox? – Reporter May 28 '19 at 08:21
  • 1
    You're calling `hb.rotate(...)` so the `HBox` is the one being rotated. You should, if I understand your requirements correctly, be applying the rotation to the `ImageView` created in the loop. Also, don't forget to close that `FileInputStream`. – Slaw May 28 '19 at 10:33
  • Thanks, this put me on the right track – Ricky Sinclair May 28 '19 at 10:35
  • 5
    It's unnecessary to load the same image into memory multiple times. `Image` instances can be shared between `ImageView`s and should be to reduce the memory footprint of the app as well as the performance. Furthermore clearing the child list just after creating a `HBox` has no effect, since a `HBox` is empty unless you add children via the varargs constructor... – fabian May 28 '19 at 10:36

0 Answers0