Only one image is drawn?

I am following David Brackeen’s Developing Games In Java. I happen to have a copy of it on my hard disk, so I said why not.
Anyway, I was trying to print an image to screen. I could not.
Then followed how he did it. Wrote the same code. One worked, so I tried with two images. Now only one is drawn and other is not.
I use Eclipse and both images are in my project folder, inside of the package ‘Deneme’. Here is the relevant codes:

public void loadImages(){
		 image = loadImage("src/Deneme/c2.png"); // I also tried "/Deneme/c2.png", "/c2.png" and "c2.png" as the parameter.
		 image2 = loadImage("src/Deneme/c1.png");
		 loadImage = true;   //A boolean to control if the images are loaded.
		 repaint();
	}
	
	public Image loadImage(String fileName){
		System.out.println("Got: "+fileName);  // I thought maybe only one of them is loaded for a reason, but no...
		return new ImageIcon(fileName).getImage();
	}
	
	public void paint(Graphics g){		
		Graphics2D g2d = (Graphics2D)g; //I also commented out this part thinking maybe this is the problem but it was not.
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.drawString("TEST IS OKAY", 800, 500);
		if(loadImage){
			g.drawImage(image2, 100,100, null);
			g.drawImage(image, 300, 400, null);
		}
	}

Here is the full code. The guy uses two classes. So did I.
http://pastebin.java-gaming.org/133f9046c4f

EDIT: Okay, this is embarrassing. It draws both if I put them in D: driver and run the code. But if the path was wrong, why the other one was drawn?

Dunno, but don’t use filenames anyway, use URLs you get from getResource(), so it will work as long as your images are in the classpath - even if they are in a jar…

See http://www.java-gaming.org/topics/jgo-frequently-asked-questions/26316/msg/230171/view.html#post_how_do_i_load_resources. The principle is the same, just omit AsStream if you only want to get the url for apis that don’t take inputstreams.

I don’t know why it was displaying one image, but seeing you’ve stored your images with your sources, you might want to load the images from the classloader and using ImageIO:


image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("Deneme/c1.png"));
image2 = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("Deneme/c2.png"));

(so without the ‘src’ part in the path!)

Thanks guys!
@ cylab
I didn’t use file names in the first place but it didn’t show.

OK, while I was trying the thing from the FAQ cylab pointed me, this happened:

[quote]Exception in thread “main” java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at Deneme.FullScreen.loadImages(FullScreen.java:66)
at Deneme.FullScreen.run(FullScreen.java:42)
at Deneme.FullScreen.main(FullScreen.java:32)
[/quote]
It can’t find the source. Funny thing is that, it is there. And the other is found and printed to the screen. I use the same thing. This is driving me crazy!
This works:

		 try {
			 image = ImageIO.read(getClass().getResource("c2.png")); //Also c1.png is shown.
			//image2 = ImageIO.read(getClass().getResource("c1.png")); -----------> Commented out
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();}

If I use image2 and image at the same time, it just doesn’t show it.

I tried every path combination, my primitive method, two you guys listed. It just doesn’t show it.

Where exactly did you store your images?

You don’t happen to have special characters in your turkish abc that look like c or 2? Or have some extra space in the filenames?

What about the security settings of that file?

Did you copy the files when it worked on D: or did you recreate them there?

https://dl.dropbox.com/u/19432607/myPath.png

This is where I copied them. And this is the path on my pc:
F:\DEVELOPING\Workspaces\Java\62 - Full Screen - Image\src\Deneme

Can spaces cause such weird issue? They never did, but then again, I did not print any images until yesterday.

I just drag&dropped them into the project (on Eclipse).

And in Turkish alphabet we only have ç similar to c, but I renamed the files, so it is a ‘c’. File name is right.

Something funnier; Now both don’t open. I’m going to start another project, and this time I’ll copy them on file explorer.

The it’s Eclipse - I hate it anyway - use a better IDE runsForCover :wink:

Lol I could have killed you if I was a fanatic.

Anyway, do we have a mechanism to tag this as solved? I figured it. Actually I did not. I copied everything into another project and renamed the images to a and a2 :smiley: It worked.
I will never use c1 and c2 for a file name again.

Thanks for help though! I just learnt a more reliable way to load images (And probably other stuff).

One of the images is named c3.png, not c2.png :slight_smile:

Good catch :slight_smile: But I tried to change the filename later. I mean it was c2.png once upon a time :stuck_out_tongue:

I was so desperate that I tried every single thing… Their names were not c1 and c2 in the first place :smiley:

It wasn’t the name, but I still wonder why&how it happened…

Try this one;

		InputStream in = Main.class.getResourceAsStream(fileDirectory);
		BufferedImage image = null;
		try {
			image = ImageIO.read(in);
		} catch (IOException e) {
			e.printStackTrace();
		}

Right click your project > New > Source folder and then name it “res” (without quotes obviously)
Then put your images in res

When you have done those replace fileDirectory with whatever parameter you are using (fileName?), it should be like “/c1.png” and not “c1.png”

Umarım yardımcı olabilmişimdir :wink:

I think this issue has already been resolved?

Herp derp disregard post then

No problem! :smiley: