//小弟刚刚开始学习JAVA,经常碰到一些不明白的地方,特来向各位大侠请教 //这是教程上的一个例子,运行时显示不出处理过的图片,请问是哪个语句出问题了,谢谢!
import java.awt.*; import java.awt.event.*; import java.awt.image.*;
public class pixelgrabberl extends Frame{ int x,w,h; int[] pixels; Image im,im1;
//2.定义构造器,并添加窗口事件的监听 public pixelgrabberl(){ addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); //3.要构造器中参过MediaTracker装载图像 MediaTracker tracker=new MediaTracker(this); im=Toolkit.getDefaultToolkit().getImage("flower.jpg"); tracker.addImage(im,0); try{ tracker.waitForID(0); }catch(InterruptedException e){e.printStackTrace();} w=im.getWidth(this); h=im.getHeight(this); pixels=new int[w*h]; try{ PixelGrabber pg=new PixelGrabber(im,0,0,w,h,pixels,0,w); pg.grabPixels(); }catch(InterruptedException e){e.printStackTrace();} //4.对RGB值和Alpha值进行重新计算和赋值 ColorModel cm=ColorModel.getRGBdefault(); for(x=0;x<w*h;x++){ int alpha=100; int red=cm.getRed(pixels[x]); int green=cm.getGreen(pixels[x]); int blue=cm.getBlue(pixels[x]); pixels[x]=alpha<<24|red<<16|green<<8|blue; } /*for(int i=0;i<w*h;i++){ int alpha=cm.getAlpha(pixels[i]); int red=255-cm.getRed(pixels[i]); int green=255-cm.getGreen(pixels[i]); int blue=255-cm.getBlue(pixels[i]); pixels[i]=alpha<<24 | red<<16 | green<<8 |blue; }*/ //5.通过MemoryImageSource将数组的像素产生一个图像 ImageProducer ip=new MemoryImageSource(w,h,pixels,0,w); Image im1=createImage(ip); } //6.调用paint方法,分别显示两幅图像 public void paint(Graphics g){ g.drawImage(im,10,20,this); g.drawImage(im1,w+20,h+20,this); }
//7.产生main主方法,设置窗口大小,显示窗口 public static void main(String[] args){ pixelgrabberl f=new pixelgrabberl(); f.setSize(300,200); f.show(); } }