JavaFX Line connect two circles

Description

JavaFX Line connect two circles


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

public class Main extends Application {
   @Override/*from w  ww .  ja v  a2 s.  c  om*/
   public void start(Stage primaryStage) {
      // Create a pane
      Pane pane = new Pane();

      // Create two circles
      Circle c1 = new Circle(20 + Math.random() * 201, 20 + Math.random() * 201, 15);
      c1.setFill(Color.WHITE);
      c1.setStroke(Color.BLACK);

      Circle c2 = new Circle(20 + Math.random() * 201, 20 + Math.random() * 201, 15);
      c2.setFill(Color.WHITE);
      c2.setStroke(Color.BLACK);

      // Create a line
      Line line = new Line(c1.getCenterX(), c1.getCenterY(), c2.getCenterX(), c2.getCenterY());

      // Add nodes to pane
      pane.getChildren().addAll(line, c1, c2, new Text(c1.getCenterX(), c1.getCenterY(), "1"),
            new Text(c2.getCenterX(), c2.getCenterY(), "2"));

      // Create a scene and place it in the stage
      Scene scene = new Scene(pane);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related