目录
题目:**16.25(赛车)
编写一个程序,模拟四辆赛车,如图16-47b所示。可以对每辆车设置速度,用100表示最高速。
-
习题思路
- 将15.29题中的车面板修改为一个自定义面板(自带动画功能)。
- 创建一个HBox,添加四个标签Label和四个输入框TextField。
- 创建一个VBox,添加HBox和四个车面板。
- 为每一个输入框TextField注册一个键盘事件,当Enter键被按下时,将对应的车动画的速率设置为输入框内的数字。
15.29题:Java语言程序设计基础篇_编程练习题**15.29 (赛车)-CSDN博客
-
代码示例
编程练习题16_25FourRacingCars.java
package chapter_16;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class 编程练习题16_25FourRacingCars extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
HBox hBox = new HBox(5);
hBox.setAlignment(Pos.CENTER);
TextField tf1 = new TextField("10");
tf1.setPrefWidth(50);
TextField tf2 = new TextField("10");
tf2.setPrefWidth(50);
TextField tf3 = new TextField("10");
tf3.setPrefWidth(50);
TextField tf4 = new TextField("10");
tf4.setPrefWidth(50);
Label lb1 = new Label("Car 1:",tf1);
lb1.setContentDisplay(ContentDisplay.RIGHT);
Label lb2 = new Label("Car 2:",tf2);
lb2.setContentDisplay(ContentDisplay.RIGHT);
Label lb3 = new Label("Car 3:",tf3);
lb3.setContentDisplay(ContentDisplay.RIGHT);
Label lb4 = new Label("Car 4:",tf4);
lb4.setContentDisplay(ContentDisplay.RIGHT);
hBox.getChildren().addAll(lb1,tf1, lb2,tf2, lb3,tf3, lb4,tf4);
VBox vBox = new VBox(10);
编程练习题16_25RacingPane racingPane1 = new 编程练习题16_25RacingPane(400, 50);
racingPane1.setStyle("-fx-border-color:black");
编程练习题16_25RacingPane racingPane2 = new 编程练习题16_25RacingPane(400, 50);
racingPane2.setStyle("-fx-border-color:black");
编程练习题16_25RacingPane racingPane3 = new 编程练习题16_25RacingPane(400, 50);
racingPane3.setStyle("-fx-border-color:black");
编程练习题16_25RacingPane racingPane4 = new 编程练习题16_25RacingPane(400, 50);
racingPane4.setStyle("-fx-border-color:black");
vBox.getChildren().addAll(hBox,racingPane1, racingPane2, racingPane3, racingPane4);
tf1.setOnKeyPressed(e ->{
String text = tf1.getText();
if(e.getCode() == KeyCode.ENTER&&
!text.equals("")&&
Double.valueOf(text)>0
&&Double.valueOf(text)<=100) {
double rate = Double.valueOf(text)/10.0;
racingPane1.setRate(rate);
}
});
tf2.setOnKeyPressed(e ->{
String text = tf2.getText();
if(e.getCode() == KeyCode.ENTER&&
!text.equals("")&&
Double.valueOf(text)>0
&&Double.valueOf(text)<=100) {
double rate = Double.valueOf(text)/10.0;
racingPane2.setRate(rate);
}
});
tf3.setOnKeyPressed(e ->{
String text = tf3.getText();
if(e.getCode() == KeyCode.ENTER&&
!text.equals("")&&
Double.valueOf(text)>0
&&Double.valueOf(text)<=100) {
double rate = Double.valueOf(text)/10.0;
racingPane3.setRate(rate);
}
});
tf4.setOnKeyPressed(e ->{
String text = tf4.getText();
if(e.getCode() == KeyCode.ENTER&&
!text.equals("")&&
Double.valueOf(text)>0
&&Double.valueOf(text)<=100) {
double rate = Double.valueOf(text)/10.0;
racingPane4.setRate(rate);
}
});
Scene scene = new Scene(vBox,400, 200);
primaryStage.setTitle("编程练习题16_25FourRacingCars");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
编程练习题16_25RacingPane.java
package chapter_16;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
public class 编程练习题16_25RacingPane extends Pane{
private int width;
private int height;
private PathTransition pt;
private Line line;
private Pane pane;
编程练习题16_25RacingPane(){
drawCar();
startAnimation();
}
编程练习题16_25RacingPane(int w, int h){
this.width = w;
this.height = h;
drawCar();
startAnimation();
}
void setRate(double rate){
pt.setRate(rate);
}
public void drawCar() {
getChildren().clear();
double x = 20;
double y = height/2.0;
line = new Line(x,height/2.0, width*1.5, height/2.0);
pane = new Pane();
Polygon trapezoid = new Polygon(
x+20, y-30,
x+30, y-30,
x+40, y-20,
x+10, y-20);
Rectangle rectangle = new Rectangle(x, y-20,50, 10);
Circle wheel1 = new Circle(x+15, y-5,5);
Circle wheel2 = new Circle(x+35, y-5,5);
trapezoid.setFill(Color.BLUE);
rectangle.setFill(Color.ORANGE);
wheel1.setFill(Color.BLACK);
wheel2.setFill(Color.BLACK);
pane.getChildren().addAll(trapezoid, rectangle, wheel1, wheel2);
getChildren().add(pane);
}
public void startAnimation(){
pt = new PathTransition();
pt.setDuration(Duration.millis(3000));
pt.setPath(line);
pt.setNode(pane);
pt.setCycleCount(Timeline.INDEFINITE);
pt.play();
}
}
-
结果展示