File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Colour bounds method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Colour bounds method" Watch "Colour bounds method" New topic
Author

Colour bounds method

R Jay
Greenhorn

Joined: Apr 13, 2010
Posts: 2
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

Joined: Sep 14, 2009
Posts: 1732
    
  12

Welcome to JavaRanch!

I think you might find good response in the GUI forum... I'll move this there


When you do things right, people won't be sure you've done anything at all.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
R Jay
Greenhorn

Joined: Apr 13, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19790
    
  20

And that's always a good thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Colour bounds method