Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Help with image icons in java

  1. Mar 21, 2017 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Here's the snippet I'm erroring on. I'm sure I have a bad syntax or I'm not relative referencing correctly or something. Null pointer.
    Code (Java):
         
            xo = new ImageIcon[2][2];
            xo[0][0] = new ImageIcon(getClass().getResource("img/x1.png"));
            xo[0][1] = new ImageIcon(getClass().getResource("img/o1.png"));
            xo[1][0] = new ImageIcon(getClass().getResource("img/x2.png"));
            xo[1][1] = new ImageIcon(getClass().getResource("img/o2.png"));
            player = 0;
            style = 0;
            setOpaque(true);
    Basically I just want to load in my images to an array to be referenced later. It's for a tic tac toe game. 2 players, 2 styles of x's and o's.
    I have:
    workspace -> project 2 -> img -> (png files)

    I pretty much ripped this off stack exchange, and that is how the user was referencing their images, except with 2 slashes. I tried changing that. It didn't do anything. Same error. Am I referencing improperly? Or am I doing something wrong in the ImageIcon?

    *edit
    http://stackoverflow.com/questions/17110315/jbutton-background-image
    this is where I got the code from
     
    Last edited by a moderator: Mar 22, 2017
  2. jcsd
  3. Mar 22, 2017 #2

    Mark44

    Staff: Mentor

    One problem with code like this is that if something goes wrong, it's hard to tell exactly where things are going wrong.
    Code (Java):
     xo[0][0] = new ImageIcon(getClass().getResource("img/x1.png"));
    I haven't done any coding in Java for about 20 years, so I'm not very familiar with all of the APIs that make up the language these days. In the ImageIcon() constructor, you're calling getClass(). Is this a static method? If not, you need an object on which to call it.

    Here's some other code I found:
    Code (Java):
    Image img;

    public ResourceTest() throws Exception {
       URL myurl = this.getClass().getResource("/myimage.gif");
       Toolkit tk = this.getToolkit();
       img = tk.getImage(myurl);
    }
    Comparing this snippet to your code, it appears that you are using getResource() incorrectly. This method apparently returns the URL for a resource, which in your case would be the URL for an image, not the image itself. Take a look at the code I copied. He or she used getResource to return a URL, and then used getImage() to actually get the image.

    As far as the forward slashes in the code you referenced on SE, I can't think of any reason why someone would use two forward slashes. Two backslashes, yes, but not two forward slashes. Many times when a program fails to load a file, it's because a relative path to the file can't be accessed from the location where the code is. In your working directory, is the a directory named img?

    When I was doing Java, there wasn't much in the way of debugging capabilities. By now, I'm fairly sure they are pretty full featured. I would think you should be able to pause your program and do a quick watch to evaluate some expression (like your code to get a resource. That would tell you exactly what is failing.
     
    Last edited by a moderator: Mar 22, 2017
  4. Mar 22, 2017 #3

    jedishrfu

    Staff: Mentor

    I think there's a step missing here:

    https://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

    Notice in retrieving resources you need to retrieve the classloader first. That's if the images are in your jar file as is common if you use maven to build your project and your images are stashed in ./src/main/resources/images
    subdirectory.

    So try this instead:

    Code (Java):
       
            ClassLoader classLoader = this.getClass().getClassLoader();
            xo = new ImageIcon[2][2];
            xo[0][0] = new ImageIcon(classLoader.getResource("img/x1.png"));
            xo[0][1] = new ImageIcon(classLoader.getResource("img/o1.png"));
            xo[1][0] = new ImageIcon(classLoader.getResource("img/x2.png"));
            xo[1][1] = new ImageIcon(classLoader.getResource("img/o2.png"));
            player = 0;
            style = 0;
            setOpaque(true);
     
  5. Mar 22, 2017 #4

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Yes there is. The directory setup is Eclipse Workspace -> Project Folder [titled project 2] -> img [houses the images
    In this case, I would want to actually change my directory setup, though, correct?
     
  6. Mar 22, 2017 #5

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I just went to try it, however, I don't have anything in my src folder other than my .java files.
     
  7. Mar 22, 2017 #6

    jedishrfu

    Staff: Mentor

    What happens during build is classes are compiled and placed in a target directory (called "target" for maven, "build" for ant I think) and resources are copied over to it as well. Then the whole collection is zipped up into a jar file along with a META-INF/manifest.mf file.

    Here's where your stuff should be stored to be placed in the jar file by build.

    http://ant.apache.org/easyant/history/trunk/ref/Directorystructure.html

    img should be moved to src/main/resources/img
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Help with image icons in java
  1. Java and missing images (Replies: 14)

  2. Java help (Replies: 3)

  3. Java help (Replies: 6)

  4. Display image in java (Replies: 18)

Loading...