或者参考Color的构造方法,自己定义一个颜色,alpha值设置为0

解决方案 »

  1.   

    是背景透明吗?不是的话请无视下面代码。        int width = 400;
            int height = 400;
            BufferedImage targetImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) targetImg.getGraphics();        targetImg = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            g2d.dispose();
            g2d = targetImg.createGraphics();
            
            g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, width, height);
            g2d.setColor(Color.RED);
            g2d.drawOval(100, 100, 100, 100);        File file2 = new File("c:\\zzz.png");
            OutputStream outputStream = new FileOutputStream(file2);
            ImageIO.write(targetImg, "png", outputStream);
            outputStream.close();
      

  2.   


    将下面的 2行去掉       g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, width, height);
    就是透明的了, 而且必须是PNG文件,JPG文件不是透明的。