I found a silly card dealing program in which I am trying to learn by enhancing it. In the process I found a class that I could use to place graphics into an APPLICATION, which is by no means an easy task if you are still learning this stuff. anyway, I decided that I wanted to use graphics for the cards instead of the words, so I modified the Card class to store not only the strings Face and Suit but a graphic object representation of the card itself... this way the card and picture were always together. only problem is, I am not sure how to get that graphic onto the application now... following are some snippets as to what I have done. it starts out with the Card class and the CardPanel class.
ultimately if someone can tell me how to intialize and use this portion of the Card object and the syntax involved to perform this correctly.
public Card( String f, String s, cardPanel r )
{
face = f;
suit = s;
rep = r;
}
public cardPanel getCard(){
return rep;
}
public String toString() {
return face + " of " + suit + " including " + rep;
}
}
class cardPanel extends JPanel{
private ImageIcon imageIcon;
public cardPanel(File imageFile){
super();
imageIcon = new ImageIcon (imageFile.getAbsolutePath());
setPreferredSize(new Dimension(imageIcon.getIconWidth(), imageIcon.getIconHeight()));
}
public cardPanel(String imageFileName){
super();
imageIcon = new ImageIcon(imageFileName);
setPreferredSize(new Dimension(imageIcon.getIconWidth(), imageIcon.getIconHeight()));
}
publicvoid paint(Graphics g){
super.paintComponent(g);
g.drawImage(imageIcon.getImage(),0,0,this);
}
}
first I create the deck of cards
for ( int i = 0; i < deck.length; i++ ){
File imageFile = new File(cardGifs[i]);
deck[ i ] = new Card( faces[ i % 13 ],
suits[ i / 13 ],new cardPanel(imageFile));
, then shuffle. next, the dealer deals 2 hands one to himself and one to me...
if ( mDealt1 != null ) {
myHand[0] = mDealt1;
}
else {
status.setText("Shuffle cards to continue" );
}
then when I go through each card in each hand, I want to extract just the graphic object (rep) and place that on the screen. like this: (it calls addComponent which takes care of the constraints and stuff)
myCards[] array was declared but not inited. When I put this code
for ( int i = 0; i < myHand.length; i++ )
{
java.util.Random r = new java.util.Random();
int index = r.nextInt(cardGifs.length);
File imageFile = new File(cardGifs[index]);
myHand[ i ] = new Card( faces[ index % 13 ], suits[ index / 13 ],new cardPanel(imageFile));
}
after the lines
currentCard = -1;
// *******************************
// this creates the deck of cards
// *******************************
the window showed up, but I dont know if thats how its played!
well, it took me a little while to see what you were getting at. I thought everything had been created and initialized but after seeing your 'fix' in action I realized the flaw. the real issue was a design flaw. because I dont deal any cards upon startup. I deal after a shuffle. therefore, the the objects ARE null. so I just need to init them with a blank graphic object or something similar to show no cards have been dealt. thanks man! it's the silly stuff that will get ya!