JavaFX Line create grid

Description

JavaFX Line create grid


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {
   @Override/*w  w  w .ja  va2 s  .c  o m*/
   public void start(Stage primaryStage) {
      // Create a Scene and place it in the stage
      Scene scene = new Scene(new MyPane(), 200, 200);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}

class MyPane extends Pane{
   public MyPane() {
      // Create four lines and set their properties
      Line line1 = new Line(0, 200 / 3, 200, 200 / 3);
      line1.startYProperty().bind(heightProperty().divide(3));
      line1.endYProperty().bind(heightProperty().divide(3));
      line1.endXProperty().bind(widthProperty());
      line1.setStroke(Color.BLUE);

      Line line2 = new Line();
      line2.startYProperty().bind(line1.startYProperty().multiply(2));
      line2.endYProperty().bind(line1.endYProperty().multiply(2));
      line2.endXProperty().bind(widthProperty());
      line2.setStroke(Color.BLUE);

      Line line3 = new Line(200 / 3, 0, 200 / 3, 200);
      line3.startXProperty().bind(widthProperty().divide(3));
      line3.endXProperty().bind(widthProperty().divide(3));
      line3.endYProperty().bind(heightProperty());
      line3.setStroke(Color.RED);

      Line line4 = new Line();
      line4.startXProperty().bind(line3.startXProperty().multiply(2));
      line4.endXProperty().bind(line3.endXProperty().multiply(2));
      line4.endYProperty().bind(heightProperty());
      line4.setStroke(Color.RED);

      // Place lines in pane
      getChildren().addAll(line1, line2, line3, line4); 
   }
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

   @Override/*from ww w .ja  v a2s. c  o m*/
   public void start(Stage primaryStage) {

      Pane pane = new Pane();
      primaryStage.setTitle("3x3 Grid");
      primaryStage.setScene(new Scene(pane, 400, 400));

      double divider = 3;
      for (int i = 0; i < 2; i++) {
         Line line = new Line();
         line.setStroke(Color.RED);
         line.startXProperty().bind(pane.widthProperty().divide(divider));
         line.startYProperty().bind(pane.layoutYProperty());
         line.endXProperty().bind(line.startXProperty());
         line.endYProperty().bind(pane.heightProperty());
         pane.getChildren().add(line);
         divider /= 2;
      }
      divider = 3;
      for (int i = 0; i < 2; i++) {
         Line line = new Line();
         line.setStroke(Color.RED);
         line.startXProperty().bind(pane.layoutXProperty());
         line.startYProperty().bind(pane.heightProperty().divide(divider));
         line.endXProperty().bind(pane.widthProperty());
         line.endYProperty().bind(pane.heightProperty().divide(divider));
         pane.getChildren().add(line);
         divider /= 2;
      }

      primaryStage.show();
   }

   public static void main(String[] args) {

      Application.launch(args);

   }
}



PreviousNext

Related