import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.image.*; public class applet6 extends Applet { MediaTracker mt; Image img = null; Image img2 = null; Image im = null; Image im2 = null; Image im3 = null; ImageFilter imgf = null; FilteredImageSource fis = null; public void init() { this.setBackground(new Color(244,223,223)); img = this.getImage(this.getCodeBase(), "123.jpg"); img2 = this.getImage(this.getCodeBase(), "234.jpg"); mt = new MediaTracker(this); /**/ int w = 100; int h = 100; int alpha = 0; int pix[] = new int[w * h]; int index = 0; for (int y = 0; y < h; y++) { int red = (y * 255) / (h - 1); for (int x = 0; x < w; x++) { int blue = (x * 255) / (w - 1); pix[index++] = alpha << 24 | (255 << 24) | (red << 16) | blue; } } im3 = createImage(new MemoryImageSource(w, h, pix, 0, w)); /**/ mt.addImage(img, 0); try { mt.waitForAll(0); } catch (Exception ex) { System.err.println(ex.toString()); } im = this.createImage(100, 100);// 建立新的图片,用于输入文字,以便接下来进行透明处理 Graphics g2 = im.getGraphics(); g2.setFont(new Font("宋体", Font.BOLD, 15)); g2.drawString("半透明文字 可耕地可耕地可耕地", 1, 30); imgf = new myImage(100, 100, 120);// 调用自定义类进行对象构造 fis = new FilteredImageSource(img2.getSource(), imgf);// 对图象的源(图象生产者)进行过滤处理,构造出FilteredImageSource对象实例 im2 = this.createImage(fis);// 通过FilteredImageSource实例生成Image // createImage(new MemoryImageSource(this.m, this.n, new DirectColorModel(24, 16711680, 65280, 255), localObject4, 0, this.m)); imgf = new myImage(100, 100, 100);// 调用自定义类进行对象构造 fis = new FilteredImageSource(im.getSource(), imgf);// 对图象的源(图象生产者)进行过滤处理,构造出FilteredImageSource对象实例 im = this.createImage(fis);// 通过FilteredImageSource实例生成Image imgf = new myImage(100, 100, 100);// 调用自定义类进行对象构造 fis = new FilteredImageSource(im3.getSource(), imgf);// 对图象的源(图象生产者)进行过滤处理,构造出FilteredImageSource对象实例 im3 = this.createImage(fis);// 通过FilteredImageSource实例生成Image } public void paint(Graphics g) { g.drawImage(img, 0, 0, this);// 画出图片 // g.drawImage(im, 0, 0, this);// 添加半透明文字 g.drawImage(im2, 0, 0, this);// 添加半透明图片 g.drawImage(im3, 140, 0, this);// 添加半透明图片 } public void destroy(){ System.gc(); } }
import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.awt.image.MemoryImageSource; import java.util.Random; public class appletImg extends Applet { int pixels[]; MemoryImageSource source; Image image = null; public void init() { int width = 50; int height = 50; int size = width * height; pixels = new int[size]; int value = getBackground().getRGB(); for (int i = 0; i < size; i++) { pixels[i] = value; } source = new MemoryImageSource(width, height, pixels, 0, width); source.setAnimated(true); image = createImage(source); } public void run() { Thread me = Thread.currentThread( ); me.setPriority(Thread.MIN_PRIORITY); while (true) { try { Thread.sleep(10); } catch( InterruptedException e ) { return; } // Modify the values in the pixels array at (x, y, w, h) // Send the new data to the interested ImageConsumers source.newPixels(); } } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); } }