簡體   English   中英

組件對BufferedImage具有透明背景?

[英]Component with transparent background to BufferedImage?

我有一個組件擴展了JPanel。 每次調用paintComponent方法時,它將自身保存為bufferedimage。 組件不是完全透明的,只有它的背景。 問題是背景不透明。 我正在使用setOpaque(false);

這是我的相關代碼;

private BufferedImage bufImage = null;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    // if first time call
    if (bufImage == null) {
        int w = this.getWidth();
        int h = this.getHeight();
        bufImage = (BufferedImage)this.createImage(w, h);
    }

    g2.drawImage(bufImage, null, 0, 0);

    // draw sth
    g2.draw(sth);
}

-

我也試過了

bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

代替

bufImage = (BufferedImage)this.createImage(w, h);

當我這樣做; 背景透明效果很好,但是我只能用白色繪制。 我不知道是什么原因造成的。

注意:我使用該代碼檢查它是否正常工作;

File outputfile = new File("saved.png");
ImageIO.write(bufImage, "png", outputfile);

saved.png具有透明背景,但工程圖只有白色。


這是組件,只允許用鼠標繪制矩形;

class PaintPanel extends JPanel implements MouseListener, MouseMotionListener {
    private BufferedImage _bufImage = null;
    private boolean dragging = false;
    private Point _start = null, _end = null;

    public PaintPanel() {
        setOpaque(false);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        if (_bufImage == null) {
            int w = this.getWidth();
            int h = this.getHeight();
            _bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            //_bufImage = (BufferedImage)this.createImage(w, h);
        }

        g2.drawImage(_bufImage, null, 0, 0);

        if (dragging) {
            drawCurrentShape(g2);
        }
    }

    private void drawCurrentShape(Graphics2D g2) {
        int startx = (int) _start.getX();
        int starty = (int) _start.getY();
        int stopx = (int) _end.getX();
        int stopy = (int) _end.getY();

        int width = Math.abs(startx - stopx);
        int height = Math.abs(starty - stopy);
        int x = startx, y = starty;
        if(x > stopx)
            x = stopx;
        if(y > stopy)
            y = stopy;

        Rectangle r = new Rectangle(x, y, width, height);
        g2.draw(r);
    }

    public void mousePressed(MouseEvent e) {
        dragging = true;       
        _start = e.getPoint();
        _end   = _start;
    }

    public void mouseDragged(MouseEvent e) {
        _end = e.getPoint();
        this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
        _end = e.getPoint();
        if (dragging) {
            dragging = false;
            drawCurrentShape(_bufImage.createGraphics());  
            this.repaint();
        }
    }

    public void mouseMoved  (MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited (MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
}

嘗試這個:

bufImage = new BufferedImage(w,h,java.awt.image.BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bufImage.createGraphics();
this.print(graphics);
graphics.dispose();

關鍵是要使用print()

編輯:我嘗試了以下和透明的工作就像一個魅力:

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test2 {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(400, 400));
        p.setOpaque(false);
        JButton button = new JButton("Hello world");
        p.add(button);
        frame.add(p);
        frame.pack();
        frame.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                BufferedImage bufImage = new BufferedImage(p.getWidth(), p.getHeight(), java.awt.image.BufferedImage.TYPE_INT_ARGB);
                Graphics2D graphics = bufImage.createGraphics();
                p.print(graphics);
                graphics.dispose();
                try {
                    ImageIO.write(bufImage, "png", new File("d:/tmp/tmp.png"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

createImage(w, h)將創建具有指定寬度和高度的“空白”圖像。 話雖這么說,你需要做的是在BufferedImage實例上調用createGraphics並直接繪制到返回的Graphics對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM