簡體   English   中英

BufferedImage生成黑色背景

[英]BufferedImage producing black background

好吧,所以我正在制作游戲,我正在嘗試通過在其上添加文本來修改原始命中標記圖像,並且我使用以下代碼:

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(這是緩沖圖像的尺寸),它具有透明背景。 如果有人能告訴我如何刪除這個黑色背景,將非常感謝,謝謝。

嘗試BufferedImage.TYPE_INT_ARGB 這將使區域透明而不是黑色。

您可能還想嘗試存儲Alpha通道,

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

如果你需要一個白色背景的JPG ,你需要像這樣繪制圖像:

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

這樣,當您從PNG轉到JPG時,可以避免黑色背景。

使用png而不是jpeg。 Png非常適合透明操作。 這是簡單的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