java applet例子---图片像素处理
package applet;import java.applet.Applet;import java.awt.Event;import java.awt.Graphics;import java.awt.Image;import java.awt.image.FilteredImageSource;import java.awt.image.ImageProducer;import java.awt.image.RGBImageFilter;import java.net.MalformedURLException;import java.net.URL;//实现图像色素过滤功能public class PixelProcess extends Applet {Image oldImage;Image currentImage;int i = 0;public void init() {try {oldImage = this.getImage(new URL("file:///C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/可爱小狗.jpg"));} catch (MalformedURLException e) {e.printStackTrace();}imageProcess();}public boolean mouseDown(Event e,int x,int y){ //重载父类方法i++;imageProcess();repaint();return true;}public void paint(Graphics g){g.drawImage(currentImage, 0, 0, this);}public synchronized boolean imageUpdate(Image m,int f,int x,int y,int w,int h){repaint(100);return true;}public synchronized void imageProcess() {RGBImageFilter imageFilter;if (i % 2 == 0) {imageFilter = new NonFilter();} else {imageFilter = new RGBFilter();}ImageProducer imageProducer = oldImage.getSource();imageProducer = new FilteredImageSource(imageProducer, imageFilter);currentImage = createImage(imageProducer);}}// 不做渲染class NonFilter extends RGBImageFilter {public int filterRGB(int x, int y, int rgb) {return rgb;}}class RGBFilter extends RGBImageFilter {public int filterRGB(int x, int y, int rgb) {return (rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16)| ((rgb & 0xff) << 16);}}
执行结果:
初始图片:

点击applet后

再次点击就会状态交替出现