在javafx中同时有2个或更多个线程

时间:2018-05-25 11:51:14

标签: java multithreading javafx

我正在做一个javaFX动画,我做了以下课程:

public class Visual extends javax.swing.JFrame implements Runnable {

    private static final int JFXPANEL_WIDTH_INT = 360;
    private static final int JFXPANEL_HEIGHT_INT = 550;
    private static JFXPanel fxContainer;
    private static Train train;
    private static AtomicBoolean running;

    public Visual(Train t) {
        Thread thread = new Thread(this);//new thread start
        thread.start();
        running = new AtomicBoolean(true);
        train = t;

        this.setSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
        this.setTitle(train.getId());
        this.setResizable(false);

        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
        add(fxContainer, BorderLayout.CENTER);

        this.setVisible(true);

        addWindowListener(exitListener);
    }

    @Override
    public void run() {
        while (train.getRoute() == null && running.get()) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }

        while (running.get()) {
            createScene(train);
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
                //Logger.logException(ex, "");
            }
        }
    }

    private static int nbSectors = 0;

    private void createScene(Train t) {
        if (t.getRoute() != null) {

            nbSectors = 0;

            for (Block s : t.getRoute().getBlocks()) {
                nbSectors += s.getSectors().size();
            }
            System.out.println("number of sectors : " + t.getId());

            Group root = new Group();
            Scene scene = new Scene(root, 350, 550, Color.ALICEBLUE);

            Line l = new Line(100, 70, 100, 450);

            l.setStrokeWidth(4);
            l.setStrokeLineCap(StrokeLineCap.ROUND);

            int nbStations = t.getLine().stations.size() - 1;

            int m = 0;
            boolean skipped = false;
            for (Block b : t.getRoute().getBlocks()) {
                for (Sector s : b.getSectors()) {
                    if (s.isOccupied()) {
                        root.getChildren().add(new Circle(100, 70 + m * (380. / nbSectors), 20, Color.RED));
                        skipped = true;
                        break;
                    }
                    if (skipped) {
                        break;
                    }
                    m++;
                }
                m++;
            }

            int i = 0;
            // System.out.println("nb de lignes : "+nbLines);
            for (Station station : t.getLine().stations) {
                root.getChildren().add(new Text(130, 70 + i * (380 / nbStations), station.getId()));

                root.getChildren().add(new Circle(100, 70 + i * (380 / nbStations), 10, Color.BLACK));
                i++;
            }
            for (int j = 0; j < nbSectors; j++) {
                root.getChildren().add(new Line(95, 70 + j * (380 / nbSectors), 105, 70 + j * (380 / nbSectors)));
            }

            root.getChildren().add(l);

            fxContainer.setScene(scene);
        }

    }

    public final void stop() {
        running.set(false);
    }

    WindowListener exitListener = new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {

            stop();

        }
    };
}

例如,当我在GUI中执行一个new Visual(greenTrain)时,一切都运行良好。当我启动多个可视窗口时会出现此问题。终端中没有错误,但只创建了最后一个Visual实例。其他人之前什么都不做。而且我不明白为什么,因为每个人都应该在一个不同的主题中,不是吗?

感谢您的帮助;)

0 个答案:

没有答案
相关问题