简体   繁体   English

类扩展了BufferedImage,但图片无法更改。 可以解决吗?

[英]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:

  1. the front 前方
  2. the back 背部

Then the painting method will paint the front or the back depending on the "flip" property. 然后,绘制方法将根据“翻转”属性绘制正面或背面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串不能改变。 但是int,char可以改变 - String can't change. But int, char can change 为什么不能更改此BufferedImage? - Why can't I change this BufferedImage? 对于抽象类A是否有更好的解决方案 <T extends A> ? - Is there any better solution for abstract class A<T extends A>? 有什么方法可以与生成意图并从扩展IntentService类的类发送该意图的类进行通信? - Is there any way by which we can communicate back to the class which generated an intent and sent it from a class which extends IntentService class? 无法将BufferedImage缩放到另一个BufferedImage中 - Can't draw BufferedImage into another BufferedImage with scale 有什么方法可以解决不实现Serializable的类吗? - Is there any way to get around a class not implementing Serializable? 无法将组件添加到扩展JFrame的类中 - Can't add components to a class that extends JFrame 不能模拟方法(Class <? extends SomeClass> ) - Can't mock method(Class<? extends SomeClass>) 有什么方法可以创建从Android中的Canvas扩展的类吗? - Is there any way to create class which extends from Canvas in Android? 将扩展JFrame的类更改为扩展JPanel的类 - Change class that extends JFrame to class that extends JPanel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM