简体   繁体   English

使用具有透明像素的图像的mouseListeners和JLabel

[英]mouseListeners and JLabels using images with transparent pixels

I'm willing to make a isometric game but I'm having hard time with the mouseListener. 我愿意做一个等距游戏,但在mouseListener上却遇到了困难。

I'm using Swing and make losanges by using square images with transparent pixels (GIF format). 我正在使用Swing并通过使用带有透明像素(GIF格式)的正方形图像制作losanges。

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. 问题是使losanges彼此接触会导致一个像素的透明像素位于另一个像素的顶部,这是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. 我很想知道确切地单击了哪个losange,但是当附近的losange的透明像素位于被单击的losange的顶部时,选择了错误的losange作为KeyEvent源。

Is there a way to have mouseListener not considering transparent pixels as part of the shape ? 有没有办法让mouseListener不将透明像素视为形状的一部分?

Thanks for reading. 谢谢阅读。

It's not clear 还不清楚

1) why the tiles have to overlap, or 1)为什么瓷砖必须重叠,或者

2) why you're using JLabels for the tiles 2)为什么要使用JLabels绘制图块

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. 有很多方法可以解决您遇到的问题,但是我只制作一个JComponent即可根据需要渲染图块,并且它是唯一的MouseListener。

As mentioned by Jonathan, I think you're using the wrong technology for the job. 正如乔纳森(Jonathan)所述,我认为您在使用错误的技术来完成这项工作。 I've had great success using a 2D graphics framework for software such as this. 对于这样的软件,我使用2D图形框架取得了巨大的成功。 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. 我知道您不想听这个,但是请考虑从适当的工具集开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM