A
Abs
Hi!
I'm building a component with a JList that lists the JPG files
in a directory as thumbnails. The problem is that I have to
resize them and this is very slow, cpu and memory consuming. This is
the code for the listcellrenderer I'm using:
public class ThumbnaiListCellRenderer extends JLabel implements
ListCellRenderer {
private Hashtable thumbnails;
public ThumbnaiListCellRenderer() {
setOpaque(true);
setVerticalTextPosition(JLabel.BOTTOM);
setHorizontalTextPosition(JLabel.CENTER);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
thumbnails = new Hashtable();
}
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
if (value instanceof File) {
File file = (File)value;
setText(file.getName());
Thumbnail thumbnail = (Thumbnail)thumbnails.get(value);
if (thumbnail == null) {
thumbnail = new Thumbnail(file);
thumbnails.put(value,thumbnail);
}
setIcon(thumbnail);
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
}
----
The code for the Thumbnail class:
----
public class Thumbnail implements Icon {
private Image image = null;
private int width = 0;
private int height = 0;
public Thumbnail(File file) {
URL imageURL = null;
try {
if (file.isDirectory()) {
File folder = new File("folder.jpg");
imageURL = folder.toURL();
} else {
imageURL = file.toURL();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (imageURL == null) {
System.err.println("Resource not found: " + file);
} else {
Image source = Toolkit.getDefaultToolkit().getImage(imageURL);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(source, 0);
try {
mediaTracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = 100;
int thumbHeight = 100;
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = source.getWidth(null);
int imageHeight = source.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
Image target = source.getScaledInstance(thumbWidth, thumbHeight,
Image.SCALE_FAST);
this.image = target;
this.width = thumbWidth;
this.height = thumbHeight;
source.flush();
}
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D graphics2D = (Graphics2D)g;
graphics2D.drawImage(image, 0, 0, getIconWidth(),
getIconHeight(), null);
}
---
Is there any way to improve the performance of this code ? I mean,
the thumbnail view in windows explorer is way faster than this
and consumes less resources.
Regards
I'm building a component with a JList that lists the JPG files
in a directory as thumbnails. The problem is that I have to
resize them and this is very slow, cpu and memory consuming. This is
the code for the listcellrenderer I'm using:
public class ThumbnaiListCellRenderer extends JLabel implements
ListCellRenderer {
private Hashtable thumbnails;
public ThumbnaiListCellRenderer() {
setOpaque(true);
setVerticalTextPosition(JLabel.BOTTOM);
setHorizontalTextPosition(JLabel.CENTER);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
thumbnails = new Hashtable();
}
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
if (value instanceof File) {
File file = (File)value;
setText(file.getName());
Thumbnail thumbnail = (Thumbnail)thumbnails.get(value);
if (thumbnail == null) {
thumbnail = new Thumbnail(file);
thumbnails.put(value,thumbnail);
}
setIcon(thumbnail);
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
}
----
The code for the Thumbnail class:
----
public class Thumbnail implements Icon {
private Image image = null;
private int width = 0;
private int height = 0;
public Thumbnail(File file) {
URL imageURL = null;
try {
if (file.isDirectory()) {
File folder = new File("folder.jpg");
imageURL = folder.toURL();
} else {
imageURL = file.toURL();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (imageURL == null) {
System.err.println("Resource not found: " + file);
} else {
Image source = Toolkit.getDefaultToolkit().getImage(imageURL);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(source, 0);
try {
mediaTracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = 100;
int thumbHeight = 100;
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = source.getWidth(null);
int imageHeight = source.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
Image target = source.getScaledInstance(thumbWidth, thumbHeight,
Image.SCALE_FAST);
this.image = target;
this.width = thumbWidth;
this.height = thumbHeight;
source.flush();
}
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D graphics2D = (Graphics2D)g;
graphics2D.drawImage(image, 0, 0, getIconWidth(),
getIconHeight(), null);
}
---
Is there any way to improve the performance of this code ? I mean,
the thumbnail view in windows explorer is way faster than this
and consumes less resources.
Regards