Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX创建精确的进度条_Java_Javafx - Fatal编程技术网

JavaFX创建精确的进度条

JavaFX创建精确的进度条,java,javafx,Java,Javafx,我环顾四周,找不到任何像样的片段来告诉你如何创建准确的进度条。我看到的所有这些都是通过使用Thread.sleep(10)来表示执行“work”的线程上的一些工作负载的廉价黑客攻击。在某些情况下,这是非常不准确的,因为它实际上没有考虑线程的工作负载。我也不想使用不确定的进度条,因为它们看起来很糟糕 我的问题是:如何创建一个精确的进度条(非不确定),该进度条与执行所有“工作”的线程并行运行,在本例中,该线程将数千个图像写入一个目录。如果您不知道线程的工作负载,也不知道实际转储所有线程需要多长时间。

我环顾四周,找不到任何像样的片段来告诉你如何创建准确的进度条。我看到的所有这些都是通过使用
Thread.sleep(10)
来表示执行“work”的线程上的一些工作负载的廉价黑客攻击。在某些情况下,这是非常不准确的,因为它实际上没有考虑线程的工作负载。我也不想使用不确定的进度条,因为它们看起来很糟糕

我的问题是:如何创建一个精确的进度条(非不确定),该进度条与执行所有“工作”的线程并行运行,在本例中,该线程将数千个图像写入一个目录。如果您不知道线程的工作负载,也不知道实际转储所有线程需要多长时间。最好的方法是什么?我试图让这一切变得现实,而不是仅仅为了化妆品而显示进度条

假设我有一个这样的程序

      @FXML
  private void dump(ActionEvent e) {
        try {
                 if (e.getSource() == spriteDumpMI) {

                    Thread thread = new Thread(() -> {

                          try (Cache cache = new Cache(FileStore.open(Constants.CACHE_PATH))) {
                                ReferenceTable table = ReferenceTable.decode(Container
                                            .decode(cache.getStore().read(255, 8)).getData());

                                Platform.runLater(() -> {
                                      createTask(10, table.capacity());
                                });


                                for (int i = 0; i < table.capacity(); i++) {
                                      if (table.getEntry(i) == null)
                                            continue;

                                      Container container = cache.read(8, i);
                                      Sprite sprite = Sprite.decode(container.getData());

                                      File dir = new File(Constants.SPRITE_PATH);

                                      if (!dir.exists()) {
                                            dir.mkdir();
                                      }

                                      for (int frame = 0; frame < sprite.size(); frame++) {
                                            File file = new File(Constants.SPRITE_PATH,
                                                        i + "_" + frame + ".png");
                                            BufferedImage image = sprite.getFrame(frame);

                                            ImageIO.write(image, "png", file);
                                      }

                                }
                          } catch (FileNotFoundException e1) {
                                e1.printStackTrace();
                          } catch (IOException e1) {
                                e1.printStackTrace();
                          }

                    });

                    thread.start();

              }
        } catch (Exception ex) {
              ex.printStackTrace();
        }
  }
我知道我可以在循环中更新进度条,但我不知道如何在不创建大量线程和新任务的情况下更新进度条(这也很糟糕)

方法createTask

      private void createTask(int workload, int max) {

        Task<Boolean> task = taskCreator(workload, max);

        progressBar.setVisible(true);
        progressI.setVisible(true);

        progressBar.progressProperty().unbind();
        progressBar.progressProperty().bind(task.progressProperty());

        progressI.progressProperty().unbind();
        progressI.progressProperty().bind(task.progressProperty());

        new Thread(task).start();
  }

  private Task<Boolean> taskCreator(int workload, int max) {
        return new Task<Boolean>() {

              @Override
              protected Boolean call() throws Exception {

                    progressText.setText("In Progress");
                    progressText.setFill(Color.WHITE);

                    for (int index = 0; index < max; index++) {
                          Thread.sleep(workload);

                          updateProgress(index, max);
                    }

                    progressText.setText("Complete!");
                    progressText.setFill(Color.GREEN);

                    Thread.sleep(1000);

                    progressBar.setVisible(false);
                    progressI.setVisible(false);
                    progressText.setText("");

                    return true;

              }

        };
  }
