I am due to start work on a 2D platform game in Java using Java2D, and am trying to devise a way to create a world. I have been reading up about this for the last few hours now, and as far as I can tell, a relatively effective way is to have a text file with a “matrix” of values in it, which is read in by the program in order to create the map (stored in a 2D array).
Now, my plan is to have multiple JComponents that display ImageIcons for the various textures in the world; the JComponent object would depend on the character in the given array index.
- Is there anything I may have overlooked?
- Will this schematic work with a background image, i.e. when there is a character that represents a blank space, will part of the background be shown?
Apologies if this seems like a lazy question, I can assure you it is not out of laziness. I am merely trying to plan this out before hacking code together.
Unless you have compelling reason to, having a different component for each tile is probably not a good way to go. Look into a Canvas and displaying loaded images at different offsets in it.
Example:
480×640 Canvas
128×16 image file(contains 8 16×16 tile images)
So your file has a bunch of numbers(characters etc.), we’ll say 0-7 for the 8 tiles in the image. The file has 30×40 numbers, laid out in a grid the same as the canvas. So
1 2 1 3 4 8 2…
…
And to display the code ends up something like(not tested, based on docs)
Which basically maps the number to your tile image, then puts that tile image into the right spot in the canvas(x,y) coordinates * size of tile.