package com.example.javafxproject;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Simle extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("笑脸");
primaryStage.show();
}
private Pane getPane() {
Group group = new Group();
Circle head = new Circle(200, 200, 170); //头
head.setStyle("-fx-stroke: black; -fx-fill: white;");
Polygon nose = new Polygon(200, 165, 230, 230, 170, 230); //鼻子
nose.setStyle("-fx-stroke: black; -fx-fill: white;");
Ellipse leftEye = new Ellipse(140, 140, 35, 27); //左眼
leftEye.setStyle("-fx-stroke: black; -fx-fill: white;");
Ellipse rightEye = new Ellipse(260, 140, 35, 27); //右眼
rightEye.setStyle("-fx-stroke: black; -fx-fill: white;");
Arc mouth = new Arc(200, 200, 100, 100, 220, 100); //嘴
mouth.setStyle("-fx-stroke: black; -fx-fill: null;");
mouth.setType(ArcType.OPEN);
group.getChildren().addAll(head, nose, leftEye, rightEye, new Circle(140, 140, 20),
new Circle(260, 140, 20), mouth);
head.setOnMouseClicked(e -> {
Ellipse left = new Ellipse(130, 210, 35, 27);
left.setStyle("-fx-stroke: null; -fx-fill: pink;");
Ellipse right = new Ellipse(270, 210, 35, 27);
right.setStyle("-fx-stroke: null; -fx-fill: pink;");
if (group.getChildren().contains(left) && group.getChildren().contains(right)) {
group.getChildren().remove(right);
group.getChildren().remove(left);
}else group.getChildren().addAll(left, right);
});
return new BorderPane(group);
}
}
JavaFX、绘制笑脸
于 2022-03-27 07:57:09 首次发布