2016-08-03 13 views
0

誰でもJLabelに写真を読み込むことができますか? ATMはImageIOを認識しません。変数ImageIOを見つけることができません

private void doAnimation (int frame) 
{ 
    String str = Integer.toString(frame); 
    String selFrame = "f"+ str + ".png"; 

    BufferedImage wPic = ImageIO.read(this.getClass().getResource(selFrame)); 
    JLabel wIcon = new JLabel(new ImageIcon(wPic)); 
    add(wIcon); 
    JLabel picLabel = new JLabel(new ImageIcon(wPic)); 
    add(picLabel); 
} 

selFrameは、timerで生成されたpngファイルの名前です。フレームは、アプリケーションで使用されたときに関数にインポートされた数値です。ここで

は、コンストラクタで起動したタイマここ

if (actionEvent.getSource() == timerBackground) 
    { 
     if (backgroundNum == 42) backgroundNum = 1; 
     doAnimation(backgroundNum); 
     backgroundNum++; 
    } 

のActionListenerのタイマです

timerBackground = new javax.swing.Timer (200, this); 
あなたは PaintComponenetメソッドをオーバーライドして、画像の周りにtryキャッチ(IOExceptionを)ブロックを作成する必要があり
+0

42種類のフレームがあります(gif->私はフレームに分解してアニメーションを行いたい) –

答えて

0

= Imageio.read ... このようにメソッドをオーバーライドすることができます:

@Override 
     public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, this); 
     } 
関連する問題