2012-04-04 124 views
-1

我有this图片,我希望能够读取每个单独的图片,将其加载到绘图方法中,并将鼠标侦听器添加到每个颜色点,但不是任何黑色背景。我不希望将任何黑色背景作为“按钮”,只有彩色斑点有自己的鼠标侦听器,因此我可以区分我按下的是哪个颜色斑点。有没有人有任何想法?谢谢!Java图片颜色区域区别

+0

你考虑过机器人课吗?您可以将该点添加到相应的LinkedList中,然后在单击鼠标时可以找到包含该点的列表,然后对其执行操作。 – 2012-04-04 00:33:27

+0

机器人课程是否允许我在每个彩色区域的区域存储每个值,以便测试它? – MrDrProfessorTyler 2012-04-04 00:35:29

+0

机器人类可以从某处开始扫描颜色:如果它是红色,则将该点添加到红色列表中。等 – 2012-04-04 00:36:39

回答

1

我想你可以这样来解决:

List<Shape> buttons = ... 

for each pixel in the picture, top left to bottom right { 
    if the pixel is not black { 
     if the pixel is not already contained in one of the buttons { 
      iterate over every pixel towards the right until you reach a different color 
      iterate over every pixel towards the bottom until you reach a different color 

      // now you have the bounds of your button 
      // create a new Rectangle and add it to your list. 
     } 
    } 
} 

我从来没有尝试这样的事情,也没有我测试了上面的方法,但对我来说似乎应该工作。

为什么你不能复制图片JButton s和JPanel s并简化你的生活?

+0

嗯,我需要让用户编辑一个图片文件到他们的布局首选项,这可能是任何事情,所以我需要能够做到这一点,但它是一个好主意,我会试试看,谢谢。 – MrDrProfessorTyler 2012-04-04 01:02:45

1

mouseListener返回一个位置,所以我将使用该位置来检查相应像素处的图像,然后执行所需的操作。如果像素变成黑色,那么你简单的什么都不做。

可以通过BufferedImage对象和Raster检查图像。

或者,可以通过BufferedImage和Raster检查图像,并为每个颜色方块所在的位置创建相应的对象,分别打印和处理每个颜色。