Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
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 406 times)
0 Members and 1 Guest are viewing this topic.
Offline Xenon

Senior Newbie





« Posted 2012-08-29 17: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

JGO Knight


Medals: 37



« Reply #1 - Posted 2012-08-29 18: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;
  }
}


Offline DrewLols

Junior Member


Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2012-08-29 18: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-17 21:29:12

alaslipknot (75 views)
2013-05-16 21:24:48

gouessej (107 views)
2013-05-16 00:53:38

gouessej (103 views)
2013-05-16 00:17:58

theagentd (114 views)
2013-05-15 15:01:13

theagentd (103 views)
2013-05-15 15:00:54

StreetDoggy (148 views)
2013-05-14 15:56:26

kutucuk (171 views)
2013-05-12 17:10:36

kutucuk (169 views)
2013-05-12 15:36:09

UnluckyDevil (178 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.164 seconds with 21 queries.