Search code examples
javaperformanceopengltheorygraphics2d

Adjusting repaints in JComponent to improve performance


I am trying to do some tests on 2D "game"-programming by trying out different concepts to approach both designing and visualizing the environment.

In my baby-steps I went to go with Swing and using a JComponent's paintComponent() method to retrieve a Graphics2D object that I then use to visualize my game board.

It works quite well but I got to the point where I need to repeatedly check my whole game-model and update the view, basically once every 1/10 second something can change.

I paint the visuals by calling repaint() on my JComponent to cause a complete update of the view: I check every tile my game board has for information and paint that tile according to this data, for every tile on the board. But as I approach about 1000 - 4000 tiles that need to be painted, I come to the point where the painting of the whole view takes more than 100ms and thus a constant lag occurs when doing anything.

Now for the question(s): I am looking for a way, or opinions, on how to improve the performance of this approach. As not every tile on the board changes every "tick" I do not need to "repaint" this tile. But on the contrary, moving the visual area (camera offset) changes the position of every tile on the screen, so it would need to be repainted at a different position. Also, the later implementation of "would be" animations would need a constant update of the visual area, regardless of "happenings" or not. When looking at 3D games with high quality graphics (or even simple ones like minecraft) running at > 30 FPS, I am wondering weather or not I should immediately switch to OpenGL before running into even more problems graphics wise, or are there ways to improve the performance with algorithms checking for the right kind of changes both in the view and the model?


Solution

  • Declare a 2 dimensional array of boolean for each tile, and an int variable keeping constants of the change, updating them on every change, e.g.:

    public void updateTiles(){
    
        if(changeType == NO_CHANGE){
            //do something
        }
        else if(changeType == BOARD_MOVED){
            repaint();
        }
        else if(changeType == TILE_CHANGED) {
            for(int i = 0; i < changeList.length; i++) {
                for(int j = 0; j < changeList[i].length; j++) {
                    if(changeList[i][j]) {
                        repaint(i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE );
                        changeList[i][j] = false;
                    }
                }
            }
        }
    
        changeType == NO_CHANGE; 
    }