Display Thumbnail in jPanel?

B

Barkster

Is there a simplier way to display a thumbnail of an image from a given
path to the file on the hard drive? I've got it working using a bunch
of code and drawing the graphics but I thought there was an easier way
to do it using createthumbnail? I'm trying to build simple app that
has a file dialog and select a file and a thumbnail is show in a
jpanel.

Thanks
 
O

Oliver Wong

Barkster said:
Is there a simplier way to display a thumbnail of an image from a given
path to the file on the hard drive? I've got it working using a bunch
of code and drawing the graphics but I thought there was an easier way
to do it using createthumbnail? I'm trying to build simple app that
has a file dialog and select a file and a thumbnail is show in a
jpanel.

See http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

- Oliver
 
B

Barkster

Thanks, I've seen that but wasn't sure how to size the files. I'll
take another look
 
O

Oliver Wong

B

Barkster

I'll take a look at that then, thanks

Oliver said:
[post re-ordered]

Barkster said:
Thanks, I've seen that but wasn't sure how to size the files. I'll
take another look

Actually, you might not be able to directly resize the image via that
class. I wasn't aware that that was the main problem. You might want to look
at these classes:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/AffineTransformOp.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.html

- Oliver
 
B

Barkster

Any ideas as to why this doesn't work for me? I can select a file but
it doesn't display anything.

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

Public class ImageViewer extends JFrame implements ActionListener {
public ImageViewer() {
setTitle("ImageViewer");
setSize(400, 600);

JMenuBar mbar = new JMenuBar();
JMenu m = new JMenu("File");
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
m.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
m.add(exitItem);
mbar.add(m);
setJMenuBar(mbar);

label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label, "Center");
label.setText("What the hell");
}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".gif")
|| f.isDirectory();
}

public String getDescription() {
return "GIF Images";
}
});

int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getName();
label.setIcon(new ImageIcon(name));
}
} else if (source == exitItem)
System.exit(0);
}

public static void main(String[] args) {
JFrame frame = new ImageViewer();
frame.show();
}

private JLabel label;

private JMenuItem openItem;

private JMenuItem exitItem;
}


Oliver said:
[post re-ordered]

Barkster said:
Thanks, I've seen that but wasn't sure how to size the files. I'll
take another look

Actually, you might not be able to directly resize the image via that
class. I wasn't aware that that was the main problem. You might want to look
at these classes:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/AffineTransformOp.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.html

- Oliver
 
O

Oliver Wong

Barkster said:
Any ideas as to why this doesn't work for me? I can select a file but
it doesn't display anything.

[most of the code snipped]
JFileChooser chooser = new JFileChooser(); [...]

int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getName();
label.setIcon(new ImageIcon(name));
}

File.getName() only returns the filename. use getAbsolutePath() instead.

- Oliver
 
B

Barkster

Thanks that was it. Appreciate the help, now hopefully with the
combination of these functions I can put something together.

How do you know which classes you need to do a particular function?
Say for instance manipulate an image? Will java tell you when you
compile that it needs a library? I'm very new to java as you can tell

Thanks


Oliver said:
Barkster said:
Any ideas as to why this doesn't work for me? I can select a file but
it doesn't display anything.

[most of the code snipped]
JFileChooser chooser = new JFileChooser(); [...]

int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getName();
label.setIcon(new ImageIcon(name));
}

File.getName() only returns the filename. use getAbsolutePath() instead.

- Oliver
 
O

Oliver Wong

Barkster said:
How do you know which classes you need to do a particular function?

Experience and guesswork. If you go to
http://java.sun.com/j2se/1.5.0/docs/api/ you'll see 3 frames. The bottom
left one contains a list of all classes in the standard Java class library.
You can browse through that and hope that their names accurately reflect
what they do.
Say for instance manipulate an image?

I'd look for classes that have the word "image" or "graphic" in them.
Will java tell you when you
compile that it needs a library?

There's a bit of a terminology problem here. I'll try to clarify the
problem with an analogy: In the "real-non-Java-world", a library is a place
with a bunch of books. When you're doing a research project, it's not that
you borrow the library. Rather, you borrow books from the library.

So in Java, instead of "borrowing books", you "import classes" (you can
also import interfaces). That's what the import statements at the top of
most java source files are for: they tell the compiler which classes you're
going to need for this program.

If you try to use a class without importing it, the compiler will
complain.[*]

- Oliver

[*] There are two exceptions to this rule:
(1) Some classes, like Object, String, Thread, Math, etc. are imported
automatically. You don't need to import these.
(2) You can avoid importing classes by giving their full names. Instead of
importing ArrayList, you can use the full name "java.util.ArrayList".
 
B

Barkster

Cool, thanks for that great explaination. That is helpful

Oliver said:
Barkster said:
How do you know which classes you need to do a particular function?

Experience and guesswork. If you go to
http://java.sun.com/j2se/1.5.0/docs/api/ you'll see 3 frames. The bottom
left one contains a list of all classes in the standard Java class library.
You can browse through that and hope that their names accurately reflect
what they do.
Say for instance manipulate an image?

I'd look for classes that have the word "image" or "graphic" in them.
Will java tell you when you
compile that it needs a library?

There's a bit of a terminology problem here. I'll try to clarify the
problem with an analogy: In the "real-non-Java-world", a library is a place
with a bunch of books. When you're doing a research project, it's not that
you borrow the library. Rather, you borrow books from the library.

So in Java, instead of "borrowing books", you "import classes" (you can
also import interfaces). That's what the import statements at the top of
most java source files are for: they tell the compiler which classes you're
going to need for this program.

If you try to use a class without importing it, the compiler will
complain.[*]

- Oliver

[*] There are two exceptions to this rule:
(1) Some classes, like Object, String, Thread, Math, etc. are imported
automatically. You don't need to import these.
(2) You can avoid importing classes by giving their full names. Instead of
importing ArrayList, you can use the full name "java.util.ArrayList".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top