There are also many people who ask this question, take it out of the previous code to organize the document, I hope to help you learn the picture! The main use is the java.awt.image. * Packet, inherit the RGBImageFilter class, modify the pixels of the picture, and apply applet as an example: 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 im = null; ImageFilter imgf = null; FilteredImageSource fis = null; public void init () {img = this.getImage (this.getCodeBase (), "d. "); mt = new mediatracker (this); mt.addimage (IMG, 0); try {mt.waitForll (0);} catch (exception ex) {system.rr.println (ex.Tostring ()); } Im = this.createImage (100, 100); // Establish a new picture for entering text so that transparent processing Graphics G2 = im. Maxics (); g2.setfont (New Font ", font. Bold, 15)); g2.drawstring ("translucent text", 10, 50); imgf = new myimage (100, 100, 100); // Call the custom class to make object construct fis = new filteredImagesource (imga ", IMGF ); // The source of the image (image producer) is filtered, constructing the FilteredImagesource object instance imagence = this.createImage (fis); // Generate image} via the FilteredImagesource instance
Public void Paint (Graphics G) {g.drawImage (IMG, 0, 0, this); // Draw picture g.drawImage (IM, 100, 100, this); // Add translucent text}}
class myImage extends RGBImageFilter {// abstract class is a subclass of RGBImageFilter ImageFilter, inheritance processing int ARGB it implements the image width = 0; int height = 0; int alpha = 0; public myImage (int width, int height, int alpha ) {// constructor, is used to receive the size of the image that needs to be filtered image, and transparency this.canfilterindexcolormodel = true; // transparentImageFilter class inherits from RGBIMAGEFILTER, its constructor requires the width and height of the original image. This class implements the FilterRGB abstract function. In the default mode, the function is introduced in the form of the pixels identified by the X, Y, and the programmer returns the new Argb value of the pixel after a certain program logic. Width = width; this.height = height; this.alpha = alpha;}