Results 1 to 9 of 9

Thread: Image Loading

  1. #1
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post Image Loading

    I'm stuck on loading an image. I found some sample code and i tried running it however i keep getting the exception:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
    at Class.main(Class.java:35)
    Code:
    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    /**
     *
     */
    public class Class {
    
        public static void main(String[] args) {
            Image image = null;
            try {
                // Read from a file
                File sourceimage = new File("arrowleft.gif");
                image = ImageIO.read(sourceimage);
                
                
    
            } catch (IOException e) {
            }
    
            // Use a label to display the image
            JFrame frame = new JFrame();
            JLabel label = new JLabel(new ImageIcon(image));
            frame.getContentPane().add(label, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    }
    i don't understand why it doesn't work. the image arrowleft.jpg is in the same folder as the class. Could anyone help please.
    Thank you

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I suspect that your file doesn't exist. Since the rest of that initialisation is dependent upon the code in that try block completing successfully, it should be inside the try block. As it is, if an IOException occurs, image will remain null, and the initialisation code will then pass that null pointer to the constructor of ImageIcon.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. The Following User Says Thank You to Twey For This Useful Post:

    mavvy (11-29-2008)

  4. #3
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    That is correct. Is there a specific location where i should put the image file?I have included in the package with the class. I used print statements to debug it so now i know it stops at
    Code:
    image = ImageIO.read(sourceimage);
    and doesn't run that piece of code. I dont know what to do?

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Without any path specified, it will be looked for in the current working directory.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. The Following User Says Thank You to Twey For This Useful Post:

    mavvy (11-29-2008)

  7. #5
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    I found what was wrong with it. I've put the image in C:// directory and it now works.thank you. however i now have a problem with something else. I'm trying to extract pixel data and the printing the array. however when i print the array this is what i get:[[[I@1bf216a. I dont even know what it means. do you?

  8. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's a shallow representation of the array object. [[[I means int[][][]. The rest, after the @ sign, is the memory address of the array.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. The Following User Says Thank You to Twey For This Useful Post:

    mavvy (11-29-2008)

  10. #7
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thank you, I didn't realise that. How would i then print the array contents rather than the memory address. Would i have to write a loop? This is the code I am using:

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package finalproject;
    
    /**
     *
     */
    import java.awt.image.*;
    import java.awt.*;
    // notice here that I extends an image observer, this is
    import java.io.File;
    // not necessary, but I need the ImageObserver to be passed as
    import java.io.IOException;
    // a paramater to get the width and height of the image
    import javax.imageio.ImageIO;
    public final class MyGifPixelGrabber implements ImageObserver
    {
      private Image m_image=null;   // pointer to original image
      private Object m_pixels=null; // will contains either array of 
    				// (for gif, it will be bytes)
      private int m_iNumOfColors=0;  
      private int m_iWidth, m_iHeight;
        private ColorModel m_colorModel;
        private int [] pixelArray;
    
      MyGifPixelGrabber(Image img)
      {
        m_image=img;
        //BufferedImage bi = null;
            try {
                img = ImageIO.read(new File("c:\\strawberry.jpg"));
            } catch (IOException ex) {
            }
      }
    
      public void grabPixels()
      {
        m_iWidth=m_image.getWidth(this);
        m_iHeight=m_image.getHeight(this); 
        // the parameter false below tells java that we want to open indexed
        // file.  When used with a gif file, this will cause
        // palletized mode grab.  When used with jpg, you'll get DirectColorModel
        // data
        // you can set that to true and it will cause all images to be grabbed
        // in DirectColorModel
    
        // CONSTRUCT
        PixelGrabber pixelGrabber=new PixelGrabber(m_image, 0,0, 
          m_iWidth, m_iHeight, true);  
    
        // GRAB
        try
        {
          pixelGrabber.grabPixels();
        }
        catch (Exception e)
        {
          System.out.println("PixelGrabber exception"); 
        }
    
        // GET
        m_pixels=(Object)pixelGrabber.getPixels();
        // get the palette of the image, if possible
        m_colorModel=pixelGrabber.getColorModel();
        // if loading a gif, you will get IndexedColorModel, if jpg, you will
        // get DirectColorModel
        if (!(m_colorModel instanceof IndexColorModel))
        {
          // not an indexed file (ie: not a gif file)
        }
        else
        {
          m_iNumOfColors=((IndexColorModel)m_colorModel).getMapSize();
        }
      }
    
      // you'd need to cast the return values, which will be an array of bytes
      // or an array of ints.  if the file is a gif file, it will return an 
      // array of bytes, if jpg, you will get an array of ints
      public Object getPixels()
      {
          
        return (int[]) m_pixels; 
      }
    
      public int getWidth()
      {
        return m_iWidth;
      }
    
      public int getHeight()
      {
        return m_iHeight;
      }
    
      // this won't contain a valid value if you're grabbing
      // a non palletized image (such as jpgs)
      public int getNumOfColors()
      {
        return m_iNumOfColors;
      }
    
      // returns the red component value of a pixel
      public int getRed(int pixel)
      {
        if ((m_colorModel instanceof IndexColorModel))	  
    	return ((IndexColorModel)m_colorModel).getRed(pixel);
        else
    	return ((DirectColorModel)m_colorModel).getRed(pixel);
      }
    
      // returns the green component value of a pixel
      public int getGreen(int pixel)
      {
        if ((m_colorModel instanceof IndexColorModel))	  
    	return ((IndexColorModel)m_colorModel).getGreen(pixel);
        else
    	return ((DirectColorModel)m_colorModel).getGreen(pixel);
      }
    
      // returns the blue component value of a pixel
      public int getBlue(int pixel)
      {
        if ((m_colorModel instanceof IndexColorModel))	  
    	return ((IndexColorModel)m_colorModel).getBlue(pixel);
        else
    	return ((DirectColorModel)m_colorModel).getBlue(pixel);
      }
    
      public void destroy()
      {
        m_image=null;
        m_pixels=null;
      }
    
      
      
      // we need this method just because we're extending ImageObserver.
      public boolean imageUpdate(Image img, int infoflags, int x, int y, 
        int width, int height) 
      {
        return true;	  
      }  
      
      public static void main(String[] args) {
            Image im = null;    
          try {
                im = ImageIO.read(new File("c:\\strawberry.jpg"));
            } catch (IOException ex) {
            }
          MyGifPixelGrabber grab = new MyGifPixelGrabber(im);
          grab.grabPixels();
          System.out.println(grab.getPixels());
          System.out.println(grab.getHeight());
      }
    }

  11. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, you would. Better languages usually offer a generalised function for doing this sort of thing, but in Java one must do it manually:
    Code:
    void printIntArrayArrayArray(int[][][] xs) {
        for (int[][] ys : xs)
          for (int[] zs : ys)
            for (int z : zs)
              System.out.println(z);
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  12. The Following User Says Thank You to Twey For This Useful Post:

    mavvy (11-29-2008)

  13. #9
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    sorry i'm quite new to java, how would i pass the array to that method? and where would i put it, the main method or with the rest of the code?if you could help that would be great, thanks.

Bookmarks

Posting Permissions

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