views:

148

answers:

3

Hi,

I'm willing to make a isometric game but I'm having hard time with the mouseListener.

I'm using Swing and make losanges by using square images with transparent pixels (GIF format).

The problem is that making losanges touch each other edge means having the transparent pixels of one on the top of the others, which is a problem with the mouseListener.

I'm willing to know exactly which losange was clicked on, but as the transparent pixels of the nearby losange get on the top of the one that was clicked on, the wrong losange is selected as the KeyEvent source.

Is there a way to have mouseListener not considering transparent pixels as part of the shape ?

Thanks for reading.

+1  A: 

It's not clear

1) why the tiles have to overlap, or

2) why you're using JLabels for the tiles

There's are many ways to solve the problem you're having, but I'd just make a single JComponent that renders the tiles as needed, and is the sole MouseListener.

Jonathan Feinberg
1) because each JLabel is considered a square, but have to display a losange shaped image and every losange's edges are connected to another similar shaped image. Well, if there is another solution, I would be happy to learn it.2) Hum... Probably because I'm new to Java and that JLabels seems like the easier way to print images to me.Every tile may have a different image, but they are all losange shaped.However, I need to be able to use some kind of z-index to add "objects" in my game. For now, I work with with a JLayeredPane().
Cheshire
+2  A: 

As mentioned by Jonathan, I think you're using the wrong technology for the job. I've had great success using a 2D graphics framework for software such as this. In my case I'm a big fan of Piccolo. I know you don't want to hear this, but consider starting over with the appropriate toolset.

Jason Nichols
A: 

As Jonathan Feinberg suggests, extending JComponent (or a subclass) is an appealing approach. This simple game is nothing more than a rectangular grid of components based on JToggleButton. Implementing a custom LayoutManager will allow you to "overlap" components as required. You can draw your lozenge shape as a Polygon and override getMousePosition() to use the contains() method, shown here.

trashgod