有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在更新JavaFX中的节点时性能不佳

我有一个stackPane,在其中一个窗格上,我需要表示一个javafx.scene.shape.Rectangle矩阵2D,其中不透明度每X毫秒使用带有DoubleProperty的绑定更新一次。问题是,即使尺寸非常小(10x15或20x30),也缺乏性能,帧速率非常低

我还为每个形状使用了setsetCache(true)setCacheHint(CacheHint.SPEED),但没有任何变化

以下是我代码的一部分: 单元类:

public class Cell{
 private Rectangle r;
 private DoubleProperty value;

 Cell(final int row, final int col, final int cellSize){
   r = new Rectangle(cellSize, cellSize, Color.BLACK);
   r.relocate(cellSize*col, cellSize*row);
   r.setCache(true);
   r.setCacheHint(CacheHint.SPEED);
   value = new SimpleDoubleProperty();
   r.opacityProperty().bind(value);
 }

 public void setValue(double value){ this.value.set(value);}

 public Node getNode(){return r;}
}

董事会级别:

public class Board{
  private Group group;
  private Cell[][] cells;

  Board(final int rows, final int cols, final int cellSize){
   group = new Group();
   group.setCache(true);
   group.setCacheHint(CacheHint.SPEED);
   cells = new Cell[rows][cols];
   for(int i = 0; i < rows; i++)
       for(int j = 0; j < cols; j++){
        cells[i][j] = new Cell(i,j,cellSize);
        group.getChildren().add(cells[i][j].getNode());
       }
  }

 public void setCellValueAt(int row, int col, double value){
     cells[row][col].setValue(value);
 }

 Random r = new Random();
 double t = 0;
 public void update(){
     for(int i = 0; i < cells.length; i++){
         for(int j = 0; j < cells[0].length; j++){
             cells[i][j].setValue(Math.abs(Math.sin((double)(j)/cells[0].length*2 + t)));
         }
     }
     t+=0.01;
 }

 public Node getNode(){return group;}
}

数组中的单元格值按以下方式更新:

@Override
public void start(Stage primaryStage) {
    final Board board = new Board(50, 50 , 15);
    Pane pane;
    try {
        StackPane root = (StackPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
        Scene scene = new Scene(root);
        pane = new Pane(board.getNode());
        pane.setPrefSize(50*15, 50*15);
        root.getChildren().add(pane);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }

    new Thread(()->{
        while(true){
            Platform.runLater(()->{board.update();});
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) { e.printStackTrace(); }
        }

    }).start();

}

性能非常差,即使是高X值(例如50/100)。。。在我的应用程序中,我必须以低刷新率(5/10/20)处理非常大的矩阵。 有什么我忘了的吗


共 (0) 个答案