Advertisement
matyklug

Untitled

Jul 11th, 2019
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Label;
  5. import javafx.scene.layout.StackPane;
  6. import javafx.scene.paint.Color;
  7. import javafx.scene.shape.Rectangle;
  8. import javafx.stage.Stage;
  9.  
  10.  
  11. public class NodeDemo extends Application {
  12.  
  13.     private double sceneWidth = 1024;
  14.     private double sceneHeight = 768;
  15.  
  16.     private int n = 10;
  17.     private int m = 10;
  18.  
  19.     double gridWidth = sceneWidth / n;
  20.     double gridHeight = sceneHeight / m;
  21.  
  22.     MyNode[][] playfield = new MyNode[n][m];
  23.  
  24.     @Override
  25.     public void start(Stage primaryStage) {
  26.  
  27.  
  28.         Group root = new Group();
  29.  
  30.         // initialize playfield
  31.         for( int i=0; i < n; i++) {
  32.             for( int j=0; j < m; j++) {
  33.  
  34.                 // create node
  35.                 MyNode node = new MyNode( "Item " + i + "/" + j, i * gridWidth, j * gridHeight, gridWidth, gridHeight);
  36.  
  37.                 // add node to group
  38.                 root.getChildren().add( node);
  39.  
  40.                 // add to playfield for further reference using an array
  41.                 playfield[i][j] = node;
  42.  
  43.             }
  44.         }
  45.  
  46.  
  47.         Scene scene = new Scene( root, sceneWidth, sceneHeight);
  48.  
  49.         primaryStage.setScene( scene);
  50.         primaryStage.show();
  51.  
  52.     }
  53.  
  54.     public static void main(String[] args) {
  55.         launch(args);
  56.     }
  57.  
  58.     public static class MyNode extends StackPane {
  59.  
  60.         public MyNode( String name, double x, double y, double width, double height) {
  61.  
  62.             // create rectangle
  63.             Rectangle rectangle = new Rectangle( width, height);
  64.             rectangle.setStroke(Color.BLACK);
  65.             rectangle.setFill(Color.LIGHTBLUE);
  66.  
  67.             // create label
  68.             Label label = new Label( name);
  69.  
  70.             // set position
  71.             setTranslateX( x);
  72.             setTranslateY( y);
  73.  
  74.             getChildren().addAll( rectangle, label);
  75.  
  76.         }
  77.  
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement