簡體   English   中英

BufferedImage僅顯示為黑色

[英]BufferedImage displaying as only black

我正在嘗試編寫紙牌游戲。 我有一個像精靈表那樣的系統來獲得單個卡。 這是我的Deck類的代碼(沒有一些功能):

    private final int ROWS=5;
    private final int COLS=13;
    private ImageIcon [][] picts =new ImageIcon[ROWS][COLS];
    static private BufferedImage bimg;

    public Deck(){

        ImageIcon ic = new ImageIcon(getClass().getResource("/pic/cards.png"));
        int imageHeight = ic.getIconHeight();
        int imageWidth  = ic.getIconWidth();

        bimg = new BufferedImage(imageWidth ,imageHeight, BufferedImage.TYPE_INT_RGB);

        int px=0, py=0, w=imageWidth/COLS, h=imageHeight/ROWS;
        System.out.println("width:"+w+" hieght:"+h);
        for(int i=0;i<ROWS;i++){
            px=0;
            for(int j=0;j<COLS;j++){
                picts[i][j]=new ImageIcon(bimg.getSubimage(px, py, w, h));
                px+=w;
            }
            py+=h;
        }
    }

當我在自己的JPanel類上繪制單個ImageIcons或較大的BufferedImage時,所有內容都是黑色的。 當我嘗試將TYPE_INT_RGB更改為ARGB時,圖像變成完全透明且無尺寸。 圖像的jpg版本也會發生這種情況。 我嘗試了g.drawImage(...,frame); g.drawImage(...,this); g.drawImage(...,null); 但這不會影響顯示。 同樣重要的是要注意我的背景圖片顯示得很好:

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(bg, 0, 0, null);//works
        g.drawImage(cards.getOP(), 30, 30, frame);//does not
    }

我閱讀了其他似乎沒有幫助的帖子,例如: BufferedImage產生黑色輸出 BufferedImage不顯示(全黑)但可以顯示圖像

,一切都只是黑色。

bimg = new BufferedImage(imageWidth ,imageHeight, BufferedImage.TYPE_INT_RGB);

那是因為您要做的就是創建一個空白的BufferedImage。 獲取umnpainted圖像的subImage會為您提供未繪制的圖像。

使用ImageIO將圖像直接讀取到BufferedImage中:

bimg = ImageIO.read(...);

現在,您應該可以獲取subImage了。

暫無
暫無

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

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