I am about to start a Tetris clone using Java as my programming language. While I was designing how it'd be done, I've stumbled upon this question:

What would be better, if any difference at all, regarding performance 1, to draw my game?

For a matter of simplicity, I will use a background grid as an example. The following code will be running in the graphics update loop:

1) To perform the drawing using my graphics g variable, such as this

for(int x = 0; x < COL_COUNT; x++) {                                               
    for(int y = 0; y < VISIBLE_ROW_COUNT; y++) {                                   
        g.drawLine(0, y * TILE_SIZE, COL_COUNT * TILE_SIZE, y * TILE_SIZE);        
        g.drawLine(x * TILE_SIZE, 0, x * TILE_SIZE, VISIBLE_ROW_COUNT * TILE_SIZE);


2) pre-load an image like this:

BufferedImage img = null;
try {
    img = ImageIO.read(new File("grid.jpg"));
} catch (IOException e) {

and then use my graphics to draw it using Graphics drawImage:

g.drawImage(img, x, y, null)

1 - I know that a Tetris game isn't that performance-hungry, but I believe that this doesn't invalidate the question.

  • Just to be clear: if you feel like this question doesn't belong on programmers, feel free to tell me and I'll work on it or move to another site where it fits better – user2018675 Jun 18 '14 at 0:55
  • Drawing graphics will require better gpu while importing images more ram on the graphics card. it's up to you. – user136955 Jun 18 '14 at 2:20
  • 1 - I know that a Tetris game isn't that performance-hungry, but I believe that this doesn't invalidate the question. I would said just the opposite. Programs which do heavy graphic processing are unlikely to need to output exactly the same image again and again (unless we are back to using sprites). So, the anwer will be not important for your tetris clone, and not relevant for more advanced graphics programming. – SJuan76 Jun 18 '14 at 7:32
  • gamedev.stackexchange.com is a nice place for question like this. – Simon Bergot Jun 18 '14 at 8:47

Consider drawing it in code, but only draw it once and cache it.

When you procedurally draw the same, static graphic every frame, you shouldn't generate it again and again using the same for-loops. Draw it once to a buffered image, and then paste that image each frame.

That way the decision between procedurally creating a graphic and loading it from a file just becomes a question of development-time (what's faster - drawing it in a graphic editor or writing the code to draw it?), loading-time (what's faster - loading and decoding a graphics file or running the code to draw the image?). The answers to these two question vary greatly on the nature of the image and your skills as a graphic artist or programmer.

A third aspect is filesize. In most cases, the executable code required to generate an image will be smaller than the encoded image file, so you can often reduce the total size of the application by generating assests procedurally. Nowadays application size does not matter much for a desktop application. But there are at least two cases where every kB counts: Smartphone apps (users will be reluctant to download a large app when they have a limited data plan) and web applications (every second of additional loading time causes more users to lose patience and close the tab).


You already have both alternatives, so what is stopping you from running 1000 times each one of them and checking the average times for each? Then, after that, it is just a matter of picking up the fastest one.

Once you are done with your tests, update your question and let us know.

  • 1
    I will proceed with the development and update the info :-) thanks for the tip. Surprisingly, I went searching on oracle docs and the web before thinking of actually testing it, which sounds pretty reasonable. – user2018675 Jun 18 '14 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.