JList cell rendering very slow

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
 
A

ak

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.
many jpeg's have alredy thumbnail, with ImageroReader you can read them very
quickly.

if some jpeg don't have embedded thumbnail, then you have to read all image
and scale it, but do it in another thread.
the thumbnail view in windows explorer is way faster than this
and consumes less resources.
explorer makes first all thumbnails (slow) and stores them in database
(thumb.db)

you could change paintIcon method of your Thumbnail class to show some
message while thumbnail not ready:

public class Thumbnail implements Icon {

boolean imageLoaded = false;

public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D graphics2D = (Graphics2D)g;
int w = getIconWidth();
int h = getIconHeight();

if(!imageLoaded ) {
g.drawString("loading...", 0, h/ 2);
}
else {
graphics2D.drawImage(image, 0, 0, w, h, null);
}
}

____________

http://reader.imagero.com the best java image reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top