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;
}
java performance swing
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.
add a comment |
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;
}
java performance swing
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 thefor
loop. It should probably be afterboard.get(i)
. I presumeboard
is anArrayList<Integer>
?
– Stingy
Apr 21 at 16:21
add a comment |
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;
}
java performance swing
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
java performance swing
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 thefor
loop. It should probably be afterboard.get(i)
. I presumeboard
is anArrayList<Integer>
?
– Stingy
Apr 21 at 16:21
add a comment |
Cross posted from stackoverflow.com/questions/49932952/…
– Zeta
Apr 21 at 7:02
There is one closing parenthesis missing in the body of thefor
loop. It should probably be afterboard.get(i)
. I presumeboard
is anArrayList<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
add a comment |
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
Use a
ThreadPool
to load the images asynchronously on start up. Cache them in memory and use them as needed.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
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.
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 usinggetSubimage
. This is a concept known as Sprites and you can find a clearer example here.
add a comment |
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
Use a
ThreadPool
to load the images asynchronously on start up. Cache them in memory and use them as needed.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
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.
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 usinggetSubimage
. This is a concept known as Sprites and you can find a clearer example here.
add a comment |
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
Use a
ThreadPool
to load the images asynchronously on start up. Cache them in memory and use them as needed.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
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.
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 usinggetSubimage
. This is a concept known as Sprites and you can find a clearer example here.
add a comment |
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
Use a
ThreadPool
to load the images asynchronously on start up. Cache them in memory and use them as needed.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
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.
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 usinggetSubimage
. This is a concept known as Sprites and you can find a clearer example here.
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
Use a
ThreadPool
to load the images asynchronously on start up. Cache them in memory and use them as needed.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
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.
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 usinggetSubimage
. This is a concept known as Sprites and you can find a clearer example here.
answered Apr 22 at 20:46
W.K.S
252217
252217
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 afterboard.get(i)
. I presumeboard
is anArrayList<Integer>
?– Stingy
Apr 21 at 16:21