Re: Display image selected from JFileChooser
I never dreamed my post would provoke such a response! Thanks all for
helping me in my image display attempts.
ilAn - thanks for the tip about the error stack - its something I'll
look into.
Lew - thanks for pointing out two serious problems. In my println
statement I wrote that the image had been printed, but didn't really
believe that - it was more just a marker to see where execution had
reached, but I agree again very misleading.
Roedy Green - thanks for the link to your classes, I will investigate
these. I have been reading some other helpful pages on your website
that I found useful also, thanks. http://mindprod.com/jgloss/image.html
Knute Johnson - LOL. I know it must be disparaging to watch someone
fumble around when the answer is obvious to you, but this is the best
way for me to learn. Thanks for your solution, I will read and digest
your techniques.
John B. Matthews - thanks once again for your help. I find it
particularly useful that you have bullet pointed major failings and
modified my code (which is largely based on the code you wrote!), as I
am happy with it's style and flow. I will read and digest your
solution in detail.
Whilst there was all of the activity on this post I was busily
fumbling around with code in NetBeans. My ultimate intention is to
create a GUI which displays images for viewing. There will be many
widgets on the GUI so I decided to use NetBeans, for ease of layout.
Using a button push event and a class which extends JPanel, I have
been able to achieve my desired effect of displaying an image in a
JPanel in the GUI. However, not having seen this style of code before
(calling the paintComponent() method) in any of the many examples on
the net, I was wondering if there is a particular reason why it is not
used? Below is the relevant snippet from the code ((button push event
and DrawImage class).
Thanks once again, I'm going to read in detail the solutions that have
been posted in detail now.
Cheers,
Jimmy
BufferedImage image;
String filename;
DrawImage pic = null;
private void
openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
JFileChooser chooser = new JFileChooser(
new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(chooser.getSelectedFile());
System.out.println("The file selected is: " +
file.getPath());
//mainImagePanel is the target JPanel for the
image
Graphics g=mainImagePanel.getGraphics();
pic = new DrawImage();
pic.setImage(image);
System.out.println("got to here");
pic.paintComponent(g);
filename = file.getPath();
jLabel1.setText(filename);
} catch (IOException ex) {
System.out.println("There was a problem opening
the selected file: " + ex);
}
}
}
class DrawImage extends JPanel{
public DrawImage() {
}
public void setImage(BufferedImage image){
this.img = image;
}
BufferedImage img;
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
System.out.println("Now I'm down here");
if(img != null)
{
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);
}
}
}