This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Displaying witrh respect to path in an Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Displaying witrh respect to path in an Applet" Watch "Displaying witrh respect to path in an Applet" New topic
Author

Displaying witrh respect to path in an Applet

Manoj Paul
Ranch Hand

Joined: Jan 18, 2007
Posts: 109
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

I really need it for college project...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
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.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Displaying witrh respect to path in an Applet
 
Similar Threads
Image Resizing Woes
Displaying witrh respect to path..
Problem with the JScrollPane
Code to create Thumbnail in .jpg and .gif
Displaying witrh respect to path in an Applet