2014-03-04 102 views
0

我正在嘗試創建一個生成10張撲克牌的java程序。撲克牌採用gif格式。我試圖把這些放在一個數組中,但不知道我如何繼續。理想情況下,撲克牌沒有嚴格的擺放位置。無論如何,卡片都是留下的。需要提示才能領先。就目前來看,我沒有收到任何錯誤消息。但是出現的那個盒子是空的。我該怎麼辦?循環圖像,Java gui?

在此先感謝

cards.java

public abstract class cards extends JPanel { 

Random gen = new Random(); 
int noOfCards = 10; 
int line; 
int col; 
boolean faceUp; 
String back = "img/b2fv.gif"; 
JLabel[] stackLabel = new JLabel[noOfCards]; 
ImageIcon[] stack = new ImageIcon[noOfCards]; 

ImageIcon [][] cards = { 
     {new ImageIcon("img/c1.gif"), new ImageIcon("img/d1.gif"), new ImageIcon("img/h1.gif") , new ImageIcon("img/s1.gif")}, 
     {new ImageIcon("img/c2.gif"), new ImageIcon("img/d2.gif"), new ImageIcon("img/h2.gif") , new ImageIcon("img/s2.gif")}, 
     {new ImageIcon("img/c3.gif"), new ImageIcon("img/d3.gif"), new ImageIcon("img/h3.gif") , new ImageIcon("img/s3.gif")}, 
     {new ImageIcon("img/c4.gif"), new ImageIcon("img/d4.gif"), new ImageIcon("img/h4.gif") , new ImageIcon("img/s4.gif")}, 
     {new ImageIcon("img/c5.gif"), new ImageIcon("img/d5.gif"), new ImageIcon("img/h5.gif") , new ImageIcon("img/s5.gif")}, 
     {new ImageIcon("img/c6.gif"), new ImageIcon("img/d6.gif"), new ImageIcon("img/h6.gif") , new ImageIcon("img/s6.gif")}, 
     {new ImageIcon("img/c7.gif"), new ImageIcon("img/d7.gif"), new ImageIcon("img/h7.gif") , new ImageIcon("img/s7.gif")}, 
     {new ImageIcon("img/c8.gif"), new ImageIcon("img/d8.gif"), new ImageIcon("img/h8.gif") , new ImageIcon("img/s8.gif")}, 
     {new ImageIcon("img/c9.gif"), new ImageIcon("img/d9.gif"), new ImageIcon("img/h9.gif") , new ImageIcon("img/s9.gif")}, 
     {new ImageIcon("img/c10.gif"), new ImageIcon("img/d10.gif"), new ImageIcon("img/h10.gif") , new ImageIcon("img/s10.gif")}, 
     {new ImageIcon("img/cj.gif"), new ImageIcon("img/dj.gif"), new ImageIcon("img/hj.gif") , new ImageIcon("img/sj.gif")}, 
     {new ImageIcon("img/cq.gif"), new ImageIcon("img/dq.gif"), new ImageIcon("img/hq.gif") , new ImageIcon("img/sq.gif")}, 
     {new ImageIcon("img/ck.gif"), new ImageIcon("img/dk.gif"), new ImageIcon("img/hk.gif") , new ImageIcon("img/sk.gif")}, 
     {new ImageIcon("img/jr.gif"), new ImageIcon("img/jb.gif")} 
}; 

public JLabel[] genCards(){ 
    try{ 
     for(int i = 0; i < noOfCards; i++){ 
      line = gen.nextInt(14); 
      col = gen.nextInt(4); 
      faceUp = gen.nextBoolean(); 

      stack[i] = cards[line][col]; 
      stackLabel[i] = new JLabel(); 
      ImageIcon img; 
      stackLabel[i].setIcon(img = stack[i]); 
      this.add(stackLabel[i]); 
     } 
    }catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(e); 
     System.out.println("Problem med array"); 
    } 
    return stackLabel; 
} 
} 

table.java

public class table extends cards { 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    for(int i = 0; i < this.stackLabel.length; i++){ 
     //System.out.println(stack[i]); 
    } 
} 

回答

1

ImageIcon的是一個Swing組件,因此只需添加那些ImageIcons像一個容器一個JPanel(也許這張卡擴展了JPanel)。容器本身繪製它包含的Swing組件,因此您不必手動繪製它們。

1

至少你必須在某處調用genCards()。可能在構造函數或init方法中:

public Cards(){ 
    genCards(); 
}