Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

ColourClustering

getting error for getRGB() and i dont know why?

package colourclustering;

import java.awt.Image;
import java.awt.Toolkit;
import java.util.HashMap;

/**
 *
 * @author u0207372
 */

public class ColourClusterTester {
    private HashMap hashMap;
    private Image img;
   
    /**
     * Creates a new instance of ColourClusterTester
     */
    public ColourClusterTester( String filename ) {
       
        hashMap = new HashMap();
        img = Toolkit.getDefaultToolkit().getImage( filename );
       
        for ( int i = 0; i < img.getWidth(null); i++ )
        {
            for ( int j = 0; j < img.getHeight(null); j++ )
            {
                int pixelFreq;
                int pixelValue = img.getRGB();
               
                hashMap.put( pixelValue, 1 );
            }
        }
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new ColourClusterTester( "H:\\MIOCReader\\ColourClustering\\ColourClustering\\src\\colourclustering\\test.gif" ) ;
    }
   
}

bascially what im trying to do is read image, get pixel value of each pixel and store value in hash map. along with the number of time that value appears but having some problems actually getting the RGB() for each pixel?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

 img = javax.imageio.ImageIO.read(new FileInputStream( filename ));
Avatar of ellandrd

ASKER

still getting red line under:

pixelValue = img.getRGB();
>> hashMap.put( pixelValue, 1 );

Why would you hard-code that value there unless you knew it to be unique?
because im not finished yet and have not added my if else statements.

that line shouldbe commented out but at th emoment by main concert si the getRGB() not working...
>> by main concert si the getRGB() not working...

OK. My first comment should fix that
Again to repeat myself - im still getting a red line under this line of code:

pixelValue = img.getRGB();

your first comment should but didnt fix it
>>private Image img;

Change to

private BufferedImage img;

and then

pixelValue = img.getRGB(i, j);
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> getRGB() is a method of Color not of Image
Well, at least the one wihtout parameters ;°)
now this line has red line under it...

 img = javax.imageio.ImageIO.read(new FileInputStream( filename ));
That's up to CEHJ, to solve that ;°)
Make sure you

import java.io.*;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And catch any exceptions thrown by creating the FileInputStream
>> bascially what im trying to do is read image, get pixel value of each pixel
Looks like you could use it very well
Have to go offline.
Goodnight to both of you
no, but by looking at the URL's contents its take me another 5 hours to add it to my code....

nobody gives straight answers on his site no more - everybody expects me to know what they know and i dont!
Your main problem was dealing with the default asynchronous loading of images
>>nobody gives straight answers on his site no more - everybody expects me to know what they know and i dont!

You've been coding Java long enough not to need spoon feeding by now ;-) You have all the pieces
yes but no every day, day in day out - im a web programmer and trying to learn java on the side and each time i try learning some the time between i last learned something and time i get to do some more - few wks or months later its like having to start over again!
Try to do a bit regularly. That'd happen to me if i didn't keep doing it ;-)
>>Try to do a bit regularly

I try - but with work and other commitments it is hard to keep on top if things!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where did this come out off? thank you!
:-)
ellandrd,

Your initial question was this:
>> getting error for getRGB() and i dont know why?

My answer was:
>> getRGB() is a method of Color not of Image
>> Well, at least the one wihtout parameters ;°)
I consider that as a comment that deserved to be awarded as an assisted answer.
Disappointed it isn't.

>> nobody gives straight answers on his site no more
Well, that WAS a rather straight answer I think.
> Your main problem was dealing with the default asynchronous loading of images

no it wasn't
i meant by giving "straight answers" was/is actually providing some constructive / helpful advice etc etc and not the one word or one sentence answers like i got a few times.  

im not saying it was all you who didnt give straight answers, but when you are trying to get some answers/ help outta people and in the process of been stressed - people who provide comments like:

img = javax.imageio.ImageIO.read(new FileInputStream( filename ));

for example, dont help especially with me struggling with Java each time i get a chance to learn it.

i didnt know how to use that code and so i didnt feel it was a straight answer and i was getting angry with the comments been made!
Curious to know why ..?
CEHJ,

zzynx was unhappy that i didnt split the points fair enough and asked me to be fair.

If you disagree please say.

@ellandrd

 
That's OK - just wondered
Thanx for being honest, ellandrd