troubleshooting Question

Set and get pixel class.

Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland asked on
5 Comments1 Solution556 ViewsLast Modified:

I'm trying to create a simple class that I can initialise with an image, perform set and get operations on a per pixel basis then call another method and get the new image. I've got something along the lines of:

      class Pixelator{
            private Image image;
            private int rows;
            private int columns;
            private int pixels[];
            private PixelGrabber pixelGrabber;
            public boolean setImage(Image aImage){
                  pixels=new int[rows*columns];
                  pixelGrabber=new PixelGrabber(image, 0, 0, columns, rows, pixels, 0, columns);
                  try{ pixelGrabber.grabPixels(); }catch(Exception e){return false;}
                  return true;
            public Image getImage(){
                  return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(columns, rows, pixels, 0, columns));
            public void setPixel(int x, int y, int red, int green, int blue){
                  int singlePix[]=new int[1];
                  pixelGrabber.setPixels(1, 1, 1, 1, pixelGrabber.getColorModel(), singlePix, 0, 0);
            public int getPixelRed(int x, int y){
                  int singlePix=pixels[y*columns+x];
                  return (singlePix>>16)&0xff;
            public int getPixelGreen(int x, int y){
                  int singlePix=pixels[y*columns+x];
                  return (singlePix>>8)&0xff;
            public int getPixelBlue(int x, int y){
                  int singlePix=pixels[y*columns+x];
                  return (singlePix)&0xff;

However it doesn't seem to be working. Can anybody please have a play around and see what's up with it?

Much appreciated!

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros