Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Only one image is drawn?  (Read 1517 times)
0 Members and 1 Guest are viewing this topic.
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Posted 2013-03-10 20:11:39 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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?
Offline cylab

JGO Ninja


Medals: 49



« Reply #1 - Posted 2013-03-10 21:48:52 »

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.

Mathias - I Know What [you] Did Last Summer!
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2013-03-10 21:49:06 »

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:

1  
2  
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!)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #3 - Posted 2013-03-10 23:17:05 »

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)
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:
1  
2  
3  
4  
5  
6  
       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.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2013-03-10 23:21:53 »

Where exactly did you store your images?

Offline cylab

JGO Ninja


Medals: 49



« Reply #5 - Posted 2013-03-10 23:30:02 »

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?

Mathias - I Know What [you] Did Last Summer!
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #6 - Posted 2013-03-10 23:44:32 »


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.
Offline cylab

JGO Ninja


Medals: 49



« Reply #7 - Posted 2013-03-10 23:55:09 »

The it's Eclipse - I hate it anyway - use a better IDE *runsForCover* Wink

Mathias - I Know What [you] Did Last Summer!
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #8 - Posted 2013-03-11 00:04:32 »

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 Cheesy 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).
Online Longarmx
« Reply #9 - Posted 2013-03-11 00:05:32 »



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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #10 - Posted 2013-03-11 00:11:17 »

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

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

It wasn't the name, but I still wonder why&how it happened...
Offline Quarry
« Reply #11 - Posted 2013-03-14 18:21:59 »

Try this one;


1  
2  
3  
4  
5  
6  
7  
      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
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #12 - Posted 2013-03-14 20:02:56 »

I think this issue has already been resolved?

Offline Quarry
« Reply #13 - Posted 2013-03-14 20:29:13 »

Herp derp disregard post then
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #14 - Posted 2013-03-14 20:30:05 »

No problem! Cheesy

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (22 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (55 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (29 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!