[英]Class extends BufferedImage, but picture can't change. Any way around this?
I'm trying to create a simple Card class that extends BufferedImage so I can draw the card directly on the screen. 我正在尝试创建一个扩展BufferedImage的简单Card类,以便可以在屏幕上直接绘制该卡。 But the card has two faces.
但是该卡有两个面孔。 The front, and the back.
正面和背面。 I am including a flip(boolean faceup) method that I want to change the image from one side to the next, but it seems that since the Class extends BufferedImage, it's final?
我包括一个flip(boolean faceup)方法,我想将图像从一侧更改为另一侧,但是似乎由于Class扩展了BufferedImage,所以这是最终的吗? I'm not sure, that's my impression.
我不确定,那是我的印象。 It doesn't change from the original image drawn in the constructor.
它与构造函数中绘制的原始图像没有变化。 Is there any way around this so I can still paint the card directly on-screen?
有什么办法可以解决,所以我仍然可以在屏幕上直接刷卡吗? This is what I have so far...
这是我到目前为止所拥有的...
public Card(int rank, int suit)
{
super(50,70,TYPE_INT_ARGB);
this.rank = rank;
this.suit = suit;
try{bi = ImageIO.read(getClass().getResource(toString()+".png"));
back = ImageIO.read(getClass().getResource("back.png"));}
catch(IOException e){e.printStackTrace();}
Graphics2D g = createGraphics();
g.drawImage(back,0,0,null);
g.dispose();
}
public void flip(boolean faceup)
{
this.faceup = faceup;
Graphics2D g = createGraphics();
if(faceup)g.drawImage(bi,0,0,null);
else g.drawImage(back,0,0,null);
g.dispose();
}
Don't extend BufferedImage. 不要扩展BufferedImage。
Instead your class can contain two BufferedImages: 相反,您的类可以包含两个BufferedImages:
Then the painting method will paint the front or the back depending on the "flip" property. 然后,绘制方法将根据“翻转”属性绘制正面或背面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.