3

I am making a JFrame for a card game. I want to restart the JFrame when the restartBtn is clicked. Can anyone help me?

The PlayGame class is to launch the frame1

public class PlayGame {

  public static void main(String[] args) {
        GameFrame frame1 = new GameFrame();

        // Set Icon
        Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
        frame1.setIconImage(icon);

        frame1.setVisible(true);
        frame1.setSize(600, 700);
        frame1.setTitle("Card Game");

        // Set to exit on close
        frame1.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
  }
}

This is the GameFrame class is for the JFrame constructor.

public class GameFrame extends JFrame implements ActionListener {

  public JLabel restartLbl;  
  public JButton restartBtn

  public GameFrame() {

    restartLbl = new JLabel(restart);
    restartBtn = new JButton();

    restartBtn..addActionListener(this);
  }


  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == restartBtn) {
    }
  }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Exorific
  • 93
  • 2
  • 2
  • 6

2 Answers2

6

You'll have to code the restart of the frame. Consider the state of the game at the start and what all the components' states are. Generally you'd have a setup point somewhere and start as well at some stage. If you can set those up it would be easy to just use setup and start as restart.

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == restartBtn) {
        restart();
    }
  }

public void restart(){
    stop(); // if necessary
    setup(); // set everything to initial state
    start(); // start game
}

public void stop(){
    // stop any timers, threads, operations etc.
}

public void setup(){
    // set to initial state.
    // something like recreate the deck, 
    // clear hands and table, shuffle, and deal.
}

public void start(){
    // code to initiate the game.
}

So the best way to go about it is to see your game as several stages, and actions such as restart should be a combination of others. Without knowing anything about your actual game code (or plans for it), it's difficult to answer this specifically. But I hope that helps. :)

EDIT

This is a better way of going about generating / shuffling cards.

   public class GenRandom {

   int []  cards = new int [GameFrame.NUMBER_OF_CARDS];

   public void generateCards() {
      for (int i = 0; i < cards.length; i++) { // for each index of the array...
         int card; // declare card
         do {
            card = (int) (Math.random() * 51) + 1; // random between 1 and 52
         } while (contains(card)); // regenerate random card if array already contains.
         cards[i] = card; // card is unique, so assign value to array index
      }
   }

   private boolean contains(int t){
      for(int i = 0; i < GameFrame.NUMBER_OF_CARDS; i++){ // for each index...
         if(cards[i] == t){
            return true; // if the generated card is already in the array..
         }
      }
      return false; // otherwise reached end, so return false.
   }

   public int [] getAllCards() {
      return cards;
   }
}
rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • i am designing a game where a player draw 5 cards, the computer draws 5 cards and compare whose card is bigger (each turn compare 1 card). Then i will display the 5 cards that the player has to let the player choose which card to VS the computer. So i created 2 buttons (restart)(Exit), if the player wan to play again, he can click restart. – Exorific Jan 28 '12 at 06:41
  • Could you upload your images as well please? 4shared.com should do. – rtheunissen Jan 28 '12 at 06:59
  • 1
    @paranoid-android : +1, for showing interest in solving the problem :-) Regards – nIcE cOw Jan 28 '12 at 08:02
  • i have looked through it but i cant really understand some part of the code especially the GenRandom ,anyway thanks a lot – Exorific Jan 28 '12 at 08:39
  • 1
    @paranoid-android: `Collections.shuffle()`, used [here](http://stackoverflow.com/a/7935040/230513), is more simple and less biased. – trashgod Jan 28 '12 at 14:35
  • I actually had thought of that but wanted to keep to using an array. There's always `asList` as well. +1, `Collections.shuffle()` is a much better idea. :) – rtheunissen Jan 29 '12 at 00:09
  • Although, in order to use `Collections.shuffle(Arrays.asList(array))` you'd have to have an `Integer []`, or alternatively if you save the cards in a `Collection` in the first place, when you return the `Collection` as an array you would have to return an `Integer []`, unless you copy each `valueOf` to a new array. Haha the best solution would be to use an `ArrayList` in the class that calls the generator, removing the need to swap between primitive and reference types. – rtheunissen Jan 29 '12 at 00:17
  • How much of what is in the comments can be worked back into the question/answer? Please clean up the comments section and focus on the *posts*. – casperOne Jan 29 '12 at 02:43
  • I believe my answer should remain as it is. I considered changing it to trashgod's suggestion, but given the context of the problem, ie. the rest of the code in question, the answer is sound. – rtheunissen Jan 29 '12 at 03:02
2

I had modified your code as per your liking, that you wanted to restart the game with the press of the Restart Button.

Here in your PlayGame class's main method, just change it to this.

public class PlayGame {

    public static void main(String[] args) {

        GameFrame frame1 = new GameFrame();            
    }

}

Whatever was inside this except for the first line, just cut and paste that to the GameFrame class's constructor, along with the previous things as it is like this :

public GameFrame()
{
    // Your previous code as it is, and paste the below lines, after your already 
    // written code, at the end of the constructor.
    // Set Icon
    Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
    setIconImage(icon);
    setSize(600, 700);
    setTitle("Card Game");

    // Set to exit on close
    setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

Now inside the actionPeformed(ActionEvent ae); method, for your Restart Button, write this.

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

    this.dispose();
    new GameFrame();
}

Hopefully this might solve your query.

Regards

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • it works perfectly for my second try after posting the code at the end of the constructor, thanks a lot – Exorific Jan 28 '12 at 07:58
  • @Exorific : Hehe, Your Welcome and Keep Smiling :-) Regards – nIcE cOw Jan 28 '12 at 08:00
  • @Exorific : Just a small suggestion, since you keeping track of number of wins by computer and human, you better declare those two variables as static inside your PlayGame class instead of GameFrame class. That way even when you restart the game, those values will remain untouched, and you can keep adding on them. Regards – nIcE cOw Jan 28 '12 at 08:25
  • So you're creating a whole new panel / gui every time you restart the game? It might be simpler but it seems unnecessary to do that. – rtheunissen Jan 29 '12 at 00:10
  • 1
    It would be much faster to just reset the components rather than re-create them. – rtheunissen Jan 29 '12 at 00:19