movingCar JavaFX实现

  1. Write a program that simulates car racing, as shown in Figure 1. The car moves from left to right. When it hits the right end, it restarts from the left and continues the same process. You can use a timer to control animation. Redraw the car with a new base coordinates (x, y), as shown in Figure 15.34b. Also let the user pause/resume the animation with a button press/release and increase/decrease the car speed by pressing the UP and DOWN arrow keys.
  2. Write a program that simulates four cars racing, as shown in Figure 2. You can set the speed for each car, with maximum 100.
package experiment7;

import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import static javafx.scene.paint.Color.*;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.awt.*;

public class movingCar extends Application {


    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        Group group=new Group();
        // 添加组件
        Circle leftwheel = new Circle(115,113,5,BLACK);
        Circle rightwheel = new Circle(135,113,5,BLACK);
        Rectangle body = new Rectangle(100,100, 50,10);
        Polygon hat = new Polygon();
        ObservableList<Double> list=hat.getPoints();
        list.addAll(120.0,90.0,110.0,100.0,140.0,100.0,130.0,90.0);
        hat.setFill(DARKCYAN);
        body.setFill(BLUE);


        Circle leftwheel2 = new Circle(115,153,5,BLACK);
        Circle rightwheel2 = new Circle(135,153,5,BLACK);
        Rectangle body2 = new Rectangle(100,140, 50,10);
        Polygon hat2 = new Polygon();
        ObservableList<Double> list2=hat2.getPoints();
        list2.addAll(120.0,130.0,110.0,140.0,140.0,140.0,130.0,130.0);
        hat2.setFill(DARKCYAN);
        body2.setFill(BLUE);


        Circle leftwheel3 = new Circle(115,113,5,BLACK);
        Circle rightwheel3 = new Circle(135,113,5,BLACK);
        Rectangle body3 = new Rectangle(100,100, 50,10);
        Polygon hat3 = new Polygon();
        ObservableList<Double> list3=hat3.getPoints();
        list3.addAll(120.0,90.0,110.0,100.0,140.0,100.0,130.0,90.0);
        hat3.setFill(DARKCYAN);
        body3.setFill(BLUE);


        Circle leftwheel4 = new Circle(115,113,5,BLACK);
        Circle rightwheel4 = new Circle(135,113,5,BLACK);
        Rectangle body4 = new Rectangle(100,100, 50,10);
        Polygon hat4 = new Polygon();
        ObservableList<Double> list4=hat4.getPoints();
        list4.addAll(120.0,90.0,110.0,100.0,140.0,100.0,130.0,90.0);
        hat4.setFill(DARKCYAN);
        body4.setFill(BLUE);


        group.getChildren().addAll(leftwheel,rightwheel,body,hat);
        pane.getChildren().add(group);

        Group group2=new Group();
        group2.getChildren().addAll(leftwheel2,rightwheel2,body2,hat2);
        pane.getChildren().add(group2);

        Group group3=new Group();
        group3.getChildren().addAll(leftwheel3,rightwheel3,body3,hat3);
        pane.getChildren().add(group3);

        Group group4=new Group();
        group4.getChildren().addAll(leftwheel4,rightwheel4,body4,hat4);
        pane.getChildren().add(group4);

        //HBox paneForText=new HBox(20);
        GridPane paneForText=new GridPane();
        paneForText.setHgap(3);
        paneForText.setVgap(3);
        paneForText.setPadding(new Insets(5,5,5,5));
        TextField car1=new TextField();
        car1.setPrefWidth(40.0);
        TextField car2=new TextField();
        car2.setPrefWidth(40.0);
        TextField car3=new TextField();
        car3.setPrefWidth(40.0);
        TextField car4=new TextField();
        car4.setPrefWidth(40.0);
        paneForText.add(new Label("car1:"),0,0);
        paneForText.add(car1,1,0);
        paneForText.add(new Label("car2:"),2,0);
        paneForText.add(car2,3,0);
        paneForText.add(new Label("car3:"),4,0);
        paneForText.add(car3,5,0);
        paneForText.add(new Label("car4:"),6,0);
        paneForText.add(car4,7,0);
        paneForText.setAlignment(Pos.CENTER);
        pane.setTop(paneForText);

        PathTransition pathTransition=new PathTransition(Duration.seconds(2), new Line(-30, 60, 530, 60), group);
        PathTransition pathTransition2=new PathTransition(Duration.seconds(4), new Line(-30, 120, 530, 120), group2);
        PathTransition pathTransition3=new PathTransition(Duration.seconds(6), new Line(-30, 200, 530, 200), group3);
        PathTransition pathTransition4=new PathTransition(Duration.seconds(8), new Line(-30, 280, 530, 280), group4);

        pathTransition.setCycleCount(Animation.INDEFINITE);
        pathTransition.play();
        pathTransition2.setCycleCount(Animation.INDEFINITE);
        pathTransition2.play();
        pathTransition3.setCycleCount(Animation.INDEFINITE);
        pathTransition3.play();
        pathTransition4.setCycleCount(Animation.INDEFINITE);
        pathTransition4.play();

        Scene scene = new Scene(pane, 500, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Homework");
        primaryStage.show();
        group.requestFocus();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值