Updating the icon of every JLabel in a JLabel ArrayList











up vote
3
down vote

favorite












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).setIcon(resizeIcon(displayTile(board.get(i),
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;
}









share|improve this question
















bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Cross posted from stackoverflow.com/questions/49932952/…
    – Zeta
    Apr 21 at 7:02












  • 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>?
    – Stingy
    Apr 21 at 16:21















up vote
3
down vote

favorite












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).setIcon(resizeIcon(displayTile(board.get(i),
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;
}









share|improve this question
















bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Cross posted from stackoverflow.com/questions/49932952/…
    – Zeta
    Apr 21 at 7:02












  • 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>?
    – Stingy
    Apr 21 at 16:21













up vote
3
down vote

favorite









up vote
3
down vote

favorite











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).setIcon(resizeIcon(displayTile(board.get(i),
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;
}









share|improve this question















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).setIcon(resizeIcon(displayTile(board.get(i),
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;
}






java performance swing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 21 at 0:13









200_success

127k15148410




127k15148410










asked Apr 21 at 1:16









hey

161




161





bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Cross posted from stackoverflow.com/questions/49932952/…
    – Zeta
    Apr 21 at 7:02












  • 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>?
    – Stingy
    Apr 21 at 16:21


















  • Cross posted from stackoverflow.com/questions/49932952/…
    – Zeta
    Apr 21 at 7:02












  • 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>?
    – Stingy
    Apr 21 at 16:21
















Cross posted from stackoverflow.com/questions/49932952/…
– Zeta
Apr 21 at 7:02






Cross posted from stackoverflow.com/questions/49932952/…
– Zeta
Apr 21 at 7:02














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>?
– Stingy
Apr 21 at 16:21




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>?
– Stingy
Apr 21 at 16:21










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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/");
pathBuilder.append(num);
pathBuilder.append(".png");

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.







share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192602%2fupdating-the-icon-of-every-jlabel-in-a-jlabel-arraylist%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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/");
    pathBuilder.append(num);
    pathBuilder.append(".png");

    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.







    share|improve this answer

























      up vote
      0
      down vote













      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/");
      pathBuilder.append(num);
      pathBuilder.append(".png");

      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.







      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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/");
        pathBuilder.append(num);
        pathBuilder.append(".png");

        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.







        share|improve this answer












        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/");
        pathBuilder.append(num);
        pathBuilder.append(".png");

        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.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 22 at 20:46









        W.K.S

        252217




        252217






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192602%2fupdating-the-icon-of-every-jlabel-in-a-jlabel-arraylist%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'