by Dinesh Thakur

 

import java.awt.*;

import java.applet.*;
import java.awt.image.*;
public class JavaExampleGrayScaleInApplet extends Applet
  {
    Image img,img1;
    public void init()
     {
        img = getImage(getDocumentBase(),"Koala.jpg");
        int pxls[] = new int[248 * 248];
        PixelGrabber Pxlgrbr = new PixelGrabber(img, 0, 0, 248, 248, pxls, 0, 248);
        try
           {
               Pxlgrbr.grabPixels();
           }
               catch (InterruptedException e1) {}
               for (int loop_indx = 0; loop_indx < 248 * 248; loop_indx++)
               {
                       int p1 = pxls[loop_indx];
                       int Red = (0xff & (p1 >> 16));
                       int Green = (0xff & (p1 >> 8));
                       int Blue = (0xff & p1);
                       int Avg = (int) ((Red + Green + Blue) / 3);
                       pxls[loop_indx] = (0xff000000 | Avg << 16 | Avg << 8 | Avg);
                   }
                       img1 = createImage(new MemoryImageSource(248, 248, pxls, 0 , 248));
      }
                       public void paint(Graphics gr)
                       {
                          gr.drawImage(img1,10,10,this);
                        }
  }
/*<APPLET CODE=JavaExampleGrayScaleInApplet.class WIDTH=300 HEIGHT=300 ></APPLET> */
GrayScale in Java Example