【JavaFx】6.乌龟图

专栏目录
1.JavaFx实现闹钟小程序

2.银行账户管理员

3.大数字

4.购物车

5.文本编辑器

6.乌龟图

文章目录

    • 所有程序皆使用JDK8
    • JavaFX
      • JavaFx是什么
      • JavaFx使用注意事项
    • 乌龟图
      • 项目描述
      • 项目目录
      • gitee地址
      • 程序代码
        • turtle.java
        • Control.java
      • 运行截图


所有程序皆使用JDK8

JavaFX

JavaFx菜鸟教程
JavaFx哔哩哔哩教程

JavaFx是什么

JavaFx是java实现图形界面的一种方式,其他还有java的awt、swing,但是逐渐被淘汰。

awt --> swing --> JavaFx

javafx可以实现逻辑和样式的分离,可以使用xml和css来编写样式。

在学习之前请确保你已经熟练掌握面向对象、包装类、枚举、注解、匿名对象等内容的概念和使用

JavaFx使用注意事项

自从java11以后,jdk已经不内置javafx库,已交给开源社区管理,所以我们需要自己导入

可以到网站去下载 jar 包。注意下载的类型是sdk

若无法访问建议使用科学上网,或自行选取其他方式下载

或者使用maven引入依赖

<dependency>
       <groupId>org.openjfxgroupId>
       <artifactId>javafx-controlsartifactId>
       <version>19-ea+8version>
 dependency>

乌龟图

项目描述

Make an application that instructs a turtle icon on the screen to draw various shapes based on user input. For instance, if the user issues the command “drop pen” the turtle will start a line. If the user then issues the command “move to 1,1” it will then draw that line from its current position to coordinates 1,1… which then makes a line. The user can then tell the turtle to “lift pen” where it will then end the line. The program should allow the user to instruct the turtle how to draw lines, move to various coordinates and drop or lift its pen.
Tips:
This type of program is great if you wish to learn how to take in user instructions, parse them and then translate them into actions the turtle on screen does. So the first part is to come up with a syntax for commands that the program can parse. For instance, perhaps the command is “DROP” and the object is “PEN” in which case you can tell the turtle to drop its pen. If the user enters “DROP MARKER” it would see “DROP” and understand it, but would not understand “MARKER” so it would issue an error. Once you have a function that can parse various commands, all that is left is to instruct the turtle what to do. It is suggested that you create various functions that you can call to control the turtle. You could even make a Turtle class and have various methods to control it.
Added Difficulty:
Have the turtle draw a star from its current location with one command

项目目录

–src
------Control.java
------turtle.java

gitee地址

程序代码

turtle.java
/** @projectName: TurtleGraphics
 * @package: javaFx
 * @className: turtle
 * @author: Jiabao Yu
 * @date: 2023/1/13 16:29
 */

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.sql.Time;


public class turtle extends Application {
    protected static Stage book = new Stage();
    protected static Group canvas = new Group();
    protected static Scene board = new Scene(canvas);
    private static Polygon triangle = new Polygon();
    private static Circle circle = new Circle(7);
    private static Polygon arrow = new Polygon();
    protected static int cursor = 0;
    private static double ra = 0;
    protected static Color color = Color.BLACK;
    protected static boolean pen = false, t = false;

    @Override
    public void start(Stage primaryStage) {
        start(1000, 800, 300, 100);
    }

    public static void start(double w, double h, double x, double y) {
        book.setScene(board);
        book.setWidth(w);
        book.setHeight(h);
        book.setX(x);
        book.setY(y);
        book.show();
        arrow.setLayoutX(w / 2);
        arrow.setLayoutY(h / 2);
        circle.setCenterX(w / 2);
        circle.setCenterY(h / 2);
        triangle.setLayoutX(w / 2);
        triangle.setLayoutY(h / 2);
    }

