JavaFX、绘制笑脸


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);
    }
}

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值