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

时间:2015-07-11 16:01:55

标签: 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个红色旁边的单元格无论如何,所以没有理由实现同时绘制多个的方法。

任何人都可以提出加速当前流程的方法,或者绘制方块的不同方法。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这种方法,由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());
    }
}