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

下面是一个简单的JavaFX代码示例,用于绘制中国国旗(五星红旗): ``` import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class ChinaFlag extends Application { @Override public void start(Stage primaryStage) { Group root = new Group(); // 画国旗背景 Rectangle bg = new Rectangle(0, 0, 600, 400); bg.setFill(Color.RED); root.getChildren().add(bg); // 画大星星 Polygon bigStar = new Polygon( 300, 100, 330, 160, 390, 160, 340, 195, 360, 250, 300, 220, 240, 250, 260, 195, 210, 160, 270, 160 ); bigStar.setFill(Color.YELLOW); root.getChildren().add(bigStar); // 画四个小星星 Polygon smallStar1 = new Polygon( 320, 120, 330, 130, 340, 120, 330, 110 ); smallStar1.setFill(Color.YELLOW); root.getChildren().add(smallStar1); Polygon smallStar2 = new Polygon( 360, 120, 370, 130, 380, 120, 370, 110 ); smallStar2.setFill(Color.YELLOW); root.getChildren().add(smallStar2); Polygon smallStar3 = new Polygon( 320, 170, 330, 180, 340, 170, 330, 160 ); smallStar3.setFill(Color.YELLOW); root.getChildren().add(smallStar3); Polygon smallStar4 = new Polygon( 360, 170, 370, 180, 380, 170, 370, 160 ); smallStar4.setFill(Color.YELLOW); root.getChildren().add(smallStar4); Scene scene = new Scene(root, 600, 400); primaryStage.setTitle("China Flag"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 这个代码将绘制一个具有红色背景和一个大黄色五角星,以及四个小黄色五角星的中国国旗。你可以运行这个程序并查看结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值