Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

JList cell rendering very slow

Thread view: 
Abs - 01 Jan 2004 16:49 GMT
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

--
Abs
ak - 01 Jan 2004 18:17 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.