private void createTask(int工作负载,int最大值){
任务任务=任务创建者(最大工作负载);
progressBar.setVisible(true);
progressI.setVisible(真);
progressBar.progressProperty().unbind();
progressBar.progressProperty().bind(task.progressProperty());
progressI.progressProperty().unbind();
progressI.progressProperty().bind(task.progressProperty());
新线程(任务).start();
}
专用任务taskCreator(int工作负载,int最大值){
返回新任务(){
@凌驾
受保护的布尔调用()引发异常{
progressText.setText(“进行中”);
progressText.setFill(颜色为白色);
对于(int index=0;index希望这能有所帮助

 @FXML
 private void dump(ActionEvent e) {

    try {
             if (e.getSource() == spriteDumpMI) {

                progressBar.setVisible(true);
                progressI.setVisible(true);

                progressBar.progressProperty().unbind();
                progressBar.progressProperty().bind(task.progressProperty());

                progressI.progressProperty().unbind();
                progressI.progressProperty().bind(task.progressProperty());

                progressText.textProperty().unbind();
                progressText.textProperty().bind(task.messageProperty());
                progressText.setFill(Color.GREEN);

                Task task = new Task<Boolean>() {

                  @Override
                  protected Boolean call() throws Exception {
                        updateMessage("In Progress");

                        try (Cache cache = new Cache(FileStore.open(Constants.CACHE_PATH))) {
                                ReferenceTable table = ReferenceTable.decode(Container
                                            .decode(cache.getStore().read(255, 8)).getData());
                                long count;
                                for (int i = 0; i < table.capacity(); i++) {
                                      if (table.getEntry(i) == null)
                                            continue;

                                      Container container = cache.read(8, i);
                                      Sprite sprite = Sprite.decode(container.getData());

                                      File dir = new File(Constants.SPRITE_PATH);

                                      if (!dir.exists()) {
                                            dir.mkdir();
                                      }

                                      for (int frame = 0; frame < sprite.size(); frame++) {
                                            count++;
                                      }

                                }

                                int current;
                                for (int i = 0; i < table.capacity(); i++) {
                                      if (table.getEntry(i) == null)
                                            continue;

                                      Container container = cache.read(8, i);
                                      Sprite sprite = Sprite.decode(container.getData());

                                      File dir = new File(Constants.SPRITE_PATH);

                                      if (!dir.exists()) {
                                            dir.mkdir();
                                      }

                                      for (int frame = 0; frame < sprite.size(); frame++) {
                                            File file = new File(Constants.SPRITE_PATH,
                                                        i + "_" + frame + ".png");
                                            BufferedImage image = sprite.getFrame(frame);

                                            ImageIO.write(image, "png", file);
                                            current++;
                                            updateProgress(current, count);
                                      }

                                }
                                updateMessage("Complete!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          } catch (FileNotFoundException e1) {
                                e1.printStackTrace();
                                updateMessage("failed!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          } catch (IOException e1) {
                                e1.printStackTrace();
                                updateMessage("failed!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          }
                        return true;

                      }

                };

          }
          task.setOnSucceeded(e ->{
                progressBar.setVisible(false);
                progressI.setVisible(false);
                progressText.setText("");
          });
          task.setOnFailed(e ->{
                progressBar.setVisible(false);
                progressI.setVisible(false);
                progressText.setText("");
          });

          new Thread(task).start();
    } catch (Exception ex) {
          ex.printStackTrace();
    }
@FXML
私有无效转储(ActionEvent e){
试一试{
如果(例如getSource()==spriteDumpMI){
progressBar.setVisible(true);
progressI.setVisible(真);
progressBar.progressProperty().unbind();
progressBar.progressProperty().bind(task.progressProperty());
progressI.progressProperty().unbind();
progressI.progressProperty().bind(task.progressProperty());
progressText.textProperty().unbind();
progressText.textProperty().bind(task.messageProperty());
progressText.setFill(颜色为绿色);
任务=新任务(){
@凌驾
受保护的布尔调用()引发异常{
更新消息(“进行中”);
try(缓存=新缓存(FileStore.open(Constants.Cache_PATH))){
ReferenceTable=ReferenceTable.decode(容器
.decode(cache.getStore().read(255,8)).getData());
长计数;
对于(int i=0;i{
progressText.setFill(颜色为绿色);
});
 @FXML
 private void dump(ActionEvent e) {

    try {
             if (e.getSource() == spriteDumpMI) {

                progressBar.setVisible(true);
                progressI.setVisible(true);

                progressBar.progressProperty().unbind();
                progressBar.progressProperty().bind(task.progressProperty());

                progressI.progressProperty().unbind();
                progressI.progressProperty().bind(task.progressProperty());

                progressText.textProperty().unbind();
                progressText.textProperty().bind(task.messageProperty());
                progressText.setFill(Color.GREEN);

                Task task = new Task<Boolean>() {

                  @Override
                  protected Boolean call() throws Exception {
                        updateMessage("In Progress");

                        try (Cache cache = new Cache(FileStore.open(Constants.CACHE_PATH))) {
                                ReferenceTable table = ReferenceTable.decode(Container
                                            .decode(cache.getStore().read(255, 8)).getData());
                                long count;
                                for (int i = 0; i < table.capacity(); i++) {
                                      if (table.getEntry(i) == null)
                                            continue;

                                      Container container = cache.read(8, i);
                                      Sprite sprite = Sprite.decode(container.getData());

                                      File dir = new File(Constants.SPRITE_PATH);

                                      if (!dir.exists()) {
                                            dir.mkdir();
                                      }

                                      for (int frame = 0; frame < sprite.size(); frame++) {
                                            count++;
                                      }

                                }

                                int current;
                                for (int i = 0; i < table.capacity(); i++) {
                                      if (table.getEntry(i) == null)
                                            continue;

                                      Container container = cache.read(8, i);
                                      Sprite sprite = Sprite.decode(container.getData());

                                      File dir = new File(Constants.SPRITE_PATH);

                                      if (!dir.exists()) {
                                            dir.mkdir();
                                      }

                                      for (int frame = 0; frame < sprite.size(); frame++) {
                                            File file = new File(Constants.SPRITE_PATH,
                                                        i + "_" + frame + ".png");
                                            BufferedImage image = sprite.getFrame(frame);

                                            ImageIO.write(image, "png", file);
                                            current++;
                                            updateProgress(current, count);
                                      }

                                }
                                updateMessage("Complete!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          } catch (FileNotFoundException e1) {
                                e1.printStackTrace();
                                updateMessage("failed!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          } catch (IOException e1) {
                                e1.printStackTrace();
                                updateMessage("failed!");
                                Platform.runLater(() -> {
                                    progressText.setFill(Color.GREEN);
                                });
                          }
                        return true;

                      }

                };

          }
          task.setOnSucceeded(e ->{
                progressBar.setVisible(false);
                progressI.setVisible(false);
                progressText.setText("");
          });
          task.setOnFailed(e ->{
                progressBar.setVisible(false);
                progressI.setVisible(false);
                progressText.setText("");
          });

          new Thread(task).start();
    } catch (Exception ex) {
          ex.printStackTrace();
    }