    public static void shape(SHAPE sha) {
        switch (sha) {
            case ARROW: {
                arrow.getPoints().addAll(
                        0.0, 0.0,
                        0.0, 17.0,
                        4.0, 13.0,
                        7.0, 18.0,
                        9.0, 18.0,
                        9.0, 16.0,
                        7.0, 12.0,
                        12.0, 12.0
                );
                if (cursor == 1) {
                    arrow.setLayoutX(circle.getCenterX());
                    arrow.setLayoutY(circle.getCenterY());
                } else if (cursor == 2) {
                    arrow.setLayoutX(triangle.getLayoutX());
                    arrow.setLayoutY(triangle.getLayoutY());
                }
                canvas.getChildren().remove(arrow);
                canvas.getChildren().remove(circle);
                canvas.getChildren().remove(triangle);
                canvas.getChildren().add(arrow);
                cursor = 0;
                break;
            }
            case CIRCLE: {
                if (cursor == 0) {
                    circle.setCenterX(arrow.getLayoutX());
                    circle.setCenterY(arrow.getLayoutY());
                } else if (cursor == 2) {
                    circle.setCenterX(triangle.getLayoutX());
                    circle.setCenterY(triangle.getLayoutY());
                }
                canvas.getChildren().remove(circle);
                canvas.getChildren().remove(arrow);
                canvas.getChildren().remove(triangle);
                canvas.getChildren().add(circle);
                cursor = 1;
                break;
            }
            case TRIANGLE: {
                triangle.getPoints().addAll(
                        0.0, 0.0,
                        -7.0, 10.0 * Math.sqrt(3),
                        7.0, 10.0 * Math.sqrt(3)
                );
                if (cursor == 0) {
                    triangle.setLayoutX(arrow.getLayoutX());
                    triangle.setLayoutY(arrow.getLayoutY());
                } else if (cursor == 1) {
                    triangle.setLayoutX(circle.getCenterX());
                    triangle.setLayoutY(circle.getCenterY());
                }
                canvas.getChildren().remove(triangle);
                canvas.getChildren().remove(arrow);
                canvas.getChildren().remove(circle);
                canvas.getChildren().add(triangle);
                cursor = 2;
                break;
            }
        }
    }

    public static void cursorShow() {
        if (t) {
            arrow.setOpacity(0);
            circle.setOpacity(0);
            triangle.setOpacity(0);
        } else {
            arrow.setOpacity(1);
            circle.setOpacity(1);
            triangle.setOpacity(1);
        }
    }

    public static void fd(double distance) {
        double d = distance / 25;
        double rand = ra * 0.01745;
        Polyline line = new Polyline();
        line.setStroke(color);
        canvas.getChildren().add(line);
        switch (cursor) {
            case 0: {
                line.getPoints().addAll(arrow.getLayoutX(), arrow.getLayoutY());
                EventHandler<ActionEvent> go = e -> {
                    arrow.setLayoutX(arrow.getLayoutX() + d * Math.cos(rand));
                    arrow.setLayoutY(arrow.getLayoutY() + d * Math.sin(rand));
                    if (pen)
                        line.getPoints().addAll(arrow.getLayoutX(), arrow.getLayoutY());
                };
                Timeline tl = new Timeline(new KeyFrame(Duration.millis(50), go));
                tl.setCycleCount(25);
                tl.play();
                break;
            }
            case 1: {
                line.getPoints().addAll(circle.getCenterX(), circle.getCenterY());
                EventHandler<ActionEvent> go = e -> {
                    circle.setCenterX(circle.getCenterX() + d * Math.cos(rand));
                    circle.setCenterY(circle.getCenterY() + d * Math.sin(rand));
                    if (pen)
                        line.getPoints().addAll(circle.getCenterX(), circle.getCenterY());
                };
                Timeline tl = new Timeline(new KeyFrame(Duration.millis(50), go));
                tl.setCycleCount(50);
                tl.play();
                break;
            }
            case 2: {
                line.getPoints().addAll(triangle.getLayoutX(), triangle.getLayoutY());
                EventHandler<ActionEvent> go = e -> {
                    if (d < 0) triangle.setRotate(180);
                    triangle.setLayoutX(triangle.getLayoutX() + d * Math.cos(rand));
                    triangle.setLayoutY(triangle.getLayoutY() + d * Math.sin(rand));
                    if (pen)
                        line.getPoints().addAll(triangle.getLayoutX(), triangle.getLayoutY());
                };
                Timeline tl = new Timeline(new KeyFrame(Duration.millis(50), go));
                tl.setCycleCount(50);
                tl.play();
                break;
            }
        }

    }

    public static void right(double r) {
        ra += r;
    }

    public static void left(double r) {
        ra -= r;
    }

    public static void penup() {
        pen = false;
    }

    public static void pendown() {
        pen = true;
    }

    public enum SHAPE {
        CIRCLE, ARROW, TRIANGLE;
    }
}

