Author Topic: Java Accessing Image Problem  (Read 2618 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Java Accessing Image Problem
« on: May 23, 2011, 04:48:42 pm »
Code: [Select]
import javax.swing.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Squared {
public static void main (String[] args)
{
Squared mySquared = new Squared();
}
public Squared()
{
Draw();
}
public void Draw()
{
JFrame mainFrame = new JFrame("Squared");
mainFrame.setSize(480,320);


BufferedImage img = null;
try {
img = ImageIO.read(new File("block.png"));
} catch (IOException e) {
e.printStackTrace();
}

JLabel picLabel = new JLabel(new ImageIcon( img ));
mainFrame.add(picLabel);

mainFrame.setVisible(true);
}
}

I have file named "Squared.java" and an image called "block.png" in the same folder and in the same package (using Eclipse); I get this:

Code: [Select]
javax.imageio.IIOException: Can't read input file!
Why can't it get the file, it's in the same directory. Thanks :)

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Java Accessing Image Problem
« Reply #1 on: May 23, 2011, 09:19:53 pm »
Is it in the same directory that you are searching in, though?  There is a method to get the full path of a file object.  Use that instead of trying to read the picture file, and it will tell you where it is looking for it.

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: Java Accessing Image Problem
« Reply #2 on: May 23, 2011, 09:30:20 pm »
You need to place the image files in the source root. So, if your source (relative to the Eclipse project) is src/mypackage/Squared/<files>, you need to put the image under the src folder. Then, in the code, use this to get the URL of the resource
Code: [Select]
this.getClass().getResource("filename");
« Last Edit: May 23, 2011, 09:32:43 pm by Binder News »
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Java Accessing Image Problem
« Reply #3 on: May 24, 2011, 02:52:35 am »
file:/home/david/Documents/Java/Squared/bin/block.png

Thanks a lot Graphmastur and Binder News. I get now, the image has to be in the folder where the class file is, thanks :)
« Last Edit: May 24, 2011, 08:06:30 am by Scout »