简体   繁体   English

BufferedImage生成黑色背景

[英]BufferedImage producing black background

Alright so I'm making a game, and I'm trying to modify the original hit marker image by adding text on it, and I'm using the following code: 好吧,所以我正在制作游戏,我正在尝试通过在其上添加文本来修改原始命中标记图像,并且我使用以下代码:

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;
    }

}

Focusing particularly on either constructor: And the error that I'm having is that when I create the BufferedImage and draw the image on the buffered image, it's creating a black background automatically and I don't know why. 特别关注构造函数:我遇到的错误是当我创建BufferedImage并在缓冲图像上绘制图像时,它会自动创建黑色背景,我不知道为什么。 I've tried researching on this topic and some say to change something about the AlphaComposite and the g.clearRect() method, but neither of those seem to work. 我已经尝试过研究这个主题,并且有些人说要改变一些关于AlphaComposite和g.clearRect()方法的东西,但这些似乎都不起作用。 By the way, the image that I'm painting on the buffered image is 35x20 (which is the dimensions of the buffered image) and it has a transparent background. 顺便说一句,我在缓冲图像上绘制的图像是35x20(这是缓冲图像的尺寸),它具有透明背景。 If anyone can tell me how to remove this black background, it would be very much appreciated, thank you. 如果有人能告诉我如何删除这个黑色背景,将非常感谢,谢谢。

Try BufferedImage.TYPE_INT_ARGB . 尝试BufferedImage.TYPE_INT_ARGB This will make the regions transparent instead of black. 这将使区域透明而不是黑色。

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

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

If you need a JPG with white background, you need to draw the image like this: 如果你需要一个白色背景的JPG ,你需要像这样绘制图像:

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

This way you avoid the black background when going from PNG to JPG . 这样,当您从PNG转到JPG时,可以避免黑色背景。

Use png instead of jpeg. 使用png而不是jpeg。 Png is much suitable for transparency operations. Png非常适合透明操作。 Here is simple png export code snippet; 这是简单的png导出代码片段;

        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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM