试试Graphics2D中的这个类AffineTransform或者Graphics2D关联的类.
我的思路是如果前景是单色,用一些颜色的与或运算就可以了.如果是一个图片之类的东西,那么必须有一个地方可以统一处理输出前的转换,以前在AffineTransform处理过输出比例的问题,你可以看看Graphics2D其他的类.

解决方案 »

  1.   

    AffineTransform is not enough. It does not process Alpha or transparence 透明.I used raster to do this.
    If the image (.gif) has alpha channel, transform only pixels which alpha !=0.
    If the image (.jpg) has no alpha channel, it become a little more complex. If I know the image has white( or other) background, the only transfor pixels are not white.Process:
    Image src=..... //width,height
    BufferedImage proc= new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2=proc.createGraphics();
    g2.drawImage(src,width,height,null); //offscreen copy src to procRaster rSrc=proc.getRaster();
    ColorModel cm=proc.getColorModel(); float[][] matrix={  //alpha blend: src*0.75+dst*0.25, dst is a blue rect
       {0.75f,0,0,0,0}, //red
       {0,0.75f,0,0,0}, //green
       {0,0,0.75f,0,255*0.25f}, //blue, increase blue channel
       {0,0,0,0.75f,255*0.25f} //alpha
    }BandCombineOp op=new BandCombineOp(matrix, null){
       // override filter to process alpha channel 
       public WritableRaster filter(Raster src,
                                 WritableRaster dst){         ...//src.getPixel
            ...//getAlpha
            ...//if has alpha channel and alpha!=0 use matrix to transfor 
            ...//if no ahpha channel, and pixel color is not the background,  
               //do transform
            .. //dst.setPixel
         
           return dst;
       }
    }WritableRaster rDst=op.filter(rSrc,rSrc); //op.filter(rSrc,null) is the same
    BufferedImage dst=new BufferedImage(cm, rDst,false,null);
    //draw dst to the screen if selected.1.op.filter() refer to j2sdk source code, add the code to process alpha
    2.adjust 0.75f to other numbers to get the effect you want.