How to create a StackPane using JavaFX?

JavaFXObject Oriented ProgrammingProgramming

Once you create all the required nodes for your application you can arrange them using a layout. Where a layout is a process of calculating the position of objects in the given space. JavaFX provides various layouts in the javafx.scene.layout package.

Stack Pane

In this layout, the nodes are arranged as a stack from bottom to top (one upon another). You can create a stack pane in your application by instantiating the javafx.scene.layout.StackPane class.

  • You can set the alignment of the nodes in this pane using the setAlignment() method.

  • In the same you can set a margin for a node within the pane, using the setMatgin() method.

To add nodes to this pane you can either pass them as arguments of the constructor or, add them to the observable list of the pane as −

getChildren().addAll();

Example

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Sphere;
public class StackedPaneExample extends Application {
   public void start(Stage stage) {
      //Drawing sphere1
      Sphere sphere1 = new Sphere(80);
      sphere1.setDrawMode(DrawMode.LINE);
      //Drawing sphere2
      Sphere sphere2 = new Sphere();
      //Setting the properties of the Box(cube)
      sphere2.setRadius(120.0);
      //Setting other properties
      sphere2.setCullFace(CullFace.BACK);
      sphere2.setDrawMode(DrawMode.FILL);
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.BROWN);
      sphere2.setMaterial(material);
      //Setting the perspective camera
      PerspectiveCamera cam = new PerspectiveCamera();
      cam.setTranslateX(-50);
      cam.setTranslateY(25);
      cam.setTranslateZ(0);
      //Drawing the shape
      MoveTo moveTo = new MoveTo(208, 71);
      LineTo line1 = new LineTo(421, 161);
      LineTo line2 = new LineTo(226,232);
      LineTo line3 = new LineTo(332,52);
      LineTo line4 = new LineTo(369, 250);
      LineTo line5 = new LineTo(208, 71);
      //Creating a Path
      Path path = new Path(moveTo, line1, line2, line3, line4, line5);
      path.setFill(Color.DARKCYAN);
      path.setStrokeWidth(8.0);
      path.setStroke(Color.DARKSLATEGREY);
      //.setMaterial(material);
      //Creating a stack pane
      StackPane stackPane = new StackPane();
      //Setting the margin for the circle
      stackPane.setMargin(sphere2, new Insets(50, 50, 50, 50));
      //Retrieving the observable list of the Stack Pane
      ObservableList list = stackPane.getChildren();
      //Adding all the nodes to the pane
      list.addAll(sphere2, sphere1, path);
      //Setting the Scene
      Scene scene = new Scene(stackPane, 595, 300);
      scene.setCamera(cam);
      stage.setTitle("Stack Pane");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 19-May-2020 16:47:47
Advertisements