java - Why will my JButton not update the imageicon? -


currently i'm trying make video poker. far consists of 2 classes: card int value, char suit, , boolean checked. card jbutton. deck stack of card objects.

the jbutton in videopoker class won't update imageicon when draw card, , can't figure out why life of me. updates when want background image , again when want original image, why not new card?

below code

import javax.swing.*; import java.awt.event.*;  public class videopoker extends jpanel implements actionlistener {      private deck deck;     private card[] cards;     private jbutton draw;     private final int maxcards = 5;      public videopoker() {         deck = new deck();         cards = new card[maxcards];         for(int = 0; < maxcards; i++) {             cards[i] = deck.draw();             cards[i].addactionlistener(this);             add(cards[i]);             cards[i].seticon(new imageicon                             ("cards/" + cards[i].getvalue() +                             cards[i].getsuit() + ".png"));         }         draw = new jbutton("draw");         draw.addactionlistener(this);         add(draw);     }      public void actionperformed(actionevent e) {         if(e.getsource() == draw) {             int checked = 0;             for(int = 0; < maxcards; i++) {                 if(cards[i].getchecked()) {                     cards[i] = deck.draw();                     cards[i].seticon(new imageicon                             ("cards/" + cards[i].getvalue() +                              cards[i].getsuit() + ".png"));                     checked++;                 }             }         }         if(e.getsource() instanceof card) {             card source = (card)e.getsource();             if(!source.getchecked()) {                 source.setchecked(true);                 source.seticon(new imageicon("cards/back.png"));             }             else {                 source.setchecked(false);                 source.seticon(new imageicon                         ("cards/" + source.getvalue() +                         source.getsuit() + ".png"));             }         }     } 

it looks problem caused not adding newly drawn card videopoker panel. when draw event handled, items in cards array can replaced, these card/button objects not yet added panel:

public void actionperformed(actionevent e) {     if(e.getsource() == draw) {         int checked = 0;         for(int = 0; < maxcards; i++) {             if(cards[i].getchecked()) {                 cards[i] = deck.draw();                 cards[i].seticon(new imageicon                         ("cards/" + cards[i].getvalue() +                          cards[i].getsuit() + ".png"));                 checked++;             }         }     }     if(e.getsource() instanceof card) {         // ...     } } 

when remove old card panel before getting new 1 , add new card panel after that, card changes on screen well.

                remove(cards[i]);                 cards[i] = deck.draw();                 add(cards[i]); 

there still work because card needs added @ right position.

you can change design , use cardbutton has reference card instead of having card button. way can have 5 fixed cardbuttons , let these reference right card objects, making buttons , cards less tightly coupled.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -