Search code examples
javaswingimageicon

Cannot update Java Swing ImageIcon


Here is my entire code, Main class and WarGame class, sorry if its really messy. :( I'm trying to get ImageIcon to Update everytime Button is clicked.

This is a War Card game, Everytime button is clicked it should update the picture.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.*;

public class WarGame extends JFrame implements ActionListener{
 static boolean y = true;
 static Scanner sc = new Scanner(System.in);

 static String suit1;
 static String suit2;
 static String Card1;
 static String Card2;
 static int Player1Wins = 0;
 static int Player2Wins = 0;
  JLabel Player1Card;
  JLabel Player2Card;
  JLabel vs;
  JLabel player1Icon;
  JLabel player2Icon;
private ImageIcon img1;
private  ImageIcon img2;
  private JButton draw;
 String player1Show;
 String player2Show;

 int f;
 int d;
 int j;
 int k;


 public WarGame(){
     setName("WarGame");
     setLayout(null);
     setSize(350,200);

     setVisible(true);
     Player1Card = new JLabel("Player One:");
     Player1Card.setBounds(10,10, 100, 50);
     add(Player1Card);
     Player2Card = new JLabel("Player Two:");
     Player2Card.setBounds(155,10, 100, 50);
     add(Player2Card);
     vs = new JLabel("V.S");
     vs.setBounds(125,70, 100, 50);
     add(vs);
     j = 0;
     k = 0;
     f = 0;
     d= 0;
     draw = new JButton("Draw");
        draw.addActionListener(this);
        draw.setBounds(100,135, 100, 30);
        add(draw);

 }





public void actionPerformed(ActionEvent e) {

        if(e.getSource() == draw) {



                 f = 1 + (int)(Math.random()*4);
                 d = 1 + (int)(Math.random()*4);
                if (f == 1 ){
                     suit1 = "Clubs";
                }else if (f == 2){
                     suit1 = "Spades";
                }else if (f == 3){
                     suit1 = "Hearts";
                }else if (f == 4){
                   suit1 = "Diamonds";
                }

                if (d == 1 ){
                     suit2 = "Clubs";
                }else if (d == 2){
                     suit2 = "Spades";
                }else if (d == 3){
                     suit2 = "Hearts";
                }else if (d == 4){
                   suit2 = "Diamonds";
                }


                 j = 2 + (int)(Math.random()*13);
                k = 2 + (int)(Math.random()*13);

               int player1 = j;
               int player2 = k;

               if (j == 11){
                  Card1 = "Jack";
               }else if (j == 12){
                   Card1 = "Queen";
               }else if (j == 13){    
                   Card1 = "King";
               }else if (j == 3){
                   j = 6;
               }
               if (k == 11){
                      Card2 = "Jack";
               }else if (k == 12){
                       Card2 = "Queen";
               }else if (k == 13){    
                       Card2 = "King";
               }else if ( k == 3){
                   k = 6;
               }

               if (j == 0){
                   j = 2;
               }
               if (j == 14){
                   Card1 = "Ace";
               }
               if (k == 0){
                   k = 2;
               }
               if (k == 14){
                   Card2 = "Ace";
               }


               if (j <= 10 && j >= 2){
                   System.out.println("Player 1: " + j + " of " + suit1);
                   player1Show = j + suit1;

               }

               if (k <= 10 && k >= 2){
                  System.out.println("Player 2: " + k  + " of " + suit2);
                  player2Show = k+suit2;

               }
               if (j >= 11 || j == 1 ){
                   System.out.println("Player 1: " + Card1 + " of " + suit1);
                   player1Show = Card1 + suit1;

               }
               if (k >= 11 || k == 1){
                    System.out.println("Player 2: " + Card2  + " of " + suit2);
                    player2Show = Card2 + suit2;


               }

               img1 = new ImageIcon("src//decks2//" + player1Show + ".png");
                player1Icon = new JLabel();
                player1Icon.setIcon(img1);
                img2 = new ImageIcon("src//decks2//" + player2Show + ".png");
                player2Icon = new JLabel();
                player2Icon.setIcon(img2);

                player1Icon.setBounds(20, 50, 100, 100);
                player2Icon.setBounds(150, 50, 100, 100);
                add(player1Icon);
                add(player2Icon);

                if (player1 > player2){
                    System.out.println("Player 1 Wins The War");
                    Player1Wins ++;
                }else if (player2 > player1){
                   System.out.println("Player 2 Wins The War");
                   Player2Wins ++;
                }else{
                   System.out.println("The War Is A Tie");
                }
                System.out.println("Player 1 Wins: " + Player1Wins + "  Player2 Wins: " + Player2Wins );


                   if(Player1Wins < Player2Wins){
                       System.out.println("Congratulations! Player 2 Wins with a score of " + Player2Wins + " to " + Player1Wins);
                   }else if (Player1Wins > Player2Wins){
                       System.out.println("Congratulations! Player 1 Wins with a score of " + Player1Wins + " to " + Player2Wins);
                   }else if (Player1Wins == Player2Wins){
                       System.out.println("Oops! Its a tie!" + Player1Wins + " to " + Player2Wins);
                   }





        }

             revalidate();    
         repaint();
    }




}


import java.io.IOException;

public class Main {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    WarGame s = new WarGame();
    s.setVisible(true);

}

}

Solution

  • You should revalidate() and repaint() after adding/removing components. Try to add these to the end of your actionPerformed()

     public void actionPerformed(ActionEvent e){
         ...
    
         add(player1Icon); 
         add(player2Icon);
    
         ...
    
         revalidate();    
         repaint();
     }
    

    See revalidate() javadoc

    Edit:

    It's hard for me to replicate your problem from my end because I don't have the card images, but try this. At the top where you declare your card image labels, instantiate them too. JLabel player1Icon = new JLabel();. In WarGame constructor, add those labels. In your actionPerformed, take out the add(player1Icon) and the player1show = new JLabel() and just leave the player1show.setIcon()