Control.java
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Control extends Application {
    TextArea old = new TextArea();
    turtle Turtle = new turtle();

    @Override
    public void start(Stage primaryStage) {
        CheckBox left = new CheckBox("左旋:"), right = new CheckBox("右旋:");
        Button up = new Button("提笔"), down = new Button("落笔"), paint = new Button("绘画"),
                clean = new Button("清空"), show = new Button("隐藏");
        ComboBox color = new ComboBox(), shape = new ComboBox(),
                Lt = new ComboBox(), Rt = new ComboBox();
        Label co = new Label("颜色:"), sh = new Label("形状:"), fd = new Label("前进:");
        TextField go = new TextField();
        //
        fd.setFont(Font.font(22));
        fd.setLayoutX(70);
        fd.setLayoutY(250);
        go.setFont(Font.font(22));
        go.setLayoutX(140);
        go.setLayoutY(240);
        co.setFont(Font.font(16));
        sh.setFont(Font.font(16));
        co.setLayoutX(20);
        co.setLayoutY(300);
        sh.setLayoutX(250);
        sh.setLayoutY(300);
        color.setPrefWidth(150);
        color.setLayoutX(70);
        color.setLayoutY(300);
        shape.setPrefWidth(150);
        shape.setLayoutX(300);
        shape.setLayoutY(300);
        up.setPrefWidth(200);
        up.setPrefHeight(60);
        up.setLayoutX(20);
        up.setLayoutY(20);
        show.setPrefWidth(200);
        show.setPrefHeight(60);
        show.setLayoutX(20);
        show.setLayoutY(100);
        down.setPrefWidth(200);
        down.setPrefHeight(60);
        down.setLayoutX(270);
        down.setLayoutY(20);
        clean.setPrefWidth(200);
        clean.setPrefHeight(60);
        clean.setLayoutX(270);
        clean.setLayoutY(100);
        left.setFont(Font.font(16));
        left.setLayoutX(20);
        left.setLayoutY(200);
        Lt.setLayoutX(100);
        Lt.setLayoutY(200);
        Lt.setPrefWidth(140);
        right.setFont(Font.font(16));
        right.setLayoutX(250);
        right.setLayoutY(200);
        Rt.setLayoutX(330);
        Rt.setLayoutY(200);
        Rt.setPrefWidth(140);
        old.setPrefWidth(450);
        old.setPrefHeight(250);
        old.setLayoutX(20);
        old.setLayoutY(500);
        old.setEditable(false);
        paint.setPrefWidth(300);
        paint.setPrefHeight(90);
        paint.setLayoutX(100);
        paint.setLayoutY(400);
        //
        Lt.setValue("0");
        Rt.setValue("0");
        set(color, shape);
        Lt.setEditable(true);
        Rt.setEditable(true);
        put(Lt, Rt);
        //
        Group group = new Group();
        Scene scene = new Scene(group);
        group.getChildren().addAll(old, left, right, Lt, Rt, paint, up, down, color, shape, co, sh, clean, show, fd, go);
        Turtle.start(new Stage());
        primaryStage.setWidth(500);
        primaryStage.setHeight(Turtle.book.getHeight());
        primaryStage.setX(Turtle.book.getX() + Turtle.book.getWidth());
        primaryStage.setY(Turtle.book.getY());
        primaryStage.setScene(scene);
        primaryStage.show();
        //
        up.setOnAction(e -> {
            Turtle.penup();
        });
        down.setOnAction(e -> {
            Turtle.pendown();
        });
        show.setOnAction(e -> {
            Turtle.t = !Turtle.t;
            Turtle.cursorShow();
        });
        clean.setOnAction(e -> {
            Turtle.book.close();
            Turtle.book.show();
        });
        paint.setOnAction(e -> {
            if (left.isSelected() && !right.isSelected()) {
                Turtle.left(Double.parseDouble(Lt.getValue().toString()));
            } else if (right.isSelected() && !left.isSelected()) {
                Turtle.right(Double.parseDouble(Rt.getValue().toString()));
            }
            switch (color.getValue().toString()) {
                case "黑色": {
                    Turtle.color = Color.BLACK;
                    break;
                }
                case "蓝色": {
                    Turtle.color = Color.BLUE;
                    break;
                }
                case "绿色": {
                    Turtle.color = Color.GREEN;
                    break;
                }
                case "黄色": {
                    Turtle.color = Color.YELLOW;
                    break;
                }
                case "紫色": {
                    Turtle.color = Color.PURPLE;
                    break;
                }
                case "红色": {
                    Turtle.color = Color.RED;
                    break;
                }
            }
            switch (shape.getValue().toString()) {
                case "箭头": {
                    Turtle.shape(turtle.SHAPE.ARROW);
                    break;
                }
                case "圆形": {
                    Turtle.shape(turtle.SHAPE.CIRCLE);
                    break;
                }
                case "三角": {
                    Turtle.shape(turtle.SHAPE.TRIANGLE);
                    break;
                }
            }
            if (go.getText() != "") Turtle.fd(Double.parseDouble(go.getText()));
        });
    }

    public void set(ComboBox c1, ComboBox c2) {
        c1.setValue("黑色");
        c2.setValue("箭头");
        c1.getItems().addAll("黑色", "蓝色", "绿色", "黄色", "紫色", "红色");
        c2.getItems().addAll("箭头", "圆形", "三角");
    }

    public void put(ComboBox c1, ComboBox c2) {
        for (int i = 0; i < 361; i++) {
            c1.getItems().add("" + i);
        }
        for (int i = 0; i < 361; i++) {
            c2.getItems().add("" + i);
        }
    }
}

运行截图

【JavaFx】6.乌龟图_第1张图片

你可能感兴趣的:(JavaFX,javafx)