Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default Trouble drawing image from file picked by JFileChooser

    I'm not to certain where I went wrong, maybe I oberlooked something so I'll post the whole code. Its is meant to display an image you select in an extension of Frame. Also the bold portion was generated entirely by netBeans GUI Builder. The FileFilterExtension aslo seems fine. The red text are the lines mentioned in the error

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    [COLOR="Red"]public class FileTest extends javax.swing.JFrame {[/COLOR]
       javax.swing.filechooser.FileFilter ff = new FileFilterExtension();
       Graphics g;
    
        public FileTest() {
            setTitle("Pick a file");
            this.setResizable(false);
            initComponents();
            jFileChooser1.setFileFilter(ff);
        }
    
    [B]    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jFileChooser1 = new javax.swing.JFileChooser();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    [COLOR="Red"]jFileChooser1ActionPerformed(evt);[/COLOR]
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>      [/B]                  
    
        private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
            String filename = jFileChooser1.getSelectedFile().getName();
            final ImageFrame iFrame = new ImageFrame(filename);
            iFrame.setSize(1024,768);
            iFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    iFrame.setVisible(false);}});
                    iFrame.setVisible(true);
                   [COLOR="Red"] iFrame.showImage(g);[/COLOR]
        }                                             
    
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FileTest().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JFileChooser jFileChooser1;
        // End of variables declaration                   
    
    }

    Java Code:
    class ImageFrame extends Frame{
            Image image;
            public ImageFrame(String name){
                setTitle(name);
                image = Toolkit.getDefaultToolkit().getImage(name); 
            }
            public void showImage(Graphics g){
                [COLOR="Red"]g.drawImage(image,0,0,this);[/COLOR]
    	}
    }

    The error seems to come from the drawImage call yet I've never had this problem before. Is it somehow possible the image is null?
    Moderator Edit: Code Tags Added
    Last edited by Fubarable; 12-25-2010 at 01:30 AM. Reason: Moderator Edit: Code Tags Added

  2. #2
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    I did stop the errors for some by changing the drawImage to
    g.drawImage(image,0,0,1024,768,this);
    but it still shows nothing and I checked and the images don't seem to be null...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You should read the tutorials on how to show images in Swing as your code is all wrong. For one, you don't use AWT components such as Frame, for another if all you want to do is show the image, then put it in an ImageIcon and display it in a JLabel. If you want to show a background image in a GUI, then do it in a JPanel and draw in the JPanel's paintComponent method. Again the Swing tutorials will show you how to do this correctly.

  4. #4
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Thank you, I realized that I should override the paint method and not call the showImage method from main. There are no more errors but still no output. The only puzzling thing is that JCreatorLE displayed images with similar code to this for example
    Java Code:
     
    public class Testpaint
    {
    	public static void main(String args[])
    	{
    		GenericFrame gfx = new GenericFrame();
    		gfx.setSize(1024,768);
    		gfx.addWindowListener(new WindowAdapter()
    		 {public void windowClosing(WindowEvent e)
    		  {System.exit(0);}});
    		gfx.show();
    	}
    }
    
    class GenericFrame extends Frame
    {
        Image image;
    	public void paint(Graphics g){
                image = Toolkit.getDefaultToolkit().getImage("Beach.jpg");
                g.drawImage(image, 0,0,1024,768,this);
    	}
    }
    While NetBeans only brings up the frame. The image is in the same directory as the code yet NetBeans won't display it. :confused:

  5. #5
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Also I'm only using Frame because thats what my CS Class uses and for grading purposes its better if I do the same.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Then you're beyond help. Why ask in a forum if you're unwilling to follow the perfectly valid advice offered?

    Also, your code formatting is atrocious. Recommended reading: Code Conventions for the Java(TM) Programming Language: Contents

    And finally, dump the NetBeans visual designer aka Matisse. It's not a beginner's tool.

    db

  7. #7
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    I've taken your advice, Fubarable, and decided to use a label. And DB, how is my formatting atrocious? Please tell me which parts specifically so I know where to look in the code conventions. Are these some of the parts you are talking about?
    Java Code:
            setTitle("Pick a file");
            this.setResizable(false);
            initComponents();
            jFileChooser1.setFileFilter(ff);
    Java Code:
     
    String filename = jFileChooser1.getSelectedFile().getName();
            final ImageFrame iFrame = new ImageFrame(filename);
            iFrame.setSize(1024,768);
            iFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    iFrame.setVisible(false);}});
                    iFrame.setVisible(true);
                    iFrame.showImage(g);
    But anyway the problems solved. Thanks for your help Fubarable.

Similar Threads

  1. trouble with JFileChooser
    By grcunning in forum AWT / Swing
    Replies: 3
    Last Post: 09-04-2010, 09:34 PM
  2. how to display image selected by JFileChooser
    By khushi.cutegal in forum AWT / Swing
    Replies: 17
    Last Post: 07-24-2010, 07:03 AM
  3. How to display image in a panel with JFileChooser
    By srisar in forum AWT / Swing
    Replies: 14
    Last Post: 10-11-2009, 07:06 PM
  4. Replies: 2
    Last Post: 04-24-2009, 09:10 AM
  5. Replies: 1
    Last Post: 04-17-2009, 01:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •