Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Colour bounds method

 
R Jay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Was wondering if there is a comparable method in the Java API (maybe in Swing) that carries out the same function as this one found in ActionScript.

It's a method which determines a rectangular region that fully encloses all pixels of a specified color within a bitmap image; so if there are two red circles at opposite corners of an image then the rectangular region returned would cover the entire image.

I'm sure there is, but I've spent ages searching and can't seem to find anything.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I think you might find good response in the GUI forum... I'll move this there
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not aware that something like this exists, but it isn't that hard to write one yourself using BufferedImage. Use getWidth(), getHeight() and getRGB(int x, int y). In short, you iterate over every pixel, checking if the colour at that pixel matches the desired colour. You keep track of the minimum and maximum x and y values with a match. You can start these values like this:
None of these values fall within the image so if at the end you get that one of these values is still at the initial position you know that the colour did not occur at all in the image.
 
R Jay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

I have now written the method myself, but I thought there would be a more efficient one lurking somewhere in the API.

It turned out to be a fairly easy algorithm to implement -- at least now I understand it!
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that's always a good thing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic