[英]BufferedImage displaying as only black
I'm trying to code a card game. 我正在尝试编写纸牌游戏。 I have a sprite sheet like system to get individual cards. 我有一个像精灵表那样的系统来获得单个卡。 This is the code for my Deck class (without some functions): 这是我的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;
}
}
When I paint the individual ImageIcons or the big BufferedImage on my own JPanel class, everything is just black. 当我在自己的JPanel类上绘制单个ImageIcons或较大的BufferedImage时,所有内容都是黑色的。 When I try to change TYPE_INT_RGB to ARGB the image turns completely transparent and sizeless. 当我尝试将TYPE_INT_RGB更改为ARGB时,图像变成完全透明且无尺寸。 This also happens with a jpg version of the image. 图像的jpg版本也会发生这种情况。 I tried g.drawImage(..., frame);g.drawImage(..., this);g.drawImage(..., null); 我尝试了g.drawImage(...,frame); g.drawImage(...,this); g.drawImage(...,null); but it doesn't affect the display. 但这不会影响显示。 Also important to note that I have a background Image that does display fine: 同样重要的是要注意我的背景图片显示得很好:
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(bg, 0, 0, null);//works
g.drawImage(cards.getOP(), 30, 30, frame);//does not
}
I read other posts that didn't seem to help such as: BufferedImage produces black output BufferedImage not displaying (all black) but Image can be displayed 我阅读了其他似乎没有帮助的帖子,例如: BufferedImage产生黑色输出 BufferedImage不显示(全黑)但可以显示图像
, everything is just black. ,一切都只是黑色。
bimg = new BufferedImage(imageWidth ,imageHeight, BufferedImage.TYPE_INT_RGB);
That's because all you do is create a blank BufferedImage. 那是因为您要做的就是创建一个空白的BufferedImage。 Getting a subImage of an umnpainted image gives you an unpainted image. 获取umnpainted图像的subImage会为您提供未绘制的图像。
Use ImageIO
to read the image directly into a BufferedImage: 使用ImageIO
将图像直接读取到BufferedImage中:
bimg = ImageIO.read(...);
Now you should be able to get your subImage. 现在,您应该可以获取subImage了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.