Java - how to make an image represent which tile to draw in a game? -



Java - how to make an image represent which tile to draw in a game? -

i trying figure out how create game draw tile in specific spot using image represent each spot. if pixel of image color reddish specified picture(tile) draw in game, , each pixel greenish stood different specified image. have seen people create games dont know how , dont know name it.

if need more info can seek explain want more. please help?

that may slower in long run. recommend utilize byte array represent tiles, i.e. byte[width][height]. faster, easier manage , easier extend spritedata[width][height] if single byte not supply plenty info anymore.

however, if insist on using image store game data, can utilize following:

file file = new file("mygamedata.jpg"); bufferedimage image = imageio.read(file); // getting pixel color @ position x, y (width, height) int colour = image.getrgb(x ,y); int reddish = (colour & 0x00ff0000) >> 16; int greenish = (colour & 0x0000ff00) >> 8; int bluish = colour & 0x000000ff; system.out.println("red colour component = " + red); system.out.println("green colour component = " + green); system.out.println("blue colour component = " + blue);

each component in range (0...255) , can utilize determine right tile, i.e.

graphics2d gfx = (graphics2d) offscreenimage.getimage(); if (red == 120 && greenish == 0 && bluish == 0) { gc.drawimage(tile[whichtileyouwantforred], x, y, null); // x , y pixel read. }

alternatively, can skip extracting components altogether, , utilize colour, i.e.

if (colour == 0x00ff0000) { gc.drawimage(tile[whichtileyouwantforred], x, y, null); // x , y pixel read. }

(which faster anyway , want.)

java image map 2d tile

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -