javafx实验

    xiaoxiao2023-10-13  132

    import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.input.KeyCode; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class Exercise16_25 extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { 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 hBox = new HBox(5); hBox.setAlignment(Pos.CENTER); hBox.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); BorderPane pane = new BorderPane(); pane.setTop(hBox); pane.setCenter(vBox); // Create a scene and place it in the stage Scene scene = new Scene(pane, 400, 200); primaryStage.setTitle("Exercise16_25"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage 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())); } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } } 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; KeyFrame kf = new KeyFrame(Duration.millis(150 - speed), e -> move()); Timeline animation = new Timeline( new KeyFrame(Duration.millis(150 - speed), e -> move())); public void setSpeed(int speed) { animation.setRate(speed); } public 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.play(); // Start animation setOnMousePressed(e -> { animation.pause(); }); setOnMouseReleased(e -> { animation.play(); }); requestFocus(); setOnKeyPressed(e -> { if (e.getCode() == KeyCode.UP) { animation.setRate(animation.getRate() + 1); } else if (e.getCode() == KeyCode.DOWN) { animation.setRate(animation.getRate() - 1); } }); } public void move() { if (baseX > w) { baseX = -20; } else { baseX += 1; } setValues(); } public 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); } public void setW(double w) { this.w = w; setValues(); } public void setH(double h) { this.h = h; baseY = h; setValues(); } } import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Duration; public class Exercise16_20 extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Time time = new Time(); Text text = new Text("00:00:00"); Timeline animation = new Timeline( new KeyFrame(Duration.millis(1000), e -> { time.increase(); text.setText(time.toString()); })); animation.setCycleCount(Timeline.INDEFINITE); HBox hBox = new HBox(5); Button btStart = new Button("Start"); Button btClear = new Button("Clear"); hBox.getChildren().addAll(btStart, btClear); hBox.setAlignment(Pos.CENTER); btStart.setOnAction(e -> { if (btStart.getText().equals("Start")) { btStart.setText("Pause"); animation.play(); } else if (btStart.getText().equals("Pause")) { btStart.setText("Resume"); animation.pause(); } else if (btStart.getText().equals("Resume")) { btStart.setText("Pause"); animation.play(); } }); btClear.setOnAction(e -> { text.setText("00:00:00"); animation.pause(); btStart.setText("Start"); }); text.setFont(Font.font("Times", 35)); BorderPane pane = new BorderPane(); pane.setBottom(hBox); pane.setCenter(text); // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 100); primaryStage.setTitle("Exercise16_20"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ 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() { return getTwoDigitString(getHour()) + ":" + getTwoDigitString(getMinute()) + ":" + getTwoDigitString(getSecond()); } static String getTwoDigitString(int v) { if (v < 10) return "0" + v; else return "" + v; } } }

     

    最新回复(0)