Java Picture Color Area Distinguishing

Go To StackoverFlow.com

-1

I have this picture and I wish to be able to read each individual picture, load it up into a paint method and add Mouse Listeners to each spot of color but not any of the black background. I do not wish to include ANY of the black background as a "button" and only have the colored spots have mouselisteners of their own so I can distinguish which color spot I have pressed. Does anyone have any ideas? Thanks!

2012-04-04 00:31
by MrDrProfessorTyler
Have you considered the Robot class? You can add the point to the corresponding LinkedList and then upon mouse click you can find the list that contains the point then perform an action upon that - NoName 2012-04-04 00:33
Would the Robot class allow me to store every value in the area of each colored area so I can test it - MrDrProfessorTyler 2012-04-04 00:35
The robot class can scan for a color at a point, from there: if it is red, you add the point to a red list. et - NoName 2012-04-04 00:36
I can't see using the Robot as any great help here. Loading the image file into java as an Image would allow you to do the same thing as has been recommended above. The biggest problem you have is in defining your color areas, and where your background is. If you can slice the picture into regions, you can add just 1 mouselistener and find the correct action by region using Shape.contain - ControlAltDel 2012-04-04 00:38
Ok but I need to know what color the point is and what group of red also. Sorry if that was unclear in the question. Is there any way to detect that there is black inbetween two groups of red and therefore make them two different "buttons" - MrDrProfessorTyler 2012-04-04 00:39
@user1291492 - How could I go about slicing the picture into regions based on color so I can get each area of color alone - MrDrProfessorTyler 2012-04-04 00:41


1

I suppose you could approach it this way:

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.
       }
   }
}

I've never attempted something like this, nor have I tested the above method, but to me it seems like it should work.

Why can't you just duplicate the picture with JButtons and JPanels and simplify your life?

2012-04-04 00:52
by Jeffrey
Well I need to get a user to edit a picture file to their layout preference which could be anything so I need to be able to do this but its a good idea I'll try it thanks - MrDrProfessorTyler 2012-04-04 01:02


1

The mouseListener returns a location, so I would use that location to inspect the image at the corresponding pixel, then branch to do the required action. If the pixel turns out to be black, you simple do nothing.

The image can be inspected via a BufferedImage object and a Raster.

Alternatively, one could inspect the image via BufferedImage and a Raster, and create corresponding Objects for each color square located, printing and handling each one separately.

2012-04-04 01:19
by Phil Freihofner