Results 1 to 12 of 12
  1. #1
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Loading Images in a Canvas

    Hey
    I am fairly new to Java, but I am using it for a class I am in. I am programming a to down style game for my project and need to load some images. I have tried all sorts of methods, but I can't seem to get any to work. What I am trying to do is make a load image function that will load .pngs and return them to the variable. I originally used toolKit to load them, and there were no errors. Then I output the image in my render function using g.drawImage(name, int, int, int, int, this), but nothing showed up on the canvas. Any help would be greatly appreciated!
    Thanks

    Grimey

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

    Default Re: Loading Images in a Canvas

    Either the loading of the image fails or the drawing of the image fails; check if the image loads (check if the image is non-null) and check if you get null pointer exceptions. If you can't find it post a bit of code that demonstrates the problem (leave out the irrelevant stuff as much as possible).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    I made a little check to see if the image was null, and it turned up true. Here is the code that is relevant, if you need more, let me know.

    Java Code:
    Image[] tileExt = new Image[11];
    
    public Image loadImage(String name) {
    			BufferedImage image = null;		//Creates the temp variable for return
    			//Loads an image from the directory
    			try {
    				image = ImageIO.read(new File(name));
    			}
    			catch (IOException e) {
    				//Do nothing
    			}
    			
    			return image;		//Returns the image
    	   }
    
    public void initImg() {
    		//Loads the exterior image set
    		tileExt[0] = loadImage("/extTileSet/mountain.png");
    		tileExt[1] = loadImage("/extTileSet/water.png");
    		tileExt[2] = loadImage("/extTileSet/tree1.png");
    		tileExt[3] = loadImage("/extTileSet/tree2.png");
    		tileExt[4] = loadImage("/extTileSet/roof.png");
    		tileExt[5] = loadImage("/extTileSet/window.png");
    		tileExt[6] = loadImage("/extTileSet/frontWall.png");
    		tileExt[7] = loadImage("/extTileSet/door.png");
    		tileExt[8] = loadImage("/extTileSet/stepsExt.png");
    		tileExt[9] = loadImage("/extTileSet/gravel.png");
    		tileExt[10] = loadImage("/extTileSet/grass.png");		
    	}
    
    
    
    public void render() {
    		BufferStrategy bs = getBufferStrategy();		//Buffer strategy
    
    		if (bs == null) {
    			createBufferStrategy(2);					//Second buffer strategy
    			return;
    		}
    
    		Graphics g = bs.getDrawGraphics();				//Creates the graphics
    
                    g.drawImage(tileExt[0], 0, 0, 32, 32, null);
    		
    		g.dispose();		//Memory dump
    		bs.show();			//Display graphics
    	}
    Thanks again!

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

    Default Re: Loading Images in a Canvas

    Quote Originally Posted by Grimey View Post
    I made a little check to see if the image was null, and it turned up true.
    Does that read as: the image was null?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    I just used a little if statement in the render function that outputted if the image was null.

    Java Code:
    if (tileExt[0] == null {
    System.out.println("Image is null");
    }
    The check was returning that it was true. Is it possible that I am not loading the images correctly. I created a folder in the project called extTileSet. Am I right to write the string name of the image as "/extTileSet/****.png"?

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

    Default Re: Loading Images in a Canvas

    Better supply the absolute path names of the files; you can't go wrong then. Most likely your project folder isn't the root folder so /extTileSet/*.png is not where the files are stored.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    The issue with that is that I am moving between Windows and Linux, mostly I'm using Linux though. I have the project, called Bushido, and within that project is the src folder, and finally, in there I have extTileSet and gamePackage. Am I right to put the images in the gamePackage folder, or should the folder be in the gamePackage?

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

    Default Re: Loading Images in a Canvas

    Quote Originally Posted by Grimey View Post
    Am I right to put the images in the gamePackage folder, or should the folder be in the gamePackage?
    Wherever you put them, you can load the images using getResource().

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

  9. #9
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    I modified the loadImage with a little check. It seems to be finding the images, just not drawing them.

    Java Code:
    try {
    		image = ImageIO.read(Main.class.getResourceAsStream(name));
    				
    		if (image == null)
    			System.out.println("The image was not found");
    		else
    			System.out.println("It found the image");
    }
    It says that the image is being found, but still no luck drawing it.

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

    Default Re: Loading Images in a Canvas

    Try to draw your image on an ordinary Graphics object (in the paintComponent( ... ) method of a JComponent) and see if that works.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    Alright. I used the paint() method, and it worked. The image was outputted. The thing I wonder though now, is if there is any point in extending the program as a canvas and implementing runnable?

  12. #12
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Loading Images in a Canvas

    Well, I loaded the program up today and commented out the pain function, going back to my original render function, and it worked. I'm not quite sure whether or to be terribly annoyed or to be leaping with joy. Thank you so much for all your help! I appreciate it!

Similar Threads

  1. Loading Images
    By wired-in=p in forum New To Java
    Replies: 9
    Last Post: 02-08-2012, 11:53 PM
  2. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-22-2011, 11:18 PM
  3. Multiple Images on a Canvas with SWT
    By jdc in forum SWT / JFace
    Replies: 0
    Last Post: 08-20-2009, 04:05 PM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Posting Permissions

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