我的应用程序出了问题。 我必须在JavaFX中制作多线程App。 我希望有5个叫做“挖掘机”的物体,它们会在00:05:00上升(moveUp)半小时(我的特定时间),并在00:35:00“moveDown”上半小时。这意味着在00:00:00-00:04:59和01:05:00-24:00:00之间他们将“留在()”在Window的中心。总结一下,我的问题是:我想把我的“挖掘机”从窗口中心移开一个小时。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Main extends Application {
StackPane root = new StackPane();
Time time;
Text t1 = new Text();
Image excavatorImage = new Image("resources/excavator.png");
Image smallVehicleImage = new Image("resources/small.png");
Image largeVehicleImage = new Image("resources/large.png");
Excavators excavators;
ImageView imageOfExcavator1 = new ImageView();
ImageView imageOfExcavator2 = new ImageView();
ImageView imageOfExcavator3 = new ImageView();
ImageView imageOfExcavator4 = new ImageView();
ImageView imageOfExcavator5 = new ImageView();
ImageView imageOfSmallVehicle1 = new ImageView();
ImageView imageOfSmallVehicle2 = new ImageView();
ImageView imageOfSmallVehicle3 = new ImageView();
ImageView imageOfSmallVehicle4 = new ImageView();
ImageView imageOfSmallVehicle5 = new ImageView();
ImageView imageOfSmallVehicle6 = new ImageView();
ImageView imageOfSmallVehicle7 = new ImageView();
ImageView imageOfSmallVehicle8 = new ImageView();
ImageView imageOfSmallVehicle9 = new ImageView();
ImageView imageOfSmallVehicle10 = new ImageView();
ImageView imageOfLargeVehicle1 = new ImageView();
ImageView imageOfLargeVehicle2 = new ImageView();
ImageView imageOfLargeVehicle3 = new ImageView();
ImageView imageOfLargeVehicle4 = new ImageView();
ImageView imageOfLargeVehicle5 = new ImageView();
@Override
public void start(Stage primaryStage) throws Exception {
root.setStyle("-fx-background-color: #00FF00");
primaryStage.setTitle("My App");
primaryStage.setScene(new Scene(root, 1000, 800));
primaryStage.show();
checkTime();
createExcavators();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
}
public static void main(String[] args) {
launch(args);
}
public void checkTime() {
time = new Time(this);
time.start();
root.getChildren().add(t1);
}
public void updateTime(int hour, int minute, int second) {
String time = hour + ":" + minute + ":" + second;
t1.setTranslateX(-450);
t1.setTranslateY(-380);
Platform.runLater(() -> t1.setText(time));
}
public void createExcavators() {
imageOfExcavator1.setImage(excavatorImage);
imageOfExcavator2.setImage(excavatorImage);
imageOfExcavator3.setImage(excavatorImage);
imageOfExcavator4.setImage(excavatorImage);
imageOfExcavator5.setImage(excavatorImage);
root.getChildren().add(imageOfExcavator1);
root.getChildren().add(imageOfExcavator2);
root.getChildren().add(imageOfExcavator3);
root.getChildren().add(imageOfExcavator4);
root.getChildren().add(imageOfExcavator5);
}
public void moveExcavator1(double x, double y) {
imageOfExcavator1.setTranslateX(x);
imageOfExcavator1.setTranslateY(y);
}
public void moveExcavator2(double x, double y) {
imageOfExcavator2.setTranslateX(x);
imageOfExcavator2.setTranslateY(y);
}
public void moveExcavator3(double x, double y) {
imageOfExcavator3.setTranslateX(x);
imageOfExcavator3.setTranslateY(y);
}
public void moveExcavator4(double x, double y) {
imageOfExcavator4.setTranslateX(x);
imageOfExcavator4.setTranslateY(y);
}
public void moveExcavator5(double x, double y) {
imageOfExcavator5.setTranslateX(x);
imageOfExcavator5.setTranslateY(y);
}
}
第二节课:
public class Time extends Thread {
public int hour = 0;
public int minute = 0;
public int second = 0;
Main main;
Excavators excavators = new Excavators(main);
Time(Main main) {
this.main = main;
}
public void run() {
for (; ; ) {
try {
second++;
if (second == 60) {
second = 0;
minute++;
}
if (minute == 60) {
minute = 0;
hour++;
}
if (hour == 24) {
hour = 0;
}
if (hour == 0 && minute == 5 && second == 0) {
}
setTime(hour, minute, second);
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
public void setTime(int h, int m, int s) {
Excavators excavators = new Excavators(main);
excavators.start();
System.out.println(h + ":" + m + ":" + s);
main.updateTime(h, m, s);
excavators.getTime(h, m, s);
}
}
第三课:
public class Excavators extends Thread {
Main main;
int x = 0;
int y = -450;
Excavators(Main main) {
this.main = main;
}
public void run() {
for (; ; ) {
try {
System.out.println(x + "a" + y);
Thread.sleep(40);
} catch (InterruptedException e) {
}
}
}
public void setCoordinates1(double x, double y) {
main.moveExcavator1(x, y);
}
public void setCoordinates2(double x, double y) {
main.moveExcavator2(x, y);
}
public void setCoordinates3(double x, double y) {
main.moveExcavator3(x, y);
}
public void setCoordinates4(double x, double y) {
main.moveExcavator4(x, y);
}
public void setCoordinates5(double x, double y) {
main.moveExcavator5(x, y);
}
public void setCoordinates() {
setCoordinates1(-400, y);
setCoordinates2(-200, y);
setCoordinates3(0, y);
setCoordinates4(200, y);
setCoordinates5(400, y);
}
public void moveDown() {
y = y + 2;
setCoordinates();
}
public void moveUp() {
y = y + 2;
setCoordinates();
}
public void stay() {
y = 0;
setCoordinates();
}
public void getTime(int h, int m, int s) {
if (h == 0 && m >= 5 && m < 35) {
moveUp();
} else if ((h == 0 && m >= 35) || (h == 1 && m < 5)) {
moveDown();
} else {
stay();
}
}
}