Java- paintComponent most efficient way of drawing millions of squares from an array -


i have written program, based off of examples, simulate conway's game of life. each cell in game either alive or dead , stored in array of integers int[][] array @ least 1000*1000, meaning there millions of cells.

iterating through array find apply of different rules fine, , not particularly cpu intensive. however, method used draw these rectangles on jframe can lead 90+% cpu usage on xeon e3 1246 v3 (i7 4770). on array sizes of 2000^2, can lead windows 10 hard locking completely.

@override     protected void paintcomponent(graphics g) {         livecells = 0;         super.paintcomponent(g);         color gcolor = g.getcolor();         if(!backgroundwork || ispaused) {             (int row = 0; row < grid.length; row++) {                 (int column = 0; column < grid[row].length; column++) {                     //checking if cell 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 paused", 0, 30);             g.drawstring("generation: " + generationcounter, 0, 10);             g.drawstring("living cells: " + livecells, 150, 10);         } else { //if game running             g.drawstring("generation: " + generationcounter++, 0, 10);             g.drawstring("living cells: " + livecells, 100, 10);         }         g.setcolor(gcolor);         try {             /* sleep microseconds. */             timeunit.microseconds.sleep(sleeptimer);         } catch (interruptedexception ex) {             system.err.println("an interruptedexception caught: " + ex.getmessage());         }     } 

i can see quite drawing of cells problem, in order allow program run faster, had added way change variable backgroundwork, disables updating grid of rectangles. turning on or off results in differences of 80% in task manager cpu utilisation.

with current method of drawing grid, don't see way make faster, cells independent of each other, , there won't more 3 red next each other anyway, there no reason implement way draw multiple @ same time.

can suggest way speed current process, or different way of drawing squares. help.

this method, suggested hovercraft full of eels (nice name way) faster using previously-

@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) {         (int row = 0; row < grid.length; row++) {             (int column = 0; column < grid[row].length; column++) {                 //checking if cell alive.                 if (grid[row][column] == 1 ) {                     livecells++;                     if(uiscale != 1) {                         //todo- draw squares larger 1 pixel                     } else {                         bi.setrgb(column, row, 16711680);                     }                       //the old code commented out below                      //g.setcolor(color.red);                     //drawing colour in 4x4 pixel square. window of 1000x1000, there 250x250 organisms, hence /4 everywhere                     //g.fillrect(column * uiscale, row * uiscale, uiscale, uiscale);                     //the way works draws rectangles @ coordinates of grid.                     //the graphics on screen aren't grid themselves, lots of squares                 } else {                     bi.setrgb(column, row, 16777215);                 }             }         }         g.drawimage(bi, 0, 0, null);     }      g.setcolor(gcolor);      if (ispaused) { //if game paused (ispaused true)         g.drawstring("the game paused", 0, 30);         g.drawstring("generation: " + generationcounter, 0, 10);         g.drawstring("living cells: " + livecells, 150, 10);     } else { //if game running         g.drawstring("generation: " + generationcounter++, 0, 10);         g.drawstring("living cells: " + livecells, 100, 10);     }     g.setcolor(gcolor);     try {         /* sleep seconds. */         timeunit.microseconds.sleep(sleeptimer);     } catch (interruptedexception ex) {         system.err.println("an interruptedexception caught: " + ex.getmessage());     } } 

Comments