I want to update every icon of a JLabel ArrayList, to its own individual icon based off of an outside ArrayList. The outside ArrayList is called _board_, and the JLabel ArrayList is called _tiles_. I want to update every icon to its corresponding item in _board_. How can I do that efficiently? I need it to update every icon of a huge list, we're talking like 100 - 200 items long. Right now, at a hundred items long, it takes about 4 seconds before displaying all of the icons at the same time:

for (int i = 0; i < board.size(); i++) {
    //this is all one line:
    tiles.get(i).getHeight(), tiles.get(i).getHeight()));
//this resizes the ImageIcon and returns the risized icon:
public static ImageIcon resizeIcon(ImageIcon i, int x, int y) {
    Image image = i.getImage();
    Image newimg = image.getScaledInstance(x, y, java.awt.Image.SCALE_SMOOTH);  
    i = new ImageIcon(newimg);
    return i;
//this returns an ImageIcon based off of the number that is put in:
public static ImageIcon displayTile(int num) {
    if (num != 0) {
        ImageIcon i = new ImageIcon("src/resources/" + num + ".png");
        return i;
    ImageIcon i = new ImageIcon("src/resources/0.png");
    return i;
  • \$\begingroup\$ Cross posted from stackoverflow.com/questions/49932952/… \$\endgroup\$
    – Zeta
    Commented Apr 21, 2018 at 7:02
  • \$\begingroup\$ There is one closing parenthesis missing in the body of the for loop. It should probably be after board.get(i). I presume board is an ArrayList<Integer>? \$\endgroup\$
    – Stingy
    Commented Apr 21, 2018 at 16:21

1 Answer 1


We will start by cleaning up the code:

First, let's make the for loop cleaner to read. A compiler is able to inline variables that are only used once in a scope.

for (int i = 0; i < board.size(); i++) {

    final ImageIcon tileToDisplay = displayTile(i); 

    final int height = tiles.get(i).getHeight();
    final ImageIcon resizedTileToDisplay = resizeIcon(tileToDisplay,height,height);

Second, in displayTile(int num), StringBuilder is more efficient when concatenating strings. Also, if(num != 0) is redundant because you apply the same logic in both cases.

public static ImageIcon displayTile(int num) {

    final StringBuilder pathBuilder = new StringBuilder("src/resources/");

    return new ImageIcon(pathBuilder.toString());

Lastly, resizeIcon changes the parameter i; methods should not change the parameters that they receive:

public static ImageIcon resizeIcon(ImageIcon icon, int x, int y) {

    Image resizedImage = icon.getImage().getScaledInstance(x,y,java.awt.Image.SCALE_SMOOTH));
    return new ImageIcon(resizedImage);

Now, let's deal with performance:

You are loading 100 - 200 images from a file, resizing each one and then placing it on a screen. This is a resource intensive task and you need to optimise it. Here are some suggestions

  1. Use a ThreadPool to load the images asynchronously on start up. Cache them in memory and use them as needed.

  2. Only load images as needed. For example, the board may have 200 tiles, but only 10 are visible at a given time. So load the first ten tiles. Anticipate which tiles will be needed and load them as necessary

  3. If resizing is a one time operation, perhaps saved the resized images to disk so that you won't have to resize them after the first time.

  4. BufferedImage offers getSubimage. You could have one big image with all 100-200 tiles. Load this one big image once, and then show only the relevant part of the image using getSubimage. This is a concept known as Sprites and you can find a clearer example here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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