1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.image.*; 11 12 23 public class GrayFilter extends RGBImageFilter { 24 private boolean brighter; 25 private int percent; 26 27 30 public static Image createDisabledImage (Image i) { 31 GrayFilter filter = new GrayFilter (true, 50); 32 ImageProducer prod = new FilteredImageSource(i.getSource(), filter); 33 Image grayImage = Toolkit.getDefaultToolkit().createImage(prod); 34 return grayImage; 35 } 36 37 46 public GrayFilter(boolean b, int p) { 47 brighter = b; 48 percent = p; 49 50 canFilterIndexColorModel = true; 55 } 56 57 60 public int filterRGB(int x, int y, int rgb) { 61 int gray = (int)((0.30 * ((rgb >> 16) & 0xff) + 63 0.59 * ((rgb >> 8) & 0xff) + 64 0.11 * (rgb & 0xff)) / 3); 65 66 if (brighter) { 67 gray = (255 - ((255 - gray) * (100 - percent) / 100)); 68 } else { 69 gray = (gray * (100 - percent) / 100); 70 } 71 72 if (gray < 0) gray = 0; 73 if (gray > 255) gray = 255; 74 return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0); 75 } 76 } 77 78 | Popular Tags |