ImageIcon is a Swing component, so just add those ImageIcons to a container like a JPanel (perhaps that Cards extends JPanel you have). The container itself draws the Swing components it contains, so you don't have to manually draw them.
Loop images, java gui?
Question
I'm trying to create a java program that generates 10 playing cards. The playing cards are in gif format. I have tried to put these in an array, but do not know how I get on. The playing cards should ideally be displayed without the strict placement. The cards are left anyway. Would need tips to get ahead. As it stands now, I get no error message. But the box that comes up, is empty. What should I do?
Thanks in advance
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]);
}
}
Solution
OTHER TIPS
At least you have to call to genCards() somewhere. Probably in constructor or a init method:
public Cards(){
genCards();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow