Prev: Is there a 'Filter' or 'Predicate' interface anywhere in the JDK?
Next: Taking a step forward in JAVA programming
From: jimmy on 23 Jun 2010 06:18 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[i]); } catch (IOException ex) { Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("current file: " + fileArray[i]); //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[i].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); } }
From: John B. Matthews on 23 Jun 2010 09:40 In article <1f18d83d-d97e-4837-a8b2-1fca7c9a2247(a)a30g2000yqn.googlegroups.com>, jimmy <jimmy.cullen(a)gmail.com> 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>
From: Knute Johnson on 23 Jun 2010 11:46 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[i]); > } catch (IOException ex) { > > Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE, > null, ex); > } > System.out.println("current file: " + fileArray[i]); > 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[i].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/
From: Daniel Pitts on 23 Jun 2010 13:51 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/>
From: jimmy on 27 Jun 2010 05:08
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 |