import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.image.*; public class Applet1 extends Applet { Image image, image2; boolean halfFlg; boolean isStandalone = false; Button load = new Button(); Button button2 = new Button(); Button half = new Button(); //アプレットの構築 public Applet1() { } //アプレットの初期化 public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //コンポーネントの初期化 private void jbInit() throws Exception { load.setLabel("load"); load.setBounds(new Rectangle(15, 150, 80, 30)); load.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { load_actionPerformed(e); } }); this.setLayout(null); button2.setLabel("emboss"); button2.setBounds(new Rectangle(100, 150, 80, 30)); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); half.setLabel("1/2"); half.setBounds(new Rectangle(200, 150, 60, 30)); half.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { half_actionPerformed(e); } }); this.add(button2, null); this.add(load, null); this.add(half, null); } public void paint(Graphics g) { if(image != null ){ int iwidth=image.getWidth(this); int iheight=image.getHeight(this); //System.out.println("size="+iwidth+" "+iheight); if( !halfFlg) g.drawImage(image, 0, 0, this); else g.drawImage(image, 0, 0, iwidth/2,iheight/2,this); } } void load_actionPerformed(ActionEvent e) { //ファイルを読みpixels[]に記録する halfFlg=false; image = getImage(getDocumentBase(), "ccad.gif"); //System.out.println("load="+image.getWidth(this)+" "+image.getHeight(this)); repaint(); } void button2_actionPerformed(ActionEvent e) { halfFlg=false; int iwidth=image.getWidth(this); int iheight=image.getHeight(this); //画素の配列 int pixels[] = new int[iwidth * iheight]; //ファイルを読みpixels[]に記録する // System.out.println("edge="+image.getWidth(this)+" "+image.getHeight(this)); PixelGrabber pg = new PixelGrabber(image, 0, 0, iwidth, iheight,pixels, 0, iwidth); try { pg.grabPixels(); } catch (InterruptedException ev) {} //色別に微分処理をする for (int y = 2; y < iheight-1; y++){ for (int x = 2; x < iwidth-1; x++){ int red = ((pixels[(y + 1) * iwidth + x + 1] & 0xFF) - (pixels[y * iwidth + x] & 0xFF)) + 128; red = normal(red); int green = (((pixels[(y + 1) * iwidth + x + 1] & 0xFF00) /0x100) % 0x100 - ((pixels[y *iwidth + x] & 0xFF00) /0x100) % 0x100) + 128; green = normal(green); int blue = (((pixels[(y + 1) * iwidth + x + 1] &0xFF0000) / 0x10000) % 0x100 - ((pixels[y * iwidth + x] &0xFF0000) / 0x10000) % 0x100) + 128; blue = normal(blue); //灰色にする int avg = (red + green + blue) / 3; pixels[y * iwidth + x] = (0xff000000 | avg << 16 | avg << 8 | avg); } } //表示用imageにする //image2 = createImage(new MemoryImageSource(248, 248, pixels, 0 ,248)); image = createImage(new MemoryImageSource(iwidth, iheight, pixels, 0 ,iwidth)); repaint(); } int normal(int col){//値を0..255の範囲にする if(col<0) return 0; if(col>255) return 255; return col; } void half_actionPerformed(ActionEvent e) { halfFlg=true; repaint(); } }