Code example for FilteredImageSource

0
    /** 
     * Creates an image from an existing one by replacing the old color with the new color. 
     */ 
    public static Image createImage(Image i, Color oldColor, Color newColor) {
        MaskFilter filter = MaskFilter.getInstance(oldColor, newColor);
        ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
        Image image = Toolkit.getDefaultToolkit().createImage(prod);
        return image;
    } 
 
    /** 
     * Creates an image as negative of an existing one. It will basically replace the black color with white color. 
     */ 
    public static Image createNegativeImage(Image i) {
        return createImage(i, Color.black, Color.white);
    } 
 
    /** 
     * Constructs a MaskFilter object that filters color of image to another color Please note, you can also use {@link 
     * #getInstance(java.awt.Color, java.awt.Color)} to reuse the same instance of MaskFilter.