如何在Java中生成透明GIF?

如何在Java中生成透明GIF?,java,image,gif,Java,Image,Gif,下面的代码生成一个半红半蓝的GIF图像。 我怎样才能得到一个半红半透明的 我尝试过使用IndexColorModel构造函数,该构造函数将透明像素索引作为参数,并在调用BufferedImage构造函数时将图像类型更改为image\u type\u ARGB,但没有任何效果 int pixels[] = new int[90000]; for (int x = 0; x < 300; x++) { for (int y = 0; y < 300; y+

下面的代码生成一个半红半蓝的GIF图像。 我怎样才能得到一个半红半透明的

我尝试过使用
IndexColorModel
构造函数,该构造函数将透明像素索引作为参数,并在调用
BufferedImage
构造函数时将图像类型更改为
image\u type\u ARGB
,但没有任何效果

    int pixels[] = new int[90000];
    for (int x = 0; x < 300; x++) {
        for (int y = 0; y < 300; y++) {
            pixels[(300 * y) + x] = (x < y) ? 1 : 0;
        }
    }

    Color oneColor = Color.red;
    Color anotherColor = Color.blue;

    byte[] redMap = {(byte) (oneColor.getRed()), (byte) (anotherColor.getRed())};
    byte[] greenMap = {(byte) (oneColor.getGreen()), (byte) (anotherColor.getGreen())};
    byte[] blueMap = {(byte) (oneColor.getBlue()), (byte) (anotherColor.getBlue())};

    IndexColorModel colorModel = new IndexColorModel(1, 2, redMap, greenMap, blueMap);

    MemoryImageSource mis = new MemoryImageSource(300, 300, colorModel, pixels, 0, 300);

    Image image = Toolkit.getDefaultToolkit().createImage(mis);
    BufferedImage bufferedImage = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(image, 0, 0, null);

    try {
        ImageIO.write(bufferedImage, "gif", new File("example.gif"));
    } catch (IOException e) {
        e.printStackTrace();
    }
int像素[]=newint[90000];
对于(int x=0;x<300;x++){
对于(int y=0;y<300;y++){
像素[(300*y)+x]=(x
根据我的经验,我用alpha更改了颜色的透明度。例如,透明红色如下所示:

Color transparentred = new Color (255, 0, 0, alpha);

可能尝试为红图、蓝图、绿图设置alpha,结果显示
缓冲图像。TYPE_BYTE_index
更适合这种情况。此代码实现以下功能:

    Color oneColor = Color.blue;
    Color anotherColor = Color.red;

    byte[] redMap = {(byte) (oneColor.getRed()), (byte) (anotherColor.getRed())};
    byte[] greenMap = {(byte) (oneColor.getGreen()), (byte) (anotherColor.getGreen())};
    byte[] blueMap = {(byte) (oneColor.getBlue()), (byte) (anotherColor.getBlue())};

    IndexColorModel colorModel = new IndexColorModel(1, 2, redMap, greenMap, blueMap, 0);

    int transparency = colorModel.getTransparency();
    int transparentPixel = colorModel.getTransparentPixel();
    System.out.println("colorModel.getTransparency(): " + transparency);
    System.out.println("colorModel.getTransparentPixel(): " + transparentPixel);

    BufferedImage bufferedImage = new BufferedImage(300, 300, BufferedImage.TYPE_BYTE_INDEXED, colorModel);

    WritableRaster writableRaster = bufferedImage.getRaster();

    for (int x = 0; x < 300; x++) {
        for (int y = 0; y < 300; y++) {
            int[] fill = new int[1]; // A large block...
            Arrays.fill(fill, (x < y) ? 0 : 1);  // ..  filled with one of the 7 first colors in the LUT.
            writableRaster.setSamples(x, y, 1, 1, 0, fill);
        }
    }
Color oneColor=Color.blue;
另一种颜色=Color.red;
byte[]redMap={(byte)(oneColor.getRed()),(byte)(anotherColor.getRed())};
byte[]greenMap={(byte)(oneColor.getGreen()),(byte)(anotherColor.getGreen())};
byte[]blueMap={(byte)(oneColor.getBlue()),(byte)(anotherColor.getBlue())};
IndexColorModel colorModel=新的IndexColorModel(1,2,红色地图,绿色地图,蓝色地图,0);
int transparency=colorModel.getTransparency();
int transparentPixel=colorModel.getTransparentPixel();
System.out.println(“colorModel.getTransparency():”+transparency);
System.out.println(“colorModel.getTransparentPixel():”+transparentPixel);
BuffereImage BuffereImage=新的BuffereImage(300300,BuffereImage.TYPE\u BYTE\u INDEXED,colorModel);
WritableRaster WritableRaster=BuffereImage.getRaster();
对于(int x=0;x<300;x++){
对于(int y=0;y<300;y++){
int[]fill=new int[1];//一个大块。。。
数组。填充(填充,(x