专栏目录
1.JavaFx实现闹钟小程序
文章目录
所有程序皆使用JDK8
JavaFX
JavaFx是什么
JavaFx是java实现图形界面的一种方式,其他还有java的awt、swing,但是逐渐被淘汰。
awt --> swing --> JavaFx
javafx可以实现逻辑和样式的分离,可以使用xml和css来编写样式。
在学习之前请确保你已经熟练掌握面向对象、包装类、枚举、注解、匿名对象等内容的概念和使用
JavaFx使用注意事项
自从java11以后,jdk已经不内置javafx库,已交给开源社区管理,所以我们需要自己导入
可以到网站去下载 jar 包。注意下载的类型是sdk
若无法访问建议使用科学上网,或自行选取其他方式下载
或者使用maven引入依赖
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19-ea+8</version>
</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