图像点击事件JAVA [英] Image click event JAVA

查看:221
本文介绍了图像点击事件JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我希望你能帮我这个。



我想在屏幕上显示图像并为该图像设置事件处理程序。问题是我只希望事件处理程序能够处理您可以看到的图像部分。



它将是一个透明背景的PNG。让我们以球为例。我只希望事件处理程序在用户点击球时触发,而不是在他点击球旁边时触发。



我知道我可以围绕球创建一个区域并添加事件处理程序,但这将需要永远,因为我的图像是有趣的形状和大小,我有一个他们很多。



有没有办法让这个工作?

解决方案

如何编写MouseListener [ ^ ]



这也适合图像,因为也只是一个组件。





当检测到图像中的某个区域时,它需要有一个可以识别的东西它。

这可能是某个鼠标位置,该位置的某个颜色值,或者正如您已经提到的那样,是一个定义如此的叠加。



你可能会得到颜色值。因此,如果您想一想,鼠标事件发生在图像中的图像中设置了哪种颜色...



玩得开心!


< blockquote>感谢TorstenH指出我正确的方向。



如果有人在这一天有需要,我以为我会发布我的代码。请记住,这只是来自我的测试应用程序,如果你想使用它需要一些tweeking。这是放在JPanel的鼠标单击事件中。



 BufferedImage img = null; 

尝试
{
img = ImageIO.read( new 文件( res / 1.png));
}
catch (IOException e1)
{
e1.printStackTrace();
}

int packedInt = 0 ;
int ha = 1 ;
packedInt = img.getRGB(e.getX(),e.​​getY());
颜色= 颜色(packedInt,true);

if (color.getTransparency()== 1
{
尝试
{
如果(ha == < span class =code-digit> 0 )
{
img = ImageIO.read( new 文件( res / 1.png));
lblNewLabel.setIcon( new ImageIcon( RES / 1.png));
ha = 1 ;
}
else
{
img = ImageIO.read( new 文件( res / 2.png));
lblNewLabel.setIcon( new ImageIcon( RES / 2.png));
ha = 0 ;
}
}
catch (例外情况)
{

}

}


Hi all. I hope you can help me with this one.

I want to display an image on screen and have a event handler for that image. The problem is that I only want the event handler to work on the part of the image that you can see.

It will be a PNG with a transparent background. Lets use a ball as an example. I only want the event handler to trigger when the user clicks on the ball and not when he clicks right next to the ball.

I know I can create a region around the ball and add the event handler to that but that will take forever since my images is funny shapes and sizes and I have a lot of them.

Is there any way to make this work?

解决方案

How to write a MouseListener[^]

That also suits an image, as that is also just a component.


When detecting a certain area in an image, it needs to have a defined thingy on which one can recognize it.
That might be a certain mouse position, a certain color value at that position or, as you already mentioned, an overlay which defines so.

You might get along with the color value. So if you figure, which color is set in the image at the position the mouse event happens...

Have fun!


Thanks TorstenH for pointing me in the right direction.

I thought I'd post my code if anyone ells needs this one day. Please keep in mind this is only from my test app and it needs some tweeking if you want to use it. This is placed in the mouse click event of the JPanel.

BufferedImage img = null;
		    	
try
{
	img = ImageIO.read(new File("res/1.png"));
}
catch (IOException e1)
{
	e1.printStackTrace();
}
				
int packedInt = 0;
int ha = 1;
packedInt = img.getRGB(e.getX(), e.getY());
Color color = new Color(packedInt, true);
		    	
if (color.getTransparency() == 1)
{
	try
	{
		if (ha == 0)
		{
			img = ImageIO.read(new File("res/1.png"));
			lblNewLabel.setIcon(new ImageIcon("res/1.png"));
			ha = 1;
		}
		else
		{
			img = ImageIO.read(new File("res/2.png"));
			lblNewLabel.setIcon(new ImageIcon("res/2.png"));
			ha = 0;
		}
	}
	catch (Exception ex)
	{
		    			
	}
		    		
}


这篇关于图像点击事件JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