Hello dudes
In my application for image display, those images are displayed which resides in the same folder as my program(code) is.... But now i want to display those images of folder respective to which an user click on a particular folder of local drives on a JTree... I am sending codes which i have written... Please take a view... and please make any necessary changes and reply with the changed codes.
************************************************************
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ThumbnailTest
{
Color bgColor;
public ThumbnailTest(SnapsGallery obj)
{
bgColor = UIManager.getColor("Panel.background");
BufferedImage[] images = loadImages();
BufferedImage[] tnails = createThumbnails(images);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weightx = 1.0;
for(int j = 0; j < images.length; j++)
{
gbc.gridwidth = gbc.RELATIVE;
panel.add(new JLabel(new ImageIcon(images[j])), gbc);
// obj.jPanel2.add(new JLabel(new ImageIcon(images[j])), gbc);
gbc.gridwidth = gbc.REMAINDER;
panel.add(new JLabel(new ImageIcon(tnails[j])), gbc);
obj.jPanel2.add(new JLabel(new ImageIcon(tnails[j])), gbc);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(panel)); // j2se 1.5
//f.getContentPane().add(new JScrollPane(panel)); // j2se 1.4
f.setSize(400,500);
f.setLocation(200,200);
// f.setVisible(true);
}
private BufferedImage[] createThumbnails(BufferedImage[] origs)
{
final int WIDTH = 100;
final int HEIGHT = 175;
BufferedImage[] images = new BufferedImage[origs.length];
AffineTransform at;
for(int j = 0; j < origs.length; j++)
{
images[j] = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = images[j].createGraphics();
g2.setPaint(bgColor);
g2.fillRect(0, 0, WIDTH, HEIGHT);
// scale to fit
double xScale = (double)WIDTH / origs[j].getWidth();
double yScale = (double)HEIGHT / origs[j].getHeight();
double scale = Math.min(xScale, yScale);
// center thumbnail image
double x = (WIDTH - origs[j].getWidth() * scale)/2;
double y = (HEIGHT - origs[j].getHeight() * scale)/2;
at = AffineTransform.getTranslateInstance(x, y);
at.scale(scale, scale);
g2.drawRenderedImage(origs[j], at);
g2.dispose();
}
return images;
}
private BufferedImage[] loadImages()
{
String prefix = "images\\";
String ext = ".jpg";
String[] fileNames = {
"2", "3", "5", "2"
};
BufferedImage[] images = new BufferedImage[fileNames.length];
for(int j = 0; j < fileNames.length; j++)
try
{
URL url = getClass().getResource(prefix + fileNames[j] + ext);
images[j] = ImageIO.read(url);
}
catch(MalformedURLException mue)
{
System.out.println("url: " + mue.getMessage());
}
catch(IOException ioe)
{
System.out.println("read: " + ioe.getMessage());
}
return images;
}
}
************************************************************
Here in the codes the file names are clearly given in the code and so those images are displayed... But now i want to display any image of the local drives according to user click on the JTree..
I am sure i need to change particularly this method...
private BufferedImage[] loadImages()
{
}
But i dont know what to change in this method... Please help
Waiting for your reply...
Thank you