by Dinesh Thakur Category: AWT and Applets

 

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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article