多线程应用程序,缺少Java中的图像操作

时间:2018-05-25 12:30:41

标签: java multithreading javafx java-8 javafx-8

我的multiThread应用程序出了问题。 当我从" Vehicles"中调用drive()方法时我希望对象(图片)向左移动。这种机制有效,但遗憾的是我遇到了问题。有时,当我运行应用程序时,会发生其中一个对象停止移动。有时它是一些对象。通常,在几分之一秒后,图像再次向左移动。这种行为是不可重复的,不幸的是,现在小型车辆发生故障并立即站立并不罕见。也许你们其中一个人知道如何解决这个问题,以便车辆一直平稳运行?如果我在标记的位置放置一个代码(System.out.println)并且我想将文本显示到控制台,那么这个机制可以正常工作,只有当图像被移动时才会中断。

头等舱:

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;

import java.util.concurrent.locks.ReentrantLock;


public class Main extends Application {
ReentrantLock lock = new ReentrantLock();
StackPane root = new StackPane();
Time time;
Vehicles vehicles;
SmallVehicle1 smallVehicle1;
Text t1 = new Text();
Text t2 = new Text();
Image excavatorImage = new Image("resources/excavator.png");
//    Image smallVehicleImage = new Image("resources/small.png");
Image largeVehicleImage = new Image("resources/large.png");
ImageView imageOfExcavator1 = new ImageView();
ImageView imageOfExcavator2 = new ImageView();
ImageView imageOfExcavator3 = new ImageView();
ImageView imageOfExcavator4 = new ImageView();
ImageView imageOfExcavator5 = new ImageView();
ImageView imageOfSmallVehicle1 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle2 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle3 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle4 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle5 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle6 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle7 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle8 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle9 = new ImageView(new Image("resources/small.png"));
ImageView imageOfSmallVehicle10 = new ImageView(new 
Image("resources/small.png"));
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();
    lock.lock();
    checkTime();
    lock.unlock();
    createExcavators();
    createSmallVehicle();
    moveVehicles();

    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 moveVehicles(){
    vehicles = new Vehicles(this);
    vehicles.start();
}

public void checkTime() {
    time = new Time(this);
    time.start();
    root.getChildren().add(t1);
    root.getChildren().add(t2);
}

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 takeBreak() {
    Platform.runLater(() -> t2.setText("Now we have got break!"));
}

public void stopBreak() {
    takeBreak();
    t2.setTranslateY(-1000);
    t2.setTranslateX(-1000);
}

public void startBreak() {
    takeBreak();
    t2.setTranslateY(0);
    t2.setTranslateX(0);
}

public void createExcavators() {
    imageOfExcavator1.setImage(excavatorImage);
    imageOfExcavator2.setImage(excavatorImage);
    imageOfExcavator3.setImage(excavatorImage);
    imageOfExcavator4.setImage(excavatorImage);
    imageOfExcavator5.setImage(excavatorImage);
    root.getChildren().addAll(imageOfExcavator1, imageOfExcavator2, imageOfExcavator3, imageOfExcavator4, 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 void createSmallVehicle() {
    root.getChildren().addAll(imageOfSmallVehicle1, imageOfSmallVehicle2, imageOfSmallVehicle3, imageOfSmallVehicle4, imageOfSmallVehicle5, imageOfSmallVehicle6, imageOfSmallVehicle7, imageOfSmallVehicle8, imageOfSmallVehicle9, imageOfSmallVehicle10);

}

public void moveSmallVehicle1(double x, double y) {
    imageOfSmallVehicle1.setTranslateX(x);
    imageOfSmallVehicle1.setTranslateY(y);
}

public void moveSmallVehicle2(double x, double y) {
    imageOfSmallVehicle2.setTranslateX(x);
    imageOfSmallVehicle2.setTranslateY(y);
}

public void moveSmallVehicle3(double x, double y) {
    imageOfSmallVehicle3.setTranslateX(x);
    imageOfSmallVehicle3.setTranslateY(y);
}

public void moveSmallVehicle4(double x, double y) {
    imageOfSmallVehicle4.setTranslateX(x);
    imageOfSmallVehicle4.setTranslateY(y);
}

public void moveSmallVehicle5(double x, double y) {
    imageOfSmallVehicle5.setTranslateX(x);
    imageOfSmallVehicle5.setTranslateY(y);
}

public void moveSmallVehicle6(double x, double y) {
    imageOfSmallVehicle6.setTranslateX(x);
    imageOfSmallVehicle6.setTranslateY(y);
}

public void moveSmallVehicle7(double x, double y) {
    imageOfSmallVehicle7.setTranslateX(x);
    imageOfSmallVehicle7.setTranslateY(y);
}

public void moveSmallVehicle8(double x, double y) {
    imageOfSmallVehicle8.setTranslateX(x);
    imageOfSmallVehicle8.setTranslateY(y);
}

public void moveSmallVehicle9(double x, double y) {
    imageOfSmallVehicle9.setTranslateX(x);
    imageOfSmallVehicle9.setTranslateY(y);
}

public void moveSmallVehicle10(double x, double y) {
    imageOfSmallVehicle10.setTranslateX(x);
    imageOfSmallVehicle10.setTranslateY(y);
}

public void createLargeVehicle() {
    imageOfLargeVehicle1.setImage(largeVehicleImage);
    imageOfLargeVehicle2.setImage(largeVehicleImage);
    imageOfLargeVehicle3.setImage(largeVehicleImage);
    imageOfLargeVehicle4.setImage(largeVehicleImage);
    imageOfLargeVehicle5.setImage(largeVehicleImage);
    root.getChildren().addAll(imageOfLargeVehicle1, imageOfLargeVehicle2, imageOfLargeVehicle3, imageOfLargeVehicle4, imageOfLargeVehicle5);
}

public void moveLargeVehicle1(double x, double y) {
    imageOfLargeVehicle1.setTranslateX(x);
    imageOfLargeVehicle1.setTranslateY(y);
}

public void moveLargeVehicle2(double x, double y) {
    imageOfLargeVehicle2.setTranslateX(x);
    imageOfLargeVehicle2.setTranslateY(y);
}

public void moveLargeVehicle3(double x, double y) {
    imageOfLargeVehicle3.setTranslateX(x);
    imageOfLargeVehicle3.setTranslateY(y);
}

public void moveLargeVehicle4(double x, double y) {
    imageOfLargeVehicle4.setTranslateX(x);
    imageOfLargeVehicle4.setTranslateY(y);
}

public void moveLargeVehicle5(double x, double y) {
    imageOfLargeVehicle5.setTranslateX(x);
    imageOfLargeVehicle5.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 = second + 2;
            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(20);
        } catch (InterruptedException e) {
        }
    }
}


public void setTime(int h, int m, int s) {
    Excavators excavators = new Excavators(main);
    excavators.start(h, m, s);
    System.out.println(h + ":" + m + ":" + s);
    main.updateTime(h, m, s);

}
}

第三课:

public class Excavators extends Thread {
Main main;
int x = 0;
int y = 0;

Excavators(Main main) {
    this.main = main;
}


public void start(int h, int m, int s){
    getTime(h, m, s);
}

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 = 0;
    setCoordinates();
    main.stopBreak();
}

public void moveUp() {
    y = -200;
    setCoordinates();
    main.startBreak();
}

public void stay() {
    y = 0;
    setCoordinates();
    main.stopBreak();
}

public void getTime(int h, int m, int s) {
    if (h == 0 && m >= 0 && m < 5) {
        moveDown();
    } else if (h == 0 && m >= 5 && m < 15) {
        moveUp();
    } else if (h == 0 && m >= 15 && m < 30) {
        moveDown();
    } else if(h == 0 && m >= 30 && m < 45){
        moveUp();
    }else if(h == 0 && m >= 45 && m < 60){
        moveDown();
    }else if(h == 1 && m >= 0 && m < 15){
        moveUp();
    }else{
        stay();
    }
}
}

第四课::

public class Vehicles extends Thread{
Main main;
double x = 100;
double y = 120;
SmallVehicle1 smallVehicle1;

Vehicles(Main main){
    this.main = main;

}



public synchronized void run(){
    for (; ; ) {
        try{
            x = x - 5;
            drive(x, y);
            Thread.sleep(40);
        }catch (InterruptedException ex){
        }
    }
}



public void reservation(){

}

public void startLoading(){

}

public void drive(double x, double y){

    /*
    HERE IS THE PROBLEM
    Objects are moving but sometimes they are stopped.
     */
        main.imageOfSmallVehicle1.setTranslateX(x);
        main.imageOfSmallVehicle2.setTranslateX(x + 200);
        main.imageOfSmallVehicle3.setTranslateX(x + 400);
        main.imageOfSmallVehicle4.setTranslateX(x + 600);
        main.imageOfSmallVehicle5.setTranslateX(x + 800);
        main.imageOfSmallVehicle6.setTranslateX(x + 1000);
        main.imageOfSmallVehicle7.setTranslateX(x + 1200);
        main.imageOfSmallVehicle8.setTranslateX(x + 1400);
        main.imageOfSmallVehicle9.setTranslateX(x + 1600);
        main.imageOfSmallVehicle10.setTranslateX(x + 1800);
}





public void checkReservation(){

}

public void stopLoading(){

}

public void smallVehicle(){

}

public void largeVehicle(){

}


}

0 个答案:

没有答案