Display jpg in JPanel problem

Discussion in 'Java' started by jimmy, Jun 23, 2010.

  1. jimmy

    jimmy Guest

    Hi All,

    I am developing a GUI to display jpg images in a JPanel. I have
    created two ways for the images to be selected: (i) a JFileChooser
    button to select a single jpg, and (ii) a JFileChooser to select a
    directory containing jpg files. Once the images have been selected,
    they should be displayed on the JPanel.

    In the case of opening the single file, this works as I hoped. However
    when the open directory scenario is used, nothing is displayed.
    Through the use of println statements I have been able to establish
    the programme is executing the same Classes and Methods as in the
    single image display. In addition to displaying an image, I have a
    JLabel in which I display the name of the current image. Again for the
    single image file selection, this works. For the open directory
    scenario, only the name of the final image in the directory is
    displayed (without the image).

    Thinking that maybe there was not sufficient time for the image to be
    displayed, I added a Thread.sleep(4000); line, but this did not help.

    Included below are the two actionPerformed methods from each button,
    plus the DrawImage and MyFilter Classes. I would appreciate if anyone
    could indicate possible causes for the failure of the open directory
    display method.

    Many thanks,

    Jimmy



    BufferedImage image;
    String filename;
    DrawImage pic = null;

    DrawImage pic2 = null;
    String filename2;
    BufferedImage image2 = null;

    private void
    openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(chooser);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    try {
    image = ImageIO.read(file);
    }
    catch (IOException ex) {
    System.out.println("There was a problem opening
    the selected file: " + ex);
    }

    //mainImagePanel is the target JPanel for the image
    Graphics g=mainImagePanel.getGraphics();

    pic = new DrawImage();
    pic.setImage(image);
    pic.paintComponent(g);

    //Add label text
    filename = file.getPath();
    jLabel1.setText(filename);
    }
    }

    private void
    openBatchButtonActionPerformed(java.awt.event.ActionEvent evt)
    {

    //JFileChooser to select a batch directory
    JFileChooser chooserBatch = new JFileChooser();

    chooserBatch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnValBatch =
    chooserBatch.showOpenDialog(chooserBatch);
    File fileBatch = chooserBatch.getSelectedFile();

    if (returnValBatch == JFileChooser.APPROVE_OPTION) {
    System.out.println("Batch directory chosen: " +
    fileBatch);
    } else {
    System.out.println("Error selecting batch directory: ");
    }

    //Get a file array of all jpgs in this directory
    FilenameFilter only = new MyFilter("jpg");
    File[] fileArray = fileBatch.listFiles(only);

    //loop through jpg files
    for (int i=0; i < fileArray.length; i++){

    try {
    image2 = ImageIO.read(fileArray);
    } catch (IOException ex) {

    Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE,
    null, ex);
    }
    System.out.println("current file: " + fileArray);

    //load image to main image panel
    Graphics gg = mainImagePanel.getGraphics();
    pic2 = new DrawImage();
    pic2.setImage(image2);
    pic2.paintComponent(gg);

    //add filename to label
    filename2 = fileArray.getPath();
    jLabel1.setText(filename2);

    }
    }
    /*****************************/
    /*** Draw Main Image Panel ***/
    /*****************************/

    class DrawImage extends JPanel{

    BufferedImage img;

    public DrawImage() {
    }

    public void setImage(BufferedImage image){
    this.img = image;
    repaint();
    }


    @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    if(img != null) {
    g2d.scale(0.2, 0.2);
    g2d.drawImage(image, 0, 0, null);
    }
    else {
    System.out.println("Image passed for drawing to main
    panel is null");
    }
    }
    }

    /***********************/
    /*** Filename Filter ***/
    /***********************/

    public class MyFilter implements FilenameFilter {

    String ext;
    public MyFilter(String ext) {
    this.ext = "." + ext;
    }
    public boolean accept(File dir, String name) {
    return name.endsWith(ext);
    }
    }
     
    jimmy, Jun 23, 2010
    #1
    1. Advertising

  2. In article
    <>,
    jimmy <> wrote:

    > I am developing a GUI to display jpg images in a JPanel. I have
    > created two ways for the images to be selected: (i) a JFileChooser
    > button to select a single jpg, and (ii) a JFileChooser to select a
    > directory containing jpg files. Once the images have been selected,
    > they should be displayed on the JPanel.


    Displayed how?

    > In the case of opening the single file, this works as I hoped.
    > However when the open directory scenario is used, nothing is
    > displayed. Through the use of println statements I have been able to
    > establish the programme is executing the same Classes and Methods as
    > in the single image display. In addition to displaying an image, I
    > have a JLabel in which I display the name of the current image. Again
    > for the single image file selection, this works. For the open
    > directory scenario, only the name of the final image in the directory
    > is displayed (without the image).


    I suspect this has more to do with your (unseen) layout code. You might
    try developing an <http://sscce.org/> that reproduces the problem.

    > Thinking that maybe there was not sufficient time for the image to be
    > displayed, I added a Thread.sleep(4000); line, but this did not help.


    Invoking sleep() on the event dispatch thread is a bad idea.

    > Included below are the two actionPerformed methods from each button,
    > plus the DrawImage and MyFilter Classes. I would appreciate if anyone
    > could indicate possible causes for the failure of the open directory
    > display method.

    [...]

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 23, 2010
    #2
    1. Advertising

  3. On 6/23/2010 3:18 AM, jimmy wrote:
    > Hi All,
    >
    > I am developing a GUI to display jpg images in a JPanel. I have
    > created two ways for the images to be selected: (i) a JFileChooser
    > button to select a single jpg, and (ii) a JFileChooser to select a
    > directory containing jpg files. Once the images have been selected,
    > they should be displayed on the JPanel.
    >
    > In the case of opening the single file, this works as I hoped. However
    > when the open directory scenario is used, nothing is displayed.
    > Through the use of println statements I have been able to establish
    > the programme is executing the same Classes and Methods as in the
    > single image display. In addition to displaying an image, I have a
    > JLabel in which I display the name of the current image. Again for the
    > single image file selection, this works. For the open directory
    > scenario, only the name of the final image in the directory is
    > displayed (without the image).
    >
    > Thinking that maybe there was not sufficient time for the image to be
    > displayed, I added a Thread.sleep(4000); line, but this did not help.
    >
    > Included below are the two actionPerformed methods from each button,
    > plus the DrawImage and MyFilter Classes. I would appreciate if anyone
    > could indicate possible causes for the failure of the open directory
    > display method.
    >
    > Many thanks,
    >
    > Jimmy
    >
    >
    >
    > BufferedImage image;
    > String filename;
    > DrawImage pic = null;
    >
    > DrawImage pic2 = null;
    > String filename2;
    > BufferedImage image2 = null;
    >
    > private void
    > openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
    > {
    > JFileChooser chooser = new JFileChooser();
    > int returnVal = chooser.showOpenDialog(chooser);
    > if (returnVal == JFileChooser.APPROVE_OPTION) {
    > File file = chooser.getSelectedFile();
    > try {
    > image = ImageIO.read(file);
    > }
    > catch (IOException ex) {
    > System.out.println("There was a problem opening
    > the selected file: " + ex);
    > }
    >
    > //mainImagePanel is the target JPanel for the image
    > Graphics g=mainImagePanel.getGraphics();
    >
    > pic = new DrawImage();
    > pic.setImage(image);
    > pic.paintComponent(g);
    >
    > //Add label text
    > filename = file.getPath();
    > jLabel1.setText(filename);
    > }
    > }
    >
    > private void
    > openBatchButtonActionPerformed(java.awt.event.ActionEvent evt)
    > {
    >
    > //JFileChooser to select a batch directory
    > JFileChooser chooserBatch = new JFileChooser();
    >
    > chooserBatch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    >
    > int returnValBatch =
    > chooserBatch.showOpenDialog(chooserBatch);
    > File fileBatch = chooserBatch.getSelectedFile();
    >
    > if (returnValBatch == JFileChooser.APPROVE_OPTION) {
    > System.out.println("Batch directory chosen: " +
    > fileBatch);
    > } else {
    > System.out.println("Error selecting batch directory: ");
    > }
    >
    > //Get a file array of all jpgs in this directory
    > FilenameFilter only = new MyFilter("jpg");
    > File[] fileArray = fileBatch.listFiles(only);
    >


    This is going to take longer than you want to spend on the EDT

    > //loop through jpg files
    > for (int i=0; i< fileArray.length; i++){
    >
    > try {
    > image2 = ImageIO.read(fileArray);
    > } catch (IOException ex) {
    >
    > Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE,
    > null, ex);
    > }
    > System.out.println("current file: " + fileArray);
    >


    You are creating new DrawImage here but not putting anywhere it can be
    seen. Also, calling paintComponent() directly is probably not what you
    want to do.

    > //load image to main image panel
    > Graphics gg = mainImagePanel.getGraphics();
    > pic2 = new DrawImage();
    > pic2.setImage(image2);
    > pic2.paintComponent(gg);
    >
    > //add filename to label
    > filename2 = fileArray.getPath();
    > jLabel1.setText(filename2);
    >
    > }
    > }


    This looks OK.

    > /*****************************/
    > /*** Draw Main Image Panel ***/
    > /*****************************/
    >
    > class DrawImage extends JPanel{
    >
    > BufferedImage img;
    >
    > public DrawImage() {
    > }
    >
    > public void setImage(BufferedImage image){
    > this.img = image;
    > repaint();
    > }
    >
    >
    > @Override
    > public void paintComponent(Graphics g)
    > {
    > super.paintComponent(g);
    > Graphics2D g2d = (Graphics2D)g;
    > if(img != null) {
    > g2d.scale(0.2, 0.2);
    > g2d.drawImage(image, 0, 0, null);
    > }
    > else {
    > System.out.println("Image passed for drawing to main
    > panel is null");
    > }
    > }
    > }
    >


    This ought to work too.

    > /***********************/
    > /*** Filename Filter ***/
    > /***********************/
    >
    > public class MyFilter implements FilenameFilter {
    >
    > String ext;
    > public MyFilter(String ext) {
    > this.ext = "." + ext;
    > }
    > public boolean accept(File dir, String name) {
    > return name.endsWith(ext);
    > }
    > }



    Active rendering, calling paintComponent() directly is a useful and
    quick method in some circumstances. I would however avoid doing that in
    this case unless you are trying to create some sort of animation and
    then your program needs to be redesigned to make it work correctly.

    You should create an SSCCE, that will allow the list to give you better
    advice.

    --

    Knute Johnson
    email s/nospam/knute2010/
     
    Knute Johnson, Jun 23, 2010
    #3
  4. jimmy

    Daniel Pitts Guest

    On 6/23/2010 3:18 AM, jimmy wrote:
    > Hi All,
    >
    > I am developing a GUI to display jpg images in a JPanel. I have
    > created two ways for the images to be selected: (i) a JFileChooser
    > button to select a single jpg, and (ii) a JFileChooser to select a
    > directory containing jpg files. Once the images have been selected,
    > they should be displayed on the JPanel.
    >
    > In the case of opening the single file, this works as I hoped. However
    > when the open directory scenario is used, nothing is displayed.
    > Through the use of println statements I have been able to establish
    > the programme is executing the same Classes and Methods as in the
    > single image display. In addition to displaying an image, I have a
    > JLabel in which I display the name of the current image. Again for the
    > single image file selection, this works. For the open directory
    > scenario, only the name of the final image in the directory is
    > displayed (without the image).
    >
    > Thinking that maybe there was not sufficient time for the image to be
    > displayed, I added a Thread.sleep(4000); line, but this did not help.
    >
    > Included below are the two actionPerformed methods from each button,
    > plus the DrawImage and MyFilter Classes. I would appreciate if anyone
    > could indicate possible causes for the failure of the open directory
    > display method.
    >
    > Many thanks,
    >
    > Jimmy

    Instead of drawing the image yourself, you should consider using
    JLabel's "setIcon" method and an ImageIcon instance. It makes adding
    images much easier, and is very flexible.

    HTH.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 23, 2010
    #4
  5. jimmy

    jimmy Guest

    Thanks all for your help again. I didn't include the complete code for
    my GUI as it was built using NetBeans and contains lots of other
    buttons and panels, which were not relevant to the problem. I was
    unaware of the SSCCE method of describing a problem, and I find it a
    very good method. I did write an SSCCE example showing my problem, but
    I fear that, as pointed out by Knute Johnson, the problems with my
    code run deeper than the problem I described. I haven't fully grasped
    how to work with images, therefore I am going to buy a book on Java
    today (Head First Java seems to receive high praise) and spend some
    time getting to grips with the basics.

    Thanks Daniel Pitts for the JLabel suggestion, however I need to be
    able to manipulate the images, which I believe is beyond the scope of
    JLabel.

    In the meantime I need to create a GUI for image analysis, so I have
    started one in MATLAB (which I am much more familiar with than Java).
    My intention is to create the final version in Java however.

    Cheers,

    Jimmy
     
    jimmy, Jun 27, 2010
    #5
  6. On 6/27/2010 2:08 AM, jimmy wrote:
    > Thanks all for your help again. I didn't include the complete code for
    > my GUI as it was built using NetBeans and contains lots of other
    > buttons and panels, which were not relevant to the problem. I was
    > unaware of the SSCCE method of describing a problem, and I find it a
    > very good method. I did write an SSCCE example showing my problem, but
    > I fear that, as pointed out by Knute Johnson, the problems with my
    > code run deeper than the problem I described. I haven't fully grasped
    > how to work with images, therefore I am going to buy a book on Java
    > today (Head First Java seems to receive high praise) and spend some
    > time getting to grips with the basics.
    >
    > Thanks Daniel Pitts for the JLabel suggestion, however I need to be
    > able to manipulate the images, which I believe is beyond the scope of
    > JLabel.
    >
    > In the meantime I need to create a GUI for image analysis, so I have
    > started one in MATLAB (which I am much more familiar with than Java).
    > My intention is to create the final version in Java however.
    >
    > Cheers,
    >
    > Jimmy


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;

    public class test extends JPanel {
    private BufferedImage image;

    public void setImage(BufferedImage bi) {
    image = bi;
    if (image != null) {
    setPreferredSize(new Dimension(
    bi.getWidth(),bi.getHeight()));
    revalidate();
    repaint();
    }
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null)
    g.drawImage(image,0,0,null);
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    final JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final test t = new test();
    JScrollPane sp = new JScrollPane(t);
    f.add(sp,BorderLayout.CENTER);
    JButton b = new JButton("Load Image");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(f) ==
    JFileChooser.APPROVE_OPTION) {
    try {
    File file = chooser.getSelectedFile();
    if (file.exists())
    t.setImage(ImageIO.read(file));
    } catch (IOException ioe) {
    JOptionPane.showMessageDialog(f,ioe);
    }
    }
    }
    });
    f.add(b,BorderLayout.SOUTH);
    f.setSize(400,300);
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2010/
     
    Knute Johnson, Jun 27, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,036
  2. Replies:
    0
    Views:
    360
  3. Replies:
    0
    Views:
    372
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,550
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,447
    t1m1976
    Nov 7, 2010
Loading...

Share This Page