【问题标题】:BufferedImage producing black backgroundBufferedImage 产生黑色背景
【发布时间】:2012-07-01 04:22:41
【问题描述】:

好吧,我正在制作一个游戏,我正在尝试通过在其上添加文本来修改原始命中标记图像,并且我正在使用以下代码:

import javax.swing.ImageIcon;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
public class HitMarker {

    public static final Image rangeHitMarker = new ImageIcon(HitMarker.class.getResource("rangeHitMarker.png")).getImage();
    public static final Image magicHitMarker = new ImageIcon(HitMarker.class.getResource("magicHitMarker.png")).getImage();
    public static final Image monsterHitMarker = new ImageIcon(HitMarker.class.getResource("monsterHitMarker.png")).getImage();

    public static final Font font = new Font("Tahoma", Font.PLAIN, 10);

    public static final Color t = new Color(0,0,0,0);

    public Image hitMarker;
    public BufferedImage image;
    public String hit;

    public int attackStyle;

    public boolean rangeAttack;
    public int x;
    public int y;

    public Timer timer;
    public boolean remove;

    public HitMarker(int x, int y, int hit, int attackStyle){
        this.hit = String.format("%d", hit);
        this.remove = false;
        this.x = x;
        this.y = y;
        this.attackStyle = attackStyle;
        this.hitMarker = getImage();
        BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(hitMarker, 0, 0, null);
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString(this.hit, 18, 13);
        g.dispose();
        image = bi;
        timer = new Timer(800,
                new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove = true;
                timer.stop();
            }
        }
        );
        timer.setInitialDelay(800);
        timer.start();
    }

    public HitMarker(int x, int y, int hit){
        this.hit = String.format("%d", hit);
        this.remove = false;
        this.x = x;
        this.y = y;
        this.hitMarker = monsterHitMarker;
        BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(hitMarker, 0, 0, null);
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString(this.hit, 18, 13);
        g.dispose();
        image = bi;
        timer = new Timer(800,
                new ActionListener(){
            public void actionPerformed(ActionEvent e){
                remove = true;
                timer.stop();
            }
        }
        );
        timer.setInitialDelay(800);
        timer.start();
    }

    public boolean isRangeAttack(){
        return attackStyle == AttackStyleConstants.RANGE || attackStyle == AttackStyleConstants.RANGE_DEFENCE ? true : false;
    }

    public Image getImage(){
        return isRangeAttack() ? rangeHitMarker : magicHitMarker;
    }

}

特别关注任一构造函数: 我遇到的错误是,当我创建 BufferedImage 并在缓冲图像上绘制图像时,它会自动创建黑色背景,我不知道为什么。我试过研究这个话题,有人说要改变一些关于 AlphaComposite 和 g.clearRect() 方法的东西,但这些似乎都不起作用。顺便说一句,我在缓冲图像上绘制的图像是 35x20(这是缓冲图像的尺寸)并且它具有透明背景。如果有人能告诉我如何去除这个黑色背景,将不胜感激,谢谢。

【问题讨论】:

  • 为了获得更好的帮助,请尽快发布SSCCE,有两个原因 ---> 大多数回答者不去第三个侧面链接,2) 对于期货读者
  • @JoshM 他的意思是,最好直接在这里发布代码,或者只是问题部分和完整源的链接,所以如果链接死了,你的问题不会将来变得毫无用处。
  • 哦,我明白了,对于我可能造成的任何不便,我深表歉意。好吧,问题尤其出在任一构造函数中(我实际上在其中创建了 BufferedImage),我认为最好显示整个代码(考虑到它的

标签: java image background transparency bufferedimage


【解决方案1】:

试试BufferedImage.TYPE_INT_ARGB。这将使区域透明而不是黑色。

【讨论】:

  • 因为我遇到了同样的问题,所以我尝试了上面的解决方案对我不起作用,实际上我想让背景变成浅灰色而不是黑色。有人能告诉我该怎么做吗
【解决方案2】:

您可能还想尝试存储 Alpha 通道,

 BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_ARGB);

【讨论】:

    【解决方案3】:

    使用 png 而不是 jpeg。 Png 非常适合透明度操作。 这里是简单的png导出代码sn-p;

            BufferedImage bImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = (Graphics2D) bImage.getGraphics();
            DrawingContext context = new DrawingContext(g2d);
            plot.draw(context);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png");
    
            wr.write(plot, baos, 640, 480);
            baos.flush();
    
            baos.close();
            InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
            BufferedImage bufferedImage =  ImageIO.read(inputStream);
    
            ImageIO.write(bufferedImage,"png",new File(outputFolder.getPath()+"/result.png"));
    

    【讨论】:

      【解决方案4】:

      如果您需要带有白色背景的JPG,您需要像这样绘制图像:

      g.drawImage(hitMarker, 0, 0, Color.WHITE, null);
      

      这样可以避免从PNGJPG 时出现黑色背景。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 2018-06-18
        • 2015-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-23
        相关资源
        最近更新 更多