JavaFX实验

    xiaoxiao2024-12-25  84

    import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Duration; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; public class Exercise extends Application { private static Timeline animation1 = null; private static Timeline animation2 = null; private static Timeline animation3 = null; private static Timeline animation4 = null; @Override public void start(Stage primaryStage) { BorderPane pane = new BorderPane(); TextField tfCar1 = new TextField(); TextField tfCar2 = new TextField(); TextField tfCar3 = new TextField(); TextField tfCar4 = new TextField(); tfCar1.setPrefColumnCount(2); tfCar2.setPrefColumnCount(2); tfCar3.setPrefColumnCount(2); tfCar4.setPrefColumnCount(2); HBox carhBox = new HBox(5); carhBox.setAlignment(Pos.CENTER); carhBox.getChildren().addAll(new Label("Car 1: "), tfCar1, new Label("Car 2: "), tfCar2, new Label("Car 3: "), tfCar3, new Label("Car 4: "), tfCar4); VBox vBox = new VBox(5); CarPane car1 = new CarPane(); CarPane car2 = new CarPane(); CarPane car3 = new CarPane(); CarPane car4 = new CarPane(); vBox.getChildren().addAll(car1, car2, car3, car4); pane.setCenter(carhBox); pane.setBottom(vBox); tfCar1.setOnAction(e -> car1.setSpeed( Integer.parseInt(tfCar1.getText()))); tfCar2.setOnAction(e -> car2.setSpeed( Integer.parseInt(tfCar2.getText()))); tfCar3.setOnAction(e -> car3.setSpeed( Integer.parseInt(tfCar3.getText()))); tfCar4.setOnAction(e -> car4.setSpeed( Integer.parseInt(tfCar4.getText()))); car1.widthProperty().addListener(e -> car1.setW(car1.getWidth())); car2.widthProperty().addListener(e -> car2.setW(car2.getWidth())); car3.widthProperty().addListener(e -> car3.setW(car3.getWidth())); car4.widthProperty().addListener(e -> car4.setW(car4.getWidth())); Time time1 = new Time(); Text text1 = new Text("00:00:00"); animation1 = new Timeline( new KeyFrame(Duration.millis(1000), e -> { time1.increase(); text1.setText(time1.toString()); })); animation1.setCycleCount(Timeline.INDEFINITE); HBox hBox1 = new HBox(5); Button btStart1 = new Button("Start"); Button btClear1 = new Button("Clear"); hBox1.getChildren().addAll(btStart1, btClear1); hBox1.setAlignment(Pos.CENTER); btStart1.setOnAction(e -> { if (btStart1.getText().equals("Start")) { btStart1.setText("Pause"); animation1.play(); car1.animation.play(); } else if (btStart1.getText().equals("Pause")) { btStart1.setText("Continue"); animation1.pause(); car1.animation.pause(); } else if (btStart1.getText().equals("Continue")) { btStart1.setText("Pause"); animation1.play(); car1.animation.play(); } }); btClear1.setOnAction(e -> { text1.setText("00:00:00"); animation1.pause(); time1.reset(); btStart1.setText("Start"); car1.baseX=0; car1.setValues(); }); text1.setFont(Font.font("Times", 35)); GridPane gridPane1 = new GridPane(); gridPane1.add(text1,0,0); gridPane1.add(hBox1,0,1); Time time2 = new Time(); Text text2 = new Text("00:00:00"); animation2 = new Timeline( new KeyFrame(Duration.millis(1000), e -> { time2.increase(); text2.setText(time2.toString()); })); animation2.setCycleCount(Timeline.INDEFINITE); HBox hBox2 = new HBox(5); Button btStart2 = new Button("Start"); Button btClear2 = new Button("Clear"); hBox2.getChildren().addAll(btStart2, btClear2); hBox2.setAlignment(Pos.CENTER); btStart2.setOnAction(e -> { if (btStart2.getText().equals("Start")) { btStart2.setText("Pause"); animation2.play(); car2.animation.play(); } else if (btStart2.getText().equals("Pause")) { btStart2.setText("Continue"); animation2.pause(); car2.animation.pause(); } else if (btStart2.getText().equals("Continue")) { btStart2.setText("Pause"); animation2.play(); car2.animation.play(); } }); btClear2.setOnAction(e -> { text2.setText("00:00:00"); animation2.pause(); time2.reset(); btStart2.setText("Start"); car2.baseX=0; car2.setValues(); }); text2.setFont(Font.font("Times", 35)); GridPane gridPane2 = new GridPane(); gridPane2.add(text2,0,0); gridPane2.add(hBox2,0,1); Time time3 = new Time(); Text text3 = new Text("00:00:00"); animation3 = new Timeline( new KeyFrame(Duration.millis(1000), e -> { time3.increase(); text3.setText(time3.toString()); })); animation3.setCycleCount(Timeline.INDEFINITE); HBox hBox3 = new HBox(5); Button btStart3 = new Button("Start"); Button btClear3 = new Button("Clear"); hBox3.getChildren().addAll(btStart3, btClear3); hBox3.setAlignment(Pos.CENTER); btStart3.setOnAction(e -> { if (btStart3.getText().equals("Start")) { btStart3.setText("Pause"); animation3.play(); car3.animation.play(); } else if (btStart3.getText().equals("Pause")) { btStart3.setText("Continue"); animation3.pause(); car3.animation.pause(); } else if (btStart3.getText().equals("Continue")) { btStart3.setText("Pause"); animation3.play(); car3.animation.play(); } }); btClear3.setOnAction(e -> { text3.setText("00:00:00"); animation3.pause(); time3.reset(); btStart3.setText("Start"); car3.baseX=0; car3.setValues(); }); text3.setFont(Font.font("Times", 35)); GridPane gridPane3 = new GridPane(); gridPane3.add(text3,0,0); gridPane3.add(hBox3,0,1); Time time4 = new Time(); Text text4 = new Text("00:00:00"); animation4 = new Timeline( new KeyFrame(Duration.millis(1000), e -> { time4.increase(); text4.setText(time4.toString()); })); animation4.setCycleCount(Timeline.INDEFINITE); HBox hBox4 = new HBox(5); Button btStart4 = new Button("Start"); Button btClear4 = new Button("Clear"); hBox4.getChildren().addAll(btStart4, btClear4); hBox4.setAlignment(Pos.CENTER); btStart4.setOnAction(e -> { if (btStart4.getText().equals("Start")) { btStart4.setText("Pause"); animation4.play(); car4.animation.play(); } else if (btStart4.getText().equals("Pause")) { btStart4.setText("Continue"); animation4.pause(); car4.animation.pause(); } else if (btStart4.getText().equals("Continue")) { btStart4.setText("Pause"); animation4.play(); car4.animation.play(); } }); btClear4.setOnAction(e -> { text4.setText("00:00:00"); animation4.pause(); time4.reset(); btStart4.setText("Start"); car4.baseX=0; car4.setValues(); }); text4.setFont(Font.font("Times", 35)); GridPane gridPane4 = new GridPane(); gridPane4.add(text4,0,0); gridPane4.add(hBox4,0,1); HBox hBox = new HBox(); hBox.setSpacing(40); hBox.getChildren().addAll(gridPane1,gridPane2,gridPane3,gridPane4); pane.setTop(hBox); Scene scene = new Scene(pane, 800, 400); primaryStage.setTitle("Exercise"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args){ launch(args); } static class Time { int value = 0; int getSecond(){ return value % 60; } int getMinute(){ return value / 60 % 60; } int getHour(){ return value / 3600; } void reset(){ value = 0; } void increase(){ value++; } @Override public String toString() { String hour,minute,second; if(this.getHour()<10) hour = "0"+this.getHour(); else hour = ""+this.getHour(); if(this.getMinute()<10) minute = "0"+this.getMinute(); else minute = ""+this.getMinute(); if(this.getSecond()<10) second = "0"+this.getSecond(); else second = ""+this.getSecond(); return hour+":"+minute+":"+second; } } class CarPane extends Pane { private double w = 200; private double h = 40; private double baseX = 0; private double baseY = h; private Circle c1 = new Circle(baseX + 10 + 5, baseY - 10 + 5, 5); private Circle c2 = new Circle(baseX + 30 + 5, baseY - 10 + 5, 5); private Rectangle carBody = new Rectangle(baseX, baseY - 20, 50, 10); private Polygon carTop = new Polygon(baseX + 10, baseY - 20, baseX + 20, baseY - 30, baseX + 30, baseY - 30, baseX + 40, baseY - 20); private int speed = 50; Timeline animation = new Timeline( new KeyFrame(Duration.millis(150 - speed), e -> move())); private void setSpeed(int speed) { animation.setRate(speed); } private CarPane() { this.setStyle("-fx-border-color: black"); carBody.setFill(Color.GREEN); carTop.setFill(Color.RED); this.getChildren().addAll(c1, c2, carBody, carTop); animation.setCycleCount(Timeline.INDEFINITE); animation.pause(); requestFocus(); } private void move() { if (baseX > w) { baseX = -20; } else { baseX += 1; } if(baseX==750){ animation.pause(); Exercise.animation1.pause(); Exercise.animation2.pause(); Exercise.animation3.pause(); Exercise.animation4.pause(); } setValues(); } private void setValues() { c1.setCenterX(baseX + 10 + 5); c1.setCenterY(baseY - 10 + 5); c2.setCenterX(baseX + 30 + 5); c2.setCenterY(baseY - 10 + 5); carBody.setX(baseX); carBody.setY(baseY - 20); carTop.getPoints().clear(); carTop.getPoints().addAll(baseX + 10, baseY - 20, baseX + 20, baseY - 30, baseX + 30, baseY - 30, baseX + 40, baseY - 20); } private void setW(double w) { this.w = w; setValues(); } private void setH(double h) { this.h = h; baseY = h; setValues(); } } }

     

    最新回复(0)