JavaFX Ellipse create Cylinder

Description

JavaFX Ellipse create Cylinder


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

public class Main extends Application {
  @Override//from   w  ww.j a v a2  s.c o m
  public void start(Stage primaryStage) {

    Scene scene = new Scene(new MyCylinder(), 200, 200);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

class MyCylinder extends Pane {
  public void paint() {
    double centerX = getWidth() / 2;
    double radiusX = getWidth() * 0.8 / 2;

    Ellipse ellipse = new Ellipse(centerX, 40, radiusX, 20);
    ellipse.setFill(Color.WHITE);
    ellipse.setStroke(Color.BLACK);

    Arc arc1 = new Arc(centerX, 140, radiusX, 20, 0, 180);
    arc1.setFill(Color.WHITE);
    arc1.setStroke(Color.BLACK);
    arc1.getStrokeDashArray().addAll(6.0, 21.0);

    Arc arc2 = new Arc(centerX, 140, radiusX, 20, 180, 180);
    arc2.setFill(Color.WHITE);
    arc2.setStroke(Color.BLACK);

    getChildren().clear();
    getChildren().addAll(ellipse, arc1, arc2, new Line(centerX - radiusX, 40, centerX - radiusX, 140),
        new Line(centerX + radiusX, 40, centerX + radiusX, 140));
  }

  @Override
  public void setWidth(double width) {
    super.setWidth(width);
    paint();
  }

  @Override
  public void setHeight(double height) {
    super.setHeight(height);
    paint();
  }
}



PreviousNext

Related