8

I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame. It has worked in the past for me and now is not, i can't see what I did wrong.

I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon) to the JLabel, or that to the JFrame.

This is my code...

JFrame frame = new JFrame("no image");
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(image);
frame.add(imagelabel);

The JFrame has been setVisible(true) and pack().

Could someone please help me understand what is wrong.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
user1486826
  • 119
  • 1
  • 1
  • 8
  • Please have a look at this example, [How to add images to your Project](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) or Please follow these [steps](http://gagandeepbali.uk.to/gaganisonline/webpages/makejareclipse.html) – nIcE cOw Jun 28 '12 at 17:41
  • I looked at those examples, they did not help – user1486826 Jun 28 '12 at 18:25
  • Simply place your image next to your .class file, and use it like this ImageIcon image = new ImageIcon(getClass().getResource("yourImage.extension"));. That link has to work, since it's the right way to put your images in your project. I hope you had walked through all the steps mentioned in that !! – nIcE cOw Jun 28 '12 at 18:47
  • Simply copy the nicely idented code from your notepad to the question area, then select all the code and press CTL + K, to format it. You don't really have to press space bar eight times for each line. – nIcE cOw Jun 28 '12 at 18:53
  • that throws a nullpointer exception – user1486826 Jun 29 '12 at 18:57
  • I made it work myself, the solution was to name the images "New Bitmap Image". I think that for some reason it thought i didn't specify a file name, and was looking for the default filename. – user1486826 Jun 29 '12 at 19:53
  • Glad you got it sorted, KEEP UP the GOOD WORK :-) – nIcE cOw Jun 29 '12 at 19:55
  • how to i close this question now that i have solution? – user1486826 Jun 29 '12 at 19:55

3 Answers3

12

Your problem lies here:

   ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
   JLabel imagelabel = new JLabel(character);

You create an ImageIcon "image" but you create your JLabel with "character".

It should be:

JLabel imagelabel = new JLabel(image);
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
2

Try,

ImageIcon image = new ImageIcon("c:\\path\\image.png");
imagelabel = new JLabel(character, image, JLabel.CENTER);
frame.add(imagelabel);

Take a look at Tutorial - How to use Icons

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
-1
import javax.awt.*; 
import java.awt.*; 
import java.awt.event*; 

//class name image 
class image { 
    image() 
    //constructor { 
        Frame f=new Frame("Image"); 
        //Frame
        f.setSize(500,500); 
        f.setVisible(true); 
        Panel p =new Panel(); 
        //Panel 
        f.add(p); 
        p.addLayout(null); 
        ImageIcon ii=new ImageIcon("set your image path"); 
        //ImageIcon is used to image Display . 
        Label l =new Label(ii); 
        p.add(ii); 
        p.setBounds(set you bounds); 
        //Like that(20,20,500,40); 
    } 

    public static void main(String [] args) { 
        image obj = new 
    } 
}
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
  • 2
    Please add some explanations about your code and about that why OP needs your code ;). – shA.t Apr 29 '15 at 06:19