Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  creating a level from an image :) im doing progress but i need help :P  (Read 644 times)
0 Members and 1 Guest are viewing this topic.
Offline Xenon

Senior Newbie





« Posted 2012-08-29 15:50:41 »

hey guys i started working on a class that would take an image and turn it into a map and im progressing Cheesy i have converted all the pixels into hex color codes and stored them in array and everything is working fine Tongue except when im trying to all the createMap method to actually create the tiles :/ its throwing me a nullPointerException and i can't seem to find the reason Tongue any suggestions ?

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  
public class Level {
   
   public static BufferedImage createDataArray(BufferedImage level) {
      String[] pixels = new String[level.getHeight() * level.getWidth()];
      BufferedImage out = level;
      for(int x = 0;x < level.getHeight(); x++) {
         for(int y = 0; y < level.getWidth();y++) {
            int rgb = level.getRGB(x, y);
            String hex = Integer.toHexString(rgb & 0x00ffffff);
            pixels[x+y] = hex;
         }
      }
      createMap(pixels);
      return out;
   }
   
   public static void createMap(String[] pixels) {
      for(String s : pixels) {
         if(s.equals("000000")) {
            System.out.println("wall");
         }
         if(s.equals("06941a")) {
            System.out.println("grass");
         }
      }
   }
}
Offline nsigma
« Reply #1 - Posted 2012-08-29 16:05:50 »

You've got your width and height variables mixed up.  You should also be multiplying your y value by the width before adding the x - you're getting the NPE because you're missing loads of values out and allocating the same value lots of times instead.  Something like this should be what you're looking for (hopefully correct, not compiled).

1  
2  
3  
4  
5  
6  
7  
8  
9  
int width = level.getWidth();
int height = level.getHeight();
for(int y = 0; y < height; y++) {
  for(int x = 0; x < width; x++) {
    int rgb = level.getRGB(x, y);
    String hex = Integer.toHexString(rgb & 0x00ffffff);
    pixels[(y * width) + x] = hex;
  }
}


Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2012-08-29 16:10:58 »

I also recommend that you use a 2 Dimensional array.  It's essentially an array of arrays.

1  
2  
int width = 10, height = 5;
String[][] myArray = new String[width][height];


Then, when you want to retrieve a String, you can loop through it like...

1  
2  
3  
4  
5  
6  
7  
for(int x=0; x<width; x++)
{
    for(int y=0; y<height; y++)
    {
        String element = myArray[x][y];      // You've accessed a particular string in the 2D Array.  Do whatever you want with it!
    }
}


Oh, one last thing...

pixels[x+y] = blah blah blah...  Is unsafe.

If x is 5 and y is 10, you get pixels[15] = blah blah blah.
If x is 10 and y is 5, you STILL get pixels[15] = blah blah blah.

It won't guarantee that you'll be writing to an empty slot in the array.  2D arrays are the best!

Did you know that 90% of statistics are wrong?
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.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!