Friday, June 27, 2008

The Thumbnail drama

As i thought of making an image viewer with some nice features, i always wanted to have a decent looking, visually attractive Graphical interface in my software.
So I wanted to have thumbnails of the images shown in my program, however it was really all not that easy.
I was of the view that Java must be having a nice way to show thumbnails of the images,
but i was unable to find it..
Then i also heard about third party library support to ease out this tension..but i was never sure to use it in my program..
So i decided to code from scratch for this software..
Yes, what i knew was that there is this very general function drawImage of the Graphics object that can be used in a variety of ways to get what you want.
Infact i have no words to praise this function..this function can be used in various ways eliminating use of various other specific functions.
If someone wants to have this feature of Thumbnails in Java Software coded purely in J2SE then
there are only two options:-
Create Thumbnails- using On the Fly Scaling Approach using drawImage and display it.
or Create Thumbnails using getScaledInstance of Image object, save them and then show them.
What i prefer depends on Two things:
Firstly, What your Application intends to do, and secondly, getScaledInstance is not your friend as its pretty slow as compared to drawImage.
So what needs to be done? Confused i guess!!
The answer is that drawImage is at your rescue all the time.
Whenever you need to show scaled down versions of your images, always use drawImage function.
Then if your software is such that it has to display those scaled versions repeatedly, its better to save them on disk and then have the software read from disk when you need to display that scaled version again.
This approach is faster and much robust as well.
And yes how to use drawImage to create a scaled version?
The code for that will be:-
public void paintComponent(Graphics g)
{ g.drawImage(img, 0, 0, img.getWidth()*scale, img.getHeight()*scale, null);
Here scale is your scaling factor.
In case you want to create an instance of your thumb, the code for that will look like----
public BufferedImage createThumb(BufferedImage img)
thumb= new BufferedImage(133,100,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2=thumb.createGraphics();
return thumb;
Here 133 is width and 100 is height.
Now you can directly show the thumb using drawImage of the component in which you are drawing, inside the paint(Graphics g) function by overriding it, or you can save it as well.
Alos Some people use AffineTransform for thumb creation, but my advise is: Always use drawImage, its the best as its faster , robust when you code using it, and secondly avoids creation of unnecessary Garbage objects in memory
Hope what i have written helps alot to many people as these are things which can be overlooked but are a matter of concern if you want your code to work efficiently..

1 comment:

  1. I have used drawImage method and i have got gray image as output.


Write Your Comments..

Share this Page:-