Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  Buffered image?  (Read 1966 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2012-04-17 09:01:00 »

Hi all, I've resumed developing my java gaming skills and have used the book "Killer Game Programming in Java"

To teach myself, I'm using the examples in the book and modifying them to create games, but I've hit a snag.

When running a build I get the following errors and I wondered is the book out of date and using incorrect syntax, does java even use buffered image in that form anymore? :

1  
2  
3  
4  
5  
6  
7  
8  
Exception in thread "main" java.lang.NullPointerException
   at java.io.Reader.<init>(Reader.java:78)
   at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
   at ImagesLoader.loadImagesFile(ImagesLoader.java:97)
   at ImagesLoader.<init>(ImagesLoader.java:66)
   at JackPanel.<init>(JackPanel.java:124)
   at JumpingJack.<init>(JumpingJack.java:57)
   at JumpingJack.main(JumpingJack.java:98)


When investigated further, this is one of the apparent errors in the coding:

1  
      BufferedReader br = new BufferedReader( new InputStreamReader(in));


I know this is a vague description of the problem, but as I don't know what the problem is I don't really know what to provide you Tongue any help would be appreciated!
Offline 65K
« Reply #1 - Posted 2012-04-17 09:40:38 »

Probably the image file is not at the place it is supposed to be.

Offline wookoouk

Senior Newbie





« Reply #2 - Posted 2012-04-17 09:41:10 »

Image in the build path?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stranger

Senior Member


Medals: 6



« Reply #3 - Posted 2012-04-17 09:48:51 »

You'd perform io operations with images via ImageIO class:
1  
2  
3  
4  
5  
    BufferedImage img = null;
    try {
    img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
    }


see http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

Anton
Offline 65K
« Reply #4 - Posted 2012-04-17 09:53:04 »

Image in the build path?
Depends on whether the image/stream is accessed/opened as resource to be found in the classpath or as file or as URL.
Take a look into ImagesLoader.

Offline Cruiser

Junior Member


Medals: 1
Projects: 1


Twitter: @totallymorten


« Reply #5 - Posted 2012-04-17 10:01:27 »

I'm running my game from a JAR file. Here I'm loading images like this:

1  
Image zombieImg = ImageIO.read(getClass().getResource("/img/zombie.gif"));


I then have to place "zombie.gif" in the folder "img" at the root of the JAR file.

ps. I'm using the same book :-).
Offline westloar

Junior Member





« Reply #6 - Posted 2012-04-17 11:34:39 »

I could use that method and individually load each image via hard coding, but that seems a little wasteful and could quickly get unmanageable with a game with many assets, this method reads imsInfo.txt, which tells it how big the image is, how many separate images to break it into etc. I think this method is superior and is in line with OOPs and allows easy extension of assets.

I've added the asset files into my IDE in the same project and built it, when run it still outputs the same errors.

The images are obtained through the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
String imsFNm = IMAGE_DIR + fnm;
    System.out.println("Reading file: " + imsFNm);
    try {
      InputStream in = this.getClass().getResourceAsStream(imsFNm);
      BufferedReader br = new BufferedReader( new InputStreamReader(in));
      // BufferedReader br = new BufferedReader( new FileReader(imsFNm));
     String line;
      char ch;
      while((line = br.readLine()) != null) {
        if (line.length() == 0)  // blank line
         continue;
        if (line.startsWith("//"))   // comment
         continue;
        ch = Character.toLowerCase( line.charAt(0) );
        if (ch == 'o')  // a single image
         getFileNameImage(line);
        else if (ch == 'n')  // a numbered sequence of images
         getNumberedImages(line);
        else if (ch == 's')  // an images strip
         getStripImages(line);
        else if (ch == 'g')  // a group of images
         getGroupImages(line);
        else
          System.out.println("Do not recognize line: " + line);
      }
      br.close();
    }
    catch (IOException e)
    { System.out.println("Error reading file: " + imsFNm);
      System.exit(1);
    }
  }  // end of loadImagesFile()


I've made sure that both the assets and imsInfo.txt (text file which tells the reader what kind of image file is present and how many separate sprites to break the image into etc.) are within the project and have rebuilt the project before running it, any ideas?

[EDIT]: The line that netbeans seems to be having a problem with is line 5 if that's any help?
Offline westloar

Junior Member





« Reply #7 - Posted 2012-04-18 14:48:28 »

any ideas?
Offline Cruiser

Junior Member


Medals: 1
Projects: 1


Twitter: @totallymorten


« Reply #8 - Posted 2012-04-18 14:54:30 »

What kind of problem / error do you have? The line [5] should compile just fine, I think.
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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!