Picture viewer
Image, JPEG, GIF, PNG
This week we will make a small picture viewer application with a slide show feature.
If you think this sounds advanced, do not worry. Most of framework to load and view pictures are already
implemented in the Java API. All you have to do, is to pick the pictures you want to view.
This project has evolved into a full-fledged image viewer with tagging capabilities. For more,
please visit the Java Image Viewer page.
|
First we need to read and show at least on image. Make an application that takes the name of an
image file as a command line argument and shows that images within a JFrame. Do not think about the size
of the JFrame yet. Set the size manually in your code so that the frame is big enough to display the image.
Note that
java.awt.Toolkit supports the GIF, JPEG and PNG image formats.
Hint:
java.awt.Toolkit.createImage and java.awt.Graphics.drawImage
ImageViewer.java
|
|
Now we will try to scale the image. In a slide show double size or a "fill the screen" size would be interesting.
Extend the application above to scale your image to fit the JFrame to take up as much space as possible within the frame,
while preserving the aspect ratio.
Hint: For scaling there are different options, you could use another drawImage method of Graphics to scale on the fly,
or you could pre-scale the image using an image filter of the java.awt.image package and the
java.awt.image.FilteredImageSource class.
ImageViewer.java
|
|
Next, we go on to implement the actual slide show. For this we need a list of images to display. At first we will
search the current directory for image files and then show one at a time. The easiest will probably be to make an
automated slide show which loops through the images with a pre-set delay in between.
My solution requires a file filter called com.rememberjava.io.SunFileFilter. Make the package directory and
download the file SunFileFilter.java by clicking the package icon to the left.
SlideShow.java
com.rememberjava.io.SunFileFilter.java
|
|
Now we will investigate the possibilities of full-screen views. The class
java.awt.GraphicsDevice contains some methods for this purpose.
Extend your slide show application from above to view the images in full-screen mode, scaled or
original size.
My solution requires a file filter called com.rememberjava.io.SunFileFilter. Make the package directory and
download the file SunFileFilter.java by clicking the package icon to the left.
SlideShow.java
com.rememberjava.io.SunFileFilter.java
|
|
Finally, this week, finish the slide show application and add your favorite features. This could include
using keyboard and/or mouse keys to navigate the image queue, a randomised slide show, pre-buffering of
images in the slide show, retrieving images information (size, bit-depth, file name, etc.) or doing
file operations like deleting and moving images.
|
|
This project has evolved into a full-fledged image viewer with tagging capabilities. For more,
please visit the Java Image Viewer page.
|
|
|