Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: davedes (posted 2013-02-18 17:10:14, viewed 226 times)

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   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66  
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;


public class ImgTiles {
   
   static enum Tile {
      
      GRASS(0xff00ff00), //green
      WATER(0xff0000ff), //blue
      LAVA(0xffff0000);  //red
      
      public final int argb;
      
      Tile(int argb) {
         this.argb = argb;
      }
      
      public static Tile fromColor(int argb) {
         for (Tile t : Tile.values())
            if (t.argb == argb)
               return t;
         return null;
      }
      
   }

   public static void main(String[] args) throws IOException {
      BufferedImage img = ImageIO.read(ImgTiles.class.getResource("map.png"));
      int width = img.getWidth();
      int height = img.getHeight();
      int[] pixels = img.getRGB(0, 0, width, height, null, 0, width);
      
      //our map, using 2D array for convenience
      Tile[][] map = new Tile[width][height];
      
      for (int i=0; i<width * height; i++) {
         //the ARGB packed integer
         int value = pixels[i];
         
         //determine tile from ARGB color
         Tile t = Tile.fromColor(value);
         
         //determine (x, y) position
         int y = i / width;
         int x = i - width*y;
         
         //place tile in our 2D grid for handy access
         map[x][y] = t;
         
         //if you wanted to use ARGB values for something...
         //this is how you decode them to integers in the range 0-255 
         int a = ((value & 0xff000000) >>> 24);
         int r = ((value & 0x00ff0000) >>> 16);
         int g = ((value & 0x0000ff00) >>> 8);
         int b = (value & 0x000000ff);

         //...
      }

      //now we can access our map like normal
      System.out.println(map[0][0]); //will print GRASS, LAVA, or WATER.. or null if the color did not match
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

SHC (23 views)
2014-11-25 12:00:59

SHC (21 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (67 views)
2014-11-07 19:57: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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!