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?