Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  Reading an Image  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-09-08 23:18:38 »

I somehow completely forgot this even though I did it just a few months ago. Don't worry I have researched this however I haven't found anything that seems to relate to what I want to do.
What I need to do is read an image to load a map. Let's say it's a 15x15 image:
1 = black
0 = white

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


This would be used to load a simple 2d map into my game. How would I read this image and determine if the pixels are red/white/black? I'm sure I could use a buffered image and it's raster but I don't quite know how to read the raster.
On a nice little side note I did my Ordeal for Boy Scouts Order of the Arrow this weekend Smiley (if any of you know what that is Wink ).
Thanks,
-cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline tyeeeee1
« Reply #1 - Posted 2013-09-09 00:46:55 »

Read the RGB (or was it ARGB?) values at a specific point and, depen ding on the value, do something. I can't remember exactly how to do this but it is how I did it.
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2013-09-09 00:48:04 »

Without knowing more about your libraries, e.g. file handling, can't be more specific

But what reference is from Notch's Metagun, he used this approach for his level in a ludumdare some years ago


https://github.com/alexnick/metagun/blob/master/src/com/mojang/metagun/level/Level.java

specifically look at pixels, the above Level.java class
and
Art.level.getRGB(xo * 31, yo * 23, 32, 24, pixels, 0, 32);
In this case level is a bufferedImage
as seen here
https://github.com/alexnick/metagun/blob/master/src/com/mojang/metagun/Art.java


he uses the rgb value for determine wall or not wall. to make map

He then uses other factors for other variables as well, quite useful/neat.


Given a bufferedImage
load it up into the file

then have
int[] pixels = new int[15*15]; //15*15 in your case, cause thats size of image?
then bufferedImage.getRGB( startX, startY, width, height, pixelsOut, offset, scansize);
you can also just get the x,y of something specific too
read up on http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

then your pixels has your data, and can just sweep thru it using x + y*w; // where w is 15 in your case.

and check if pixel == 0xFFFFFF  or 0x000000  for white and black.

"Experience is what you get when you did not get what you wanted"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kpars

JGO Wizard


Medals: 77
Projects: 4
Exp: 3 years


Extreme Typist.


« Reply #3 - Posted 2013-09-09 20:11:32 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
try {
    BufferedImage img = ImageIO.read(new File("res/lvl.png"));

    int w = img.getWidth();
    int h = img.getHeight();

    int[] pixels = new int[w * h];
    img.getRGB(0, 0, w, h, pixels, 0, w);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int col = pixels[(x + y * w)] & 0xFFFFFF;

            if (col == 0xFF66FF) /* Do something */ ;
        }
    }
} catch (IOException e) {
    e.printStackTrace();
    System.exit(0);
}


This should give you an idea of what to do.

Offline Slyth2727
« Reply #4 - Posted 2013-09-09 20:18:37 »

I'm sorry I figured it out myself yesterday and forgot to post Sad sorry about that!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

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

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

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

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

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

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

BurntPizza (49 views)
2014-08-09 21:09:32
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!