Java-paintComponent从数组中绘制数百万个正方形的最有效方法

标签 java arrays performance swing

我根据一些例子编写了一个程序来模拟康威的生命游戏。 游戏中的每个细胞要么活着,要么死了,它们存储在一个整数数组 int[][] 中,该数组始终至少为 1000*1000,这意味着有数百万个细胞。

迭代数组以查找应用所有不同的规则很好,并且不是特别密集的 CPU。然而,在 JFrame 上绘制这些矩形的方法可能会导致我的 Xeon E3 1246 V3 (I7 4770) 上的 CPU 使用率达到 90% 以上。在数组大小为 2000^2 时,它可以导致 Windows 10 完全硬锁定。

@Override
    protected void paintComponent(Graphics g) {
        liveCells = 0;
        super.paintComponent(g);
        Color gColor = g.getColor();
        if(!backgroundWork || isPaused) {
            for (int row = 0; row < grid.length; row++) {
                for (int column = 0; column < grid[row].length; column++) {
                    //Checking if the cell is alive.
                    if (grid[row][column] == 1 ) {
                        liveCells++;
                        g.setColor(Color.red);
                        g.fillRect(column * UIScale, row * UIScale, UIScale, UIScale);

                    }
                }
            }
        }

        g.setColor(gColor);
        if (isPaused) { 
            g.drawString("The game is paused", 0, 30);
            g.drawString("Generation: " + generationCounter, 0, 10);
            g.drawString("Living cells: " + liveCells, 150, 10);
        } else { //If the game is running
            g.drawString("Generation: " + generationCounter++, 0, 10);
            g.drawString("Living cells: " + liveCells, 100, 10);
        }
        g.setColor(gColor);
        try {
            /* Sleep for some microseconds. */
            TimeUnit.MICROSECONDS.sleep(sleepTimer);
        } catch (InterruptedException ex) {
            System.err.println("An InterruptedException was caught: " + ex.getMessage());
        }
    }

我可以很清楚地看到单元格的绘制是问题所在,为了让程序运行得更快,我已经添加了一种更改变量backgroundWork的方法,这会禁用更新矩形网格。打开或关闭此功能会导致任务管理器 CPU 利用率差异高达 80%。

使用当前绘制网格的方法,我看不出有什么方法可以使其更快,因为所有单元格都是相互独立的,并且通常不会超过 3 个彼此相邻的红色单元格,因此没有理由实现同时绘制多个单元格的方法。

任何人都可以建议一种加快当前进程的方法,或者一种绘制正方形的不同方法。 感谢您的帮助。

最佳答案

这种方法是由 Hovercraft Full Of Eels(顺便说一句,名字不错)建议的,比我以前使用的方法要快得多 -

@Override
protected void paintComponent(Graphics g) {
    liveCells = 0;

    BufferedImage BI = new BufferedImage(UIDimensions, UIDimensions, BufferedImage.TYPE_INT_RGB);

    super.paintComponent(g);
    Color gColor = g.getColor();
    if(!backgroundWork || isPaused) {
        for (int row = 0; row < grid.length; row++) {
            for (int column = 0; column < grid[row].length; column++) {
                //Checking if the cell is alive.
                if (grid[row][column] == 1 ) {
                    liveCells++;
                    if(UIScale != 1) {
                        //TODO- Draw squares larger than one pixel
                    } else {
                        BI.setRGB(column, row, 16711680);
                    } 

                    //The old code is commented out below

                    //g.setColor(Color.red);
                    //Drawing the colour in a 4x4 pixel square. With a window of 1000x1000, there are 250x250 organisms, hence the /4 everywhere
                    //g.fillRect(column * UIScale, row * UIScale, UIScale, UIScale);
                    //The way that this works is that it draws rectangles at the coordinates of the grid.
                    //The graphics on the screen aren't a grid themselves, they are just lots of squares
                } else {
                    BI.setRGB(column, row, 16777215);
                }
            }
        }
        g.drawImage(BI, 0, 0, null);
    }

    g.setColor(gColor);

    if (isPaused) { //If the game is paused (isPaused is true)
        g.drawString("The game is paused", 0, 30);
        g.drawString("Generation: " + generationCounter, 0, 10);
        g.drawString("Living cells: " + liveCells, 150, 10);
    } else { //If the game is running
        g.drawString("Generation: " + generationCounter++, 0, 10);
        g.drawString("Living cells: " + liveCells, 100, 10);
    }
    g.setColor(gColor);
    try {
        /* Sleep for some seconds. */
        TimeUnit.MICROSECONDS.sleep(sleepTimer);
    } catch (InterruptedException ex) {
        System.err.println("An InterruptedException was caught: " + ex.getMessage());
    }
}

关于Java-paintComponent从数组中绘制数百万个正方形的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359033/

相关文章:

python - 我的 Python 中的二分搜索功能不起作用

javascript - 如何在不提及所有数字和中断标记的情况下水平显示数组?

performance - 非常奇怪-添加复合索引会使查询变慢(MongoDB)

java - 类层次结构对java性能有多大影响?

java - 从java导入django模块

java - 将 BufferedImage 保存为 TIFF 文件?

arrays - Rust 是否有办法将函数/方法应用于数组或向量中的每个元素?

javascript - 是否值得收集 jQuery 相关的脚本在文档末尾运行它们?

java - 始终打开(创建)Eclipse RCP View

java - forEach jSTL 中的内部 forEach 错误