Drawina a hexagonal-tile map

Hello,

I´m doing a Battletech game as part of a university project. The GUI part is more or less done, but I´m stuck in drawing the board. I thought about using a JPanel and rewritting the paintComponent method.

The board is formed by hexagonal tiles. I have all tiles stored in png files (28x32), and I have read that I need to aply a mask to the tiles to draw them (to take out the parts of the rectangle that aren´t the hexagon). But I´m completely lost on how to do this.

Any ideas, tutorials,…? Thanks in advance. Greetings,

Vicente

can’t you simply make the images transparent?

GameDev have a good set of articles on isometric and hexagonal tile maps. You might find some useful stuff there:

http://www.gamedev.net/reference/list.asp?categoryid=44

I´ve been reading gamedev articles. The thing is that I don´t know how to do an image transparent in java.

I create a BufferedImage, get a Graphics2d and draw the png tile in the BufferedImage, but I don´t know how to make the corners transparent when I start drawing all the tiles together.

And when I try to draw the images in the JPanel, if I use a for loop, nothing is drawn, but if I draw them one by one, they appear in the JPanel (and I don´t understand why the for doesn´t work). Greetings,

Vicente

Vincente: Use your favorite graphics tool to create PNG images with 8 bit alpha channel or GIF images with transparent color and Java will do handle them correctly.

Some time ago, I put together a simple hex map demo which might be helpful: http://www.3plus4.de/java/hexmap.html. The paint method isn’t optimal and generates too many Rectangle objects but I’m also using images to draw the hex fields.

Hey sma, thanks a lot for the link. I didn´t know that java handled correctly images with and alpha channel. I´m going to see your demo now. Greetings,

Vicente

You may want to check out my old hexmap code at
http://gjt.org/servlets/JCVSlet/list/gjt/org/freeciv/tile

At some point in the past this code was able to draw hex maps with quite reasonable speed (including partial updates/refreshes repainting only needed hexes, also handling overdraw for isometric units). I have updated it last time in 1999, so it could have detoriated since then… Maybe you will find it useful.

Thanks too abies. I´m learning a lot reading those codes - I´m totally new to java graphics programming, so everything is welcome.