views:

12

answers:

1

For my project, I have to read each pixel in an image, perform a math operation on each pixel, then display the results of the modified pixel in an updated image. For this example, I took the square root of each pixel.

public void rootCalc(){//go through the rows and columns                            for(int x=grayscale_orig.getWidth() - 1; x >= 1; x--)
 {
  for (int y = grayscale_orig.getHeight() - 1; y >= 0; y--)
  {
      //this is the pixel array
      int[] rgb1 = getColors(grayscale_orig.getRGB(x, y));
      for(int g=0; g<rgb1.length;g++) 
      {   //take the square root of each pixel value
       v=255*(Math.sqrt(rgb1[g])/Math.sqrt(255));
//scale the squared value back into the 0-255 range and set the value
       grayscale.setRGB(x,y,(int)v);
      }
  }
 }}

My question is how I can set and display the modified pixels in the setRGB() code. When I insert an int -- (int)v, the image is all black. Do I have to change the int to a hexadecimal? I tried doing something like this: v=255*(Math.sqrt(rgb1[g]) / Math.sqrt(255)); String vstring=Double.toHexString(v); and wouldn't let me input a string in the setRGB(); =\

+1  A: 

There is no such thing as "a hexadecimal" - an int is just a number. We usually use decimal notation in our code and it's really just saved the binary system - however, it's just a number and you can't just "change it into a hexidecimal".

There is a problem with your code, though. You call setRGB once for the red component of the color, then once for green component of the color and then once again for the blue component (I suppose it's that order anyway). However, setRGB want a single integer to represent the whole color.

You can calculate such an integer by hand, but it's easier to make a Color object out of those compartments of the color and then to get the value to give to setGRB by calling getRGB on the Color.

Jasper