首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java制图有关问题求教

2014-01-08 
java制图问题求教我们的要求只是很简单的在图上写字,以前一直都没问题最近换了一个背景图,是白色透明边框

java制图问题求教
我们的要求只是很简单的在图上写字,以前一直都没问题
最近换了一个背景图,是白色透明边框的
这时候再运行以前的程序发现边框自动被替换为了黑色

代码如下


    private File pressTextToImage(BannerPropertiesDto bannerPropertiesDto) throws IOException {
        File img = new File(bannerPropertiesDto.getBackgroundImageFile());
        Image src = ImageIO.read(img);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(src, 0, 0, width, height, null);
        g.setColor(bannerPropertiesDto.getFontColor());
        g.setFont(new Font(bannerPropertiesDto.getFontStyle(), bannerPropertiesDto.getFontThickness(), bannerPropertiesDto.getFontSize()));
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, bannerPropertiesDto.getFontTransparency()));
        g.drawString(bannerPropertiesDto.getPressText(), (width - (getLength(bannerPropertiesDto.getPressText()) * bannerPropertiesDto.getFontSize())) / 2 + bannerPropertiesDto.getCoordinatesX(),
                (height - bannerPropertiesDto.getFontSize()) / 2 + bannerPropertiesDto.getCoordinatesY());
        g.dispose();
        File file = new File(bannerPropertiesDto.getBannerImageFile());
        ImageIO.write((BufferedImage) image, "PNG", file);
        return file;
    }


请教可以不自动去除白色透明边框吗
[解决办法]
换个color呢?g.setColor
[解决办法]
首先“白色透明边框”是不对的,要么白色,要么透明。

看你的问题,应该是透明边框被“变成”了黑色。

看你存的格式是 PNG,要保留透明边框的话,可以试试

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

这一行的 TYPE_INT_RGB 改成 TYPE_INT_ARGB

热点排行