Updating the icon of every JLabel in a JLabel ArrayList Announcing the arrival of Valued...

How to compare two different files line by line in unix?

Dating a Former Employee

Can you use the Shield Master feat to shove someone before you make an attack by using a Readied action?

Most bit efficient text communication method?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

If a VARCHAR(MAX) column is included in an index, is the entire value always stored in the index page(s)?

How to find all the available tools in mac terminal?

Why are there no cargo aircraft with "flying wing" design?

Fundamental Solution of the Pell Equation

Using audio cues to encourage good posture

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Is the Standard Deduction better than Itemized when both are the same amount?

What is the meaning of the simile “quick as silk”?

Is this homebrew Lady of Pain warlock patron balanced?

How to Make a Beautiful Stacked 3D Plot

How to tell that you are a giant?

Using et al. for a last / senior author rather than for a first author

Wu formula for manifolds with boundary

If a contract sometimes uses the wrong name, is it still valid?

What does this Jacques Hadamard quote mean?

Can anything be seen from the center of the Boötes void? How dark would it be?

Why didn't Eitri join the fight?

First console to have temporary backward compatibility

Is grep documentation wrong?



Updating the icon of every JLabel in a JLabel ArrayList



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Updating every field of every page in a formLearning to write DSL utilities for unit tests and am worried about extensibilityCustom MultiIterator with type filterDoes RunLengthEncoding class provide abstraction and encapsulation?Querying Facebook for details of a user's OAuth tokenJLabel formatting on GPA calculatorUpdating positions of enemies every frameFollow-up 1: Compare 2 unordered, rooted trees for shape-isomorphismCounter GUI in Swing, updating the count in the backgroundSearching for combinations of words and numbers





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3












$begingroup$


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











$endgroup$




bumped to the homepage by Community 11 mins ago


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















  • $begingroup$
    Cross posted from stackoverflow.com/questions/49932952/…
    $endgroup$
    – Zeta
    Apr 21 '18 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
    Apr 21 '18 at 16:21


















3












$begingroup$


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











$endgroup$




bumped to the homepage by Community 11 mins ago


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















  • $begingroup$
    Cross posted from stackoverflow.com/questions/49932952/…
    $endgroup$
    – Zeta
    Apr 21 '18 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
    Apr 21 '18 at 16:21














3












3








3





$begingroup$


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











$endgroup$




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 '18 at 0:13









200_success

131k17157422




131k17157422










asked Apr 21 '18 at 1:16









heyhey

161




161





bumped to the homepage by Community 11 mins 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 11 mins ago


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














  • $begingroup$
    Cross posted from stackoverflow.com/questions/49932952/…
    $endgroup$
    – Zeta
    Apr 21 '18 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
    Apr 21 '18 at 16:21


















  • $begingroup$
    Cross posted from stackoverflow.com/questions/49932952/…
    $endgroup$
    – Zeta
    Apr 21 '18 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
    Apr 21 '18 at 16:21
















$begingroup$
Cross posted from stackoverflow.com/questions/49932952/…
$endgroup$
– Zeta
Apr 21 '18 at 7:02






$begingroup$
Cross posted from stackoverflow.com/questions/49932952/…
$endgroup$
– Zeta
Apr 21 '18 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
Apr 21 '18 at 16:21




$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
Apr 21 '18 at 16:21










1 Answer
1






active

oldest

votes


















0












$begingroup$

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









$endgroup$














    Your Answer






    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',
    autoActivateHeartbeat: false,
    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









    0












    $begingroup$

    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









    $endgroup$


















      0












      $begingroup$

      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









      $endgroup$
















        0












        0








        0





        $begingroup$

        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









        $endgroup$



        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 '18 at 20:46









        W.K.SW.K.S

        252217




        252217






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            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

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

            Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...