Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Help with pictures

    I have some problems printing some pictures on the screen.

    How come this works:
    Java Code:
    icons[0] = new ImageIcon("Images\\player.png", "player image");
    but not this:
    Java Code:
    icons[0] = createImageIcon("Images\\player.png", "player image");
    and the method createImageIcon:
    Java Code:
    protected ImageIcon createImageIcon(String path, String name) {
    		System.out.println("generating Image...");
    		java.net.URL imgURL = getClass().getResource(path);
    		
    		if(imgURL != null) {
    			System.out.println("image generated!");
    			return new ImageIcon(path, name);
    		}else {
    			System.err.println("Failed to generate icon!\ncould not find file: " + imgURL);
    			return null;
    		}
    	}
    The problem is that in the createIconImage() method, it gives a imgURL of "null", so it never enters the return new ImageIcon(imgURL, name);

    can someone help me with this? It is just so my program doesn't have to get destroyed b-cuz I typed in the wrong path. Thx for any help :)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with pictures

    The createImageIcon method (as it is called in your code) looks for the image relative to the class that is calling it. If the class is in a package, then it looks for the Images folder within the package folder. But first things first, the getResource() method takes UNIX style path separators (see the API for more information), so presuming the class and Images directory are in the same directory, try 'Images/player.png'

  3. #3
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Help with pictures

    will "C:/Users/ole/Desktop/player.png" then be a valid path for that method?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with pictures

    Quote Originally Posted by HelloWorld1234 View Post
    will "C:/Users/ole/Desktop/player.png" then be a valid path for that method?
    Again, I'll point you to the API. The path you pass to getResource() should be relative to either the root package folder (in which one precedes the path with a '/' to denote it is an absolute path relative to the root package) or the class that contains the method. As an example, suppose you have a class named MyClass, and in the same directory you have a folder named Images which contains a file named player.png, then you can call:
    getClass().getResource("Images/player.png");

  5. #5
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Help with pictures

    okay I managed to fix it :)

  6. #6
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Help with pictures

    now That I fixed that, how do I resize the picture I have used here? is it possible to resize a ImageIcon, or do I have to use something else?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with pictures

    There's a method in the Graphics class that can draw scaled images.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Help with pictures

    I found the method, but I have no idea how to make an Image, so I'll guess I'll look at the API for it or something.

  9. #9
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Help with pictures

    Ok I need some help to make an Image... :P maybe a link to a good site to learn it, or just give me an explaination here.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with pictures

    Load an ImageIcon from a file (or any other resource); get its Image and draw it on your Graphics object.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help with pictures

    If those aren't animated GIFs, load the image as a BufferedImage using ImageIO#read(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  2. Help with pictures
    By HelloWorld1234 in forum New To Java
    Replies: 2
    Last Post: 06-14-2012, 12:40 AM
  3. PNG pictures
    By nhmllr in forum New To Java
    Replies: 15
    Last Post: 10-09-2011, 05:13 PM
  4. 3D pictures
    By mine0926 in forum Advanced Java
    Replies: 8
    Last Post: 10-27-2010, 01:43 AM
  5. pictures
    By chalo in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 12:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •