3

I am trying to simply draw an image on a jframe by using an Imageicon. However when I run it its just blank. Heres my code...

    public final class PICS

{

  public static final void main(String... aArgs)
  {   
      JFrame frame = new JFrame("IMAGE");
      frame.setVisible(true);
      frame.setSize(500,500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ImageIcon image = new ImageIcon("image/pic1.jpg");
      JLabel label = new JLabel("", image, JLabel.CENTER);
      JPanel panel = new JPanel(new BorderLayout());
      panel.add( label, BorderLayout.CENTER );
  } 
}

I am very new to everything java including this website, so I apologize if im missing something. Also im using Eclipse, and are there specific formats you can use for images, or is there a limit to size?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Please have a look at how to [add Images to your Eclipse Project](http://stackoverflow.com/a/9278270/1057230). I hope this might help :-) – nIcE cOw Aug 18 '13 at 06:04
  • Moreover this [answer](http://stackoverflow.com/a/9866659/1057230), tries to explain the whole thingy in a much better way :-) – nIcE cOw Aug 18 '13 at 11:14

2 Answers2

2

Two things.

First, make setVisible the last call AFTER you have built your frame and it's contents...ie

JFrame frame = new JFrame("IMAGE");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
// Make me last
frame.setVisible(true);

Two, make sure that the image/pic1.jpg exists and is the directory image under the current execution context.

If the image is a embedded resource (lives within the Jar or your application), then you need to supply a URL to the image instead of a String as ImageIcon treats String as a file name...

ImageIcon image = new ImageIcon(PICS.class.getResource("image/pic1.jpg"));

For example.

I would encourage you to use JFrame#pack over JFrame#setSize as it will resize the frame to the preferred size of your content...

I would also encourage you to take the time to read through Code Conventions for the Java Programming Language, Initial Threads.

I would also encourage you to use ImageIO over ImageIcon as it will, at least, throw an Exception if something goes wrong

Updated, testing image path

Try adding this to the constructor of your PICS class. This will, at least, tell you where the image isn't...

try {
    ImageIO.read(PICS.class.getResource("image/pic1.jpg"));
} catch (IOException ex) {
System.out.println("Not in image/pic1.jpg");
}
try {
    ImageIO.read(PICS.class.getResource("/image/pic1.jpg"));
} catch (IOException ex) {
System.out.println("Not in /image/pic1.jpg");
}
try {
    ImageIO.read(PICS.class.getResource("resources/image/pic1.jpg"));
} catch (IOException ex) {
System.out.println("Not in resources/image/pic1.jpg");
}
try {
    ImageIO.read(PICS.class.getResource("/resources/image/pic1.jpg"));
} catch (IOException ex) {
System.out.println("Not in /resources/image/pic1.jpg");
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

I am very new to everything java including this website

Then I would suggest you start by reading tutorials especially the Swing tutorial. Maybe the section on How to Use Icons would be a good place to start. The example code will show you how to use Icons as well as how to structure your program so that the GUI code is executed on the EDT. The tutorial on Concurrency will explain why the EDT is important.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    I have looked at those and that is why I have come this far, but I couldn't find the solution to my problem there, though it is very likely I just need to pay more attention to them or start completely from the beginning and read everything. But its getting late and I just want to figure this out and get some sleep. – EternalChronicle Aug 18 '13 at 04:29
  • 1
    @user2693212, `I have looked at those and that is why I have come this far` - So why doesn't your code look like the code from the examples in the tutorial? Why don't you create the GUI on the EDT. Why to you make the frame visible before the components are added to the frame? `I just need to pay more attention to them` - There are no shortcuts to learning. `But its getting late and I just want to figure this out and get some sleep` We are here to help people who make an honest effort, being tired is not an excuse for ignoring the tutorials. – camickr Aug 18 '13 at 04:41
  • 1
    Well to be more specific I looked at the Imageicon and panels tutorials, and this was the first thing I looked at, so it wouldn't look like the code I ended up using because I looked at lots of other tutorials and questions after and ended up with what I have above. Also im not 'Ignoring the tutorials' Im just going to read them in the morning when Im fully aware. – EternalChronicle Aug 18 '13 at 04:47