Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用图形2D执行多重合成效果_Java_Swing_Graphics - Fatal编程技术网

Java 如何使用图形2D执行多重合成效果

Java 如何使用图形2D执行多重合成效果,java,swing,graphics,Java,Swing,Graphics,我有两个不同的javax.swing.Icon对象,我想创建一个新图标,它是这两个对象的组合。我希望合成效果是一个倍增效果,类似于你在任何类似photoshop的图像编辑应用程序中选择该选项时所得到的效果。具体来说,每像素,如果您分别从图像1和图像中获得颜色Ca和Cb Ca=(Ra,Ga,Ba) Cb=(Rb、Gb、Bb) 我想要的输出是 Cc=(RaRb、GaGb、Ba*Bb) 我想动态地(实时地)完成这项工作,所以我只能使用Graphics2D操作。我看过AlphaComposite,但看不

我有两个不同的javax.swing.Icon对象,我想创建一个新图标,它是这两个对象的组合。我希望合成效果是一个倍增效果,类似于你在任何类似photoshop的图像编辑应用程序中选择该选项时所得到的效果。具体来说,每像素,如果您分别从图像1和图像中获得颜色Ca和Cb

Ca=(Ra,Ga,Ba) Cb=(Rb、Gb、Bb)

我想要的输出是

Cc=(RaRb、GaGb、Ba*Bb)


我想动态地(实时地)完成这项工作,所以我只能使用Graphics2D操作。我看过AlphaComposite,但看不出这是可以做到的。有人有什么想法吗?

因为这个问题的大多数答案都指向大量的非现场资源,所以我想我会在这里提供一个完整的实现。它本质上与您可能看到的大型混合模式库相同,但我去掉了抽象,只是为了实现特定的混合模式

有关代码的一些注释如下:

  • 只需使用
    g.setComposite(MultiplyComposite.Multiply)

  • MultiplyComposite
    类同时实现了
    Composite
    CompositeContext
    ,因为对于我的需要,这两者之间没有什么不同。如果您有一个可以调整参数或以不同方式应用混合的混合模式,则需要分别实现这两个类

  • 此代码仅适用于32位“整数”缓冲区。您需要修改此选项以使用16位颜色(5/6位而不是8位的移位和掩蔽)

  • 此代码使用最后一个字节作为alpha通道。您需要修改代码以处理没有alpha通道(24位)的数据

  • mixPixel
    方法中,向右移动8是一种快速除以256的方法。例如,
    (a*b)>>8
    而不是
    (a*b)/256

  • 像素从左到右一次处理一行。如果你有更多的内存,你可以修改它,一次处理整个缓冲区


public类MultiplyComposite实现了Composite、CompositeContext{
受保护的空白检查光栅(光栅r){
if(r.getSampleModel().getDataType()!=DataBuffer.TYPE\u INT){
抛出新的IllegalStateException(“预期的整数样本类型”);
}
}
@凌驾
公共void组合(光栅src、光栅dstIn、可写光栅dstOut){
检查光栅(src);
检查光栅(dstIn);
检查光栅(dstOut);
int width=Math.min(src.getWidth(),dstIn.getWidth());
int height=Math.min(src.getHeight(),dstIn.getHeight());
int x,y;
int[]srcPixels=新的int[宽度];
int[]像素=新的int[宽度];
对于(y=0;y<高度;y++){
getDataElements(0,y,宽度,1,srcPixels);
getDataElements(0,y,宽度,1,像素);
对于(x=0;x>8)&0xFF;
intyg=(y>>8)&0xFF;
int g=(xg*yg)/255;
int xr=(x>>16)&0xFF;
int yr=(y>>16)和0xFF;
int r=(xr*yr)/255;
int xa=(x>>24)&0xFF;
int ya=(y>>24)&0xFF;
int a=Math.min(255,xa+ya);

return(b)|(g)您已经看过java.awt.image的内容了吗?BufferedImageFilter和BufferedImageOp不合适有什么原因吗?
public class MultiplyComposite implements Composite, CompositeContext {
    protected void checkRaster(Raster r) {
        if (r.getSampleModel().getDataType() != DataBuffer.TYPE_INT) {
            throw new IllegalStateException("Expected integer sample type");
        }
    }

    @Override
    public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
        checkRaster(src);
        checkRaster(dstIn);
        checkRaster(dstOut);

        int width = Math.min(src.getWidth(), dstIn.getWidth());
        int height = Math.min(src.getHeight(), dstIn.getHeight());
        int x, y;
        int[] srcPixels = new int[width];
        int[] dstPixels = new int[width];

        for (y=0; y < height; y++) {
            src.getDataElements(0, y, width, 1, srcPixels);
            dstIn.getDataElements(0, y, width, 1, dstPixels);

            for (x=0; x < width; x++) {
                dstPixels[x] = mixPixel(srcPixels[x], dstPixels[x]);
            }

            dstOut.setDataElements(0, y, width, 1, dstPixels);
        }
    }

    private static int mixPixel(int x, int y) {
        int xb = (x) & 0xFF;
        int yb = (y) & 0xFF;
        int b = (xb * yb) / 255;

        int xg = (x >> 8) & 0xFF;
        int yg = (y >> 8) & 0xFF;
        int g = (xg * yg) / 255;

        int xr = (x >> 16) & 0xFF;
        int yr = (y >> 16) & 0xFF;
        int r = (xr * yr) / 255;

        int xa = (x >> 24) & 0xFF;
        int ya = (y >> 24) & 0xFF;
        int a = Math.min(255, xa + ya);

        return (b) | (g << 8) | (r << 16) | (a << 24);
    }


    @Override
    public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
        return this;
    }

    @Override
    public void dispose() {

    }

    public static final MultiplyComposite Multiply = new MultiplyComposite();

}