2

I'm trying to use getImage() do display an image on a JPanel in an application I'm writing. I've tried and tried to get this to work for me, and eventually discovered that even when the path is totally incorrect, it still does not work, nor does it return NullPointerException as would be expected.

Image i;    

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(i, 0, 0, 200, 200, this);
} // end paintComponent();

public Pnl() {
    super();
    setBackground(Color.GREEN);
    setBorder(BorderFactory.createLineBorder(Color.GRAY, 10));
    i = Toolkit.getDefaultToolkit().getImage("shrek.jpg");
} // end constructor    

When I run the code with a parameter such as "shrek...jdhhd" in getImage() or something of that sort, it does exactly the same thing.

  • Why would you expect the code above to throw a NPE? Nothing in the code above should do this. Have you tested if your Image is in fact null? I would get my image using `ImageIO.read(...)`, and would get it as a resource, not as a file. I would check for null before using it. – Hovercraft Full Of Eels Oct 20 '13 at 01:41
  • Is this all your code? You never even draw the image to the screen. – Alexis King Oct 20 '13 at 01:43
  • Again, there's nothing in the new code that would throw an NPE. Again, test if the image is null. You can use println statements to help you debug. Again, use `ImageIO.read(...)`. Again, get the image as a resource, not as a file. I feel as if you're ignoring my suggestions. – Hovercraft Full Of Eels Oct 20 '13 at 01:47
  • 1
    Have you tried `g2d.drawImage(null, 0, 0, 200, 200, this);` to see what it does? Have tried checking to see what `i` actually equals? – MadProgrammer Oct 20 '13 at 02:00
  • Try what these guys are saying. Try loading the image a different way. Try actually checking to see if stuff is null. If stuff is not null, try printing parameters to the console to investigate. You can use ImageIO.read and another way is the BufferedImage constructor takes an array of bytes. You can use something rudimentary like FileInputStream if you do it that way. Other ways of loading the image are only going to be like an extra 5 lines of code. – Radiodef Oct 20 '13 at 02:33
  • You should also try to use relative paths http://stackoverflow.com/questions/1978445/get-image-from-relative-path – user2469515 Oct 20 '13 at 02:34
  • @MadProgrammer Maybe OP should try to use `ImageIcon` constructor for simplicity and then extract the `Image` ? – An SO User Oct 20 '13 at 02:53
  • Please have a look at this [answer](http://stackoverflow.com/a/11372350/1057230). Hope it helps in the given direction, but for more details, please refer to this [answer](http://stackoverflow.com/a/9866659/1057230) :-) – nIcE cOw Oct 20 '13 at 03:16
  • Do try to override the [getPreferredSize()](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getPreferredSize()) method also, to define some valid size to the component at start up, as most layouts will give size 0, 0 if none is defined for a `JPanel` – nIcE cOw Oct 20 '13 at 03:23
  • `Image` can be more of a "concept" of a image rather then a physical representation of one, therefore it's possible to have a `Image` object that doesn't actually contain any image data. `Toolkit#getImage` offloads it's loading process to a different thread, meaning that while it returns, the image may not actually be loaded yet. This is the reason why you see the use of `ImageObserver`, this allows interested parties, like the `drawImage` method, not know when the image state has changed. This would suggest that it would possible to "load" an image that doesn't actually exist without issue – MadProgrammer Oct 20 '13 at 05:19

2 Answers2

3

I'm trying to use getImage() do display an image on a

When you post a question post a proper SSCCE if you want help otherwise we spend time guessing.

For example maybe the image is being read properly but the problem is that the size of your panel is (0, 0) so there is nothing to paint.

You should also be overriding the getPreferredSize() method to return a Dimension of (200, 200) since this is the size you want to paint your image at.

Without a SSCCE that shows the context of how this panel is used we are just guessing.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

There are a couple of different ways in which you can read images.

  1. By using ImageIcon and then extracting the Image from it.
  2. Using ImageIO

I suspect a problem with your path to the image. Try this:

Path imgPath = Paths.get("/path/to/image.png");
if(Files.exists(imgPath)){
    // do whatever
} else{

    // incorrect path
}

If the Path does indeed exist:

ImageIcon imageAsIcon = new ImageIcon(imgPath.getAbsolutePath());
Image imageOfIcon = imageAsIcon.getImage();

Now, you can just retrieve its Graphics object and do whatever like maybe convert it into a BufferedImage

You may also try using ImageIO as follows:

BufferedImage img;

try {
    URL url = new URL(new File("/path/to/image.png");
    img = ImageIO.read(url);
} catch (IOException e) {
}
An SO User
  • 24,612
  • 35
  • 133
  • 221