Login  Register

Placing a NewtCanvasJFX within other JavaFX layout elements

Posted by leeca on Feb 26, 2024; 10:10pm
URL: https://forum.jogamp.org/Placing-a-NewtCanvasJFX-within-other-JavaFX-layout-elements-tp4043329.html

The NewtCanvasJFX canvas all seem to want to render from the origin the underlying window region.  It does not seem to respect placements with a y-offset.  (I suspect x-offsets are also a problem, but that does not affect my use case.)

When a NewtCanvasJFX is placed as the only child of a Group, the OGL render appears correctly in the screen space.

If the NewtCanvasJFX is a second or third child of a VBOX, the canvas appears to ignore the offset.  It renders from the origin (0, 0) of the underlying region (e.g. Stage).   This over-writes the top elements in the VBox, and leaves a large gap between the bottom of the canvas and bottom element of the VBox.

The code to implement this is based on JavaFx et JOGL (https://gouessej.wordpress.com/2020/04/05/javafx-et-jogl-fonctionnent-ensemble-javafx-and-jogl-work-together/).  This demo refactors that example for Gradle and Java 17 modules in git repository (https://github.com/leeca/JavaFx_JOGL).  Branch [vbox-canvas] provides the variations for the demo code.

The code in block 1 sets up a simple VBox with three components in a vertical stack.  Running this example produces the output in image 1.

<code>
  @Override
  public void start(Stage stage) {
    Platform.setImplicitExit(true);
    final Group group = new Group();
    Scene scene = new Scene(group, 800, 600);
    stage.setScene(scene);
    stage.show();

    jogl = new JoglModule();
    group.getChildren().add(asVBox());
    jogl.demoDisplay();
    jogl.start();
  }

  private Node asVBox() {
    VBox result = new VBox();
    result.getChildren().add(new Label("Top"));
    result.getChildren().add(new Label("Middle"));
    Canvas canvas = jogl.prepareCanvas();
    // result.getChildren().add(canvas);
    result.getChildren().add(new Label("Bottom"));
    return result;
  }
</code>

VBox with 3 Labels


If we uncomment the addition of the canvas in asVbox(),  the rendering is completely different.  The Top and Middle elements of the VBox are being overwritten with the JOGL output, and the gap between the canvas frame and the Bottom element is larger than expected.  That gap is comparable to the ignored offset from the Top and Middle elements.

VBox with NewtCanvasJFX

I suspect the NEWT window is ignoring the offsets, and places itself at the origin of the “primary” window’s client space.  For this example, that happens to be a Scene.

In another use case, the NEWT window is placed in a Tabbed client area.  But other layout placement (e.g. BorderPane) within the tabbed window’s client area is ignored.  Even directly setting the layout Y in a custom wrapper Pane fails to modify the placement of the JOGL canvas.  But those are more complex examples.

Is there a kind of JOGL container that has hard-offsets that make a better wrapper for the NewtCanvasJFX?