• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying witrh respect to path in an Applet

Manoj Paul
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.add(new JScrollPane(panel)); // j2se 1.5
//f.getContentPane().add(new JScrollPane(panel)); // j2se 1.4
// 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,
Graphics2D g2 = images[j].createGraphics();
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);
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++)
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

I really need it for college project...
Ulf Dittmer
Posts: 42970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to start by adding a JTree object to your GUI that reflects the local file system. But people here are not just going to write the code for you. Tells us where you are stuck implementing that, and we'll help you get going again.

Moving to the AWT/Swing forum, as there seems to be nothing applet-specific about the question.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!