Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Multiple Applet boxes

Baka_yarouBaka_yarou Member Posts: 13
Second attempt at posting this (error message the first time about some serious error occuring). The first attempt wasn't showing up on my profile or in the forum. Could an admin delete one if that's the case. Thanks.

Hi all!

I was wondering if anyone could help me. I have my little card game, which I'm making the interface for now. I'm managing to get everything working except the reset button. When I click on it, it starts up a new game but the old one remains. Obviously because I haven't told the old one to close, but I don't know how to do that. :( Does anyone happen to know how? I'll post the code if it's needed, just let me know.

Thanks

Comments

  • silveredge52silveredge52 Member Posts: 198
    When the Reset button is pressed one would expect the program to invoke a method that contains variable initialization. Then invoke the repaint() method.

    Without code hard to say why resultant behavior occurs.

    Regards
  • Baka_yarouBaka_yarou Member Posts: 13
    Thank you for offering your assistance again. The main problem is from the actionPerformed method. All I've done is tell it to create a new instance of the Game, and I've not done anything to close the old game. I think what you mentioned, repaint() would solve that. So when the reset button is clicked, actionPerformed then calls up a method called resetGame that repaints the game frame, and calls up a new deck of cards. Would that just be a case of removing CardArea (the JPanel that holds the card panels), reseting the necessary variables and calling up the "Deal" method from the Pack class again? and then repainting the Container Pane with the new JPanel holding the new set of cards?

    [code]
    /**
    * @(#)Game.java
    *
    * Game application- This is the main class for the Concentration game
    *
    * @author GD Campbell
    * @version 1.00 2010/1/8
    */

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.String;
    import java.lang.Exception;

    public class Game extends JFrame implements MouseListener, ActionListener
    {
    Card[] plDeck = new Card[17];


    JPanel cp1 = new JPanel();
    JPanel cp2 = new JPanel();
    JPanel cp3 = new JPanel();
    JPanel cp4 = new JPanel();

    JPanel cp5 = new JPanel();
    JPanel cp6 = new JPanel();
    JPanel cp7 = new JPanel();
    JPanel cp8 = new JPanel();

    JPanel cp9 = new JPanel();
    JPanel cp10 = new JPanel();
    JPanel cp11 = new JPanel();
    JPanel cp12 = new JPanel();

    JPanel cp13 = new JPanel();
    JPanel cp14 = new JPanel();
    JPanel cp15 = new JPanel();
    JPanel cp16 = new JPanel();
    /**********************************************
    **********************************************/

    ImageIcon backF = new ImageIcon("JK.png");


    JLabel LCard1 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard2 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard3 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard4 = new JLabel(backF,JLabel.CENTER);

    JLabel LCard5 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard6 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard7 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard8 = new JLabel(backF,JLabel.CENTER);

    JLabel LCard9 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard10 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard11 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard12 = new JLabel(backF,JLabel.CENTER);

    JLabel LCard13 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard14 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard15 = new JLabel(backF,JLabel.CENTER);
    JLabel LCard16 = new JLabel(backF,JLabel.CENTER);

    /**********************************************
    **********************************************/

    JPanel CardArea = new JPanel();


    JPanel ReSc = new JPanel();
    JButton ResetBtn = new JButton();
    JLabel ScoreLbl = new JLabel();

    int Score = 46;

    int fb1 = 0, fb2 = 0;
    int k = 0;
    int fst=0, snd=0; //fst stands for first and holds the location of the 1st card
    //snd stands for second and holds the location of the 2nd card

    int pairsfound; //stores the number of pairs found

    Container gPane = getContentPane();
    JLabel header = new JLabel();
    public Game()
    {
    super("Concentration!");

    plDeck = Pack.Deal();


    int i;
    for (i=1; i<17; i++)
    {
    System.out.print("Card Value at "+i+" is "+plDeck[i].sN+"
    ");
    }



    Container gPane = getContentPane();
    gPane.setLayout(new BorderLayout());

    //Sets out the NORTH section of the Border box
    header = new JLabel("Can you find all the pairs before"
    +" the score reaches zero?", JLabel.CENTER);
    header.setPreferredSize(new Dimension(450, 60));
    header.setFont(new Font("Courier", Font.BOLD, 14));
    gPane.add(header, BorderLayout.NORTH);

    /***********************************
    **********************************/
    //This sets out the CENTER section of the Border box

    CardArea.setLayout(new GridLayout(4,4,10,10));
    gPane.add(CardArea,BorderLayout.CENTER);

    /**************************************
    adds the card images to the card panels (cp)
    ***************************************/
    cp1.add(LCard1);
    cp2.add(LCard2);
    cp3.add(LCard3);
    cp4.add(LCard4);

    cp5.add(LCard5);
    cp6.add(LCard6);
    cp7.add(LCard7);
    cp8.add(LCard8);

    cp9.add(LCard9);
    cp10.add(LCard10);
    cp11.add(LCard11);
    cp12.add(LCard12);

    cp13.add(LCard13);
    cp14.add(LCard14);
    cp15.add(LCard15);
    cp16.add(LCard16);
    /**************************************
    ***************************************/

    /**************************************
    add the mouse listener to each of the card panels
    ***************************************/

    cp1.addMouseListener(this);
    cp2.addMouseListener(this);
    cp3.addMouseListener(this);
    cp4.addMouseListener(this);

    cp5.addMouseListener(this);
    cp6.addMouseListener(this);
    cp7.addMouseListener(this);
    cp8.addMouseListener(this);

    cp9.addMouseListener(this);
    cp10.addMouseListener(this);
    cp11.addMouseListener(this);
    cp12.addMouseListener(this);

    cp13.addMouseListener(this);
    cp14.addMouseListener(this);
    cp15.addMouseListener(this);
    cp16.addMouseListener(this);

    /**************************************
    ***************************************/

    /**************************************
    add the card panes to the CardArea pane
    ***************************************/

    CardArea.add(cp1);
    CardArea.add(cp2);
    CardArea.add(cp3);
    CardArea.add(cp4);

    CardArea.add(cp5);
    CardArea.add(cp6);
    CardArea.add(cp7);
    CardArea.add(cp8);

    CardArea.add(cp9);
    CardArea.add(cp10);
    CardArea.add(cp11);
    CardArea.add(cp12);

    CardArea.add(cp13);
    CardArea.add(cp14);
    CardArea.add(cp15);
    CardArea.add(cp16);

    /**************************************
    ***************************************/

    //This sets out the SOUTH section of the Border box
    ReSc.setLayout(new GridLayout(1,2,10,4));
    ResetBtn = new JButton("Reset");
    ResetBtn.addActionListener(this);
    ScoreLbl = new JLabel("Score "+Score, JLabel.CENTER);
    ReSc.add(ResetBtn);
    ReSc.add(ScoreLbl);

    gPane.add(ReSc, BorderLayout.SOUTH);


    }//end Game

    public static void main(String[] args) {

    Game g = new Game();
    g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    g.setSize(500, 600);
    g.setVisible(true);

    //topTenSc.getTT();// throws Exception;

    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == ResetBtn)
    {
    setTitle("Does this work?");
    Game g = new Game();
    g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    g.setSize(400, 550);
    g.setVisible(true);

    }
    }//end actionPerformed

    public void mouseClicked(MouseEvent m)
    {

    ScoreLbl.setText("Score "+Score+"
    ");
    ScoreLbl.repaint();

    //boolean chkpair;

    if (k == 2)
    {

    if((fb1 != 0) && (fb2 != 0))
    {


    flipCards(fb1, fb2);

    fb1 = 0;
    fb2 = 0;

    }//end if fb1 and fb2 not 0
    k = 0;
    fst = 0;
    snd = 0;
    }//end if k == 2



    if (m.getSource() == cp1)
    {

    LCard1.setIcon(plDeck[1].front);
    cp1.removeMouseListener(this); //will prevent the player from clicking
    //the same card twice

    if (k == 0)
    {
    fst = 1;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 1;
    k++;
    }//end elseif

    }//end if cp1

    if (m.getSource() == cp2)
    {
    LCard2.setIcon(plDeck[2].front);
    cp2.removeMouseListener(this);

    if (k == 0)
    {
    fst = 2;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 2;
    k++;
    }//end elseif
    }//end if cp2

    if (m.getSource() == cp3)
    {
    LCard3.setIcon(plDeck[3].front);
    cp3.removeMouseListener(this);


    if (k == 0)
    {
    fst = 3;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 3;
    k++;
    }//end elseif


    }//end if cp3

    if(m.getSource() == cp4)
    {
    LCard4.setIcon(plDeck[4].front);
    cp4.removeMouseListener(this);

    if (k == 0)
    {
    fst = 4;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 4;
    k++;
    }//end elseif
    }//end if cp4

    if(m.getSource() == cp5)
    {
    LCard5.setIcon(plDeck[5].front);
    cp5.removeMouseListener(this);

    if (k == 0)
    {
    fst = 5;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 5;
    k++;
    }//end elseif
    }//end if cp5

    if(m.getSource() == cp6)
    {
    LCard6.setIcon(plDeck[6].front);
    cp6.removeMouseListener(this);

    if (k == 0)
    {
    fst = 6;
    k++;
    }//end if
    else if (k ==1)
    {
    snd = 6;
    k++;
    }//end elseif
    }//end if cp6

    if(m.getSource() == cp7)
    {
    LCard7.setIcon(plDeck[7].front);
    cp7.removeMouseListener(this);

    if (k == 0)
    {
    fst = 7;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 7;
    k++;
    }//end elseif
    }//end if cp7

    if(m.getSource() == cp8)
    {
    LCard8.setIcon(plDeck[8].front);
    cp8.removeMouseListener(this);

    if (k == 0)
    {
    fst = 8;
    k++;
    }//end if
    else if (k ==1)
    {
    snd = 8;
    k++;
    }//end elseif
    }//end if cp8

    if(m.getSource() == cp9)
    {
    LCard9.setIcon(plDeck[9].front);
    cp9.removeMouseListener(this);

    if (k == 0)
    {
    fst = 9;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 9;
    k++;
    }//end elseif
    }//end if cp9

    if(m.getSource() == cp10)
    {
    LCard10.setIcon(plDeck[10].front);
    cp10.removeMouseListener(this);

    if (k == 0)
    {
    fst = 10;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 10;
    k++;
    }//end elseif
    }//end if cp10

    if(m.getSource() == cp11)
    {
    LCard11.setIcon(plDeck[11].front);
    cp11.removeMouseListener(this);

    if (k == 0)
    {
    fst = 11;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 11;
    k++;
    }//end elseif
    }//end if cp11

    if(m.getSource() == cp12)
    {
    LCard12.setIcon(plDeck[12].front);
    cp12.removeMouseListener(this);

    if (k == 0)
    {
    fst = 12;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 12;
    k++;
    }//end elseif
    }//end if cp12

    if(m.getSource() == cp13)
    {
    LCard13.setIcon(plDeck[13].front);
    cp13.removeMouseListener(this);

    if (k == 0)
    {
    fst = 13;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 13;
    k++;
    }//end elseif
    }//end if cp13

    if(m.getSource() == cp14)
    {
    LCard14.setIcon(plDeck[14].front);
    cp14.removeMouseListener(this);

    if (k == 0)
    {
    fst = 14;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 14;
    k++;
    }//end elseif
    }//end if cp14

    if(m.getSource() == cp15)
    {
    LCard15.setIcon(plDeck[15].front);
    cp15.removeMouseListener(this);

    if (k == 0)
    {
    fst = 15;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 15;
    k++;
    }//end elseif
    }//end if cp15

    if(m.getSource() == cp16)
    {
    LCard16.setIcon(plDeck[16].front);
    cp16.removeMouseListener(this);

    if (k == 0)
    {
    fst = 16;
    k++;
    }//end if
    else if (k == 1)
    {
    snd = 16;
    k++;
    }//end elseif
    }//end if cp16

    System.out.print("
    "+fst+" "+snd+"
    ");

    //check that there have been too selections
    //boolean chkpair;
    if (k == 2)
    {
    setTitle("You picked the "+plDeck[fst].fullName+" and the "+plDeck[snd].fullName);

    rPairMatch(fst, snd);

    //System.out.print("
    "+fst+" "+snd+"
    ");


    }//end if k is 2



    }//end mouseclicked




    public void rPairMatch(int fst, int snd)
    {
    //boolean pairRmatch = false;
    String f = plDeck[fst].sN;
    String s = plDeck[snd].sN;

    System.out.print("first - "+f+" second - "+s+"

    ");

    if (f.equals(s))
    {
    //pairRmatch = true;
    pairsfound++;

    if (pairsfound == 8)
    {
    ScoreLbl.setText("Well done! You found them all!");
    ScoreLbl.repaint();
    }else{
    ScoreLbl.setText("Well done! Find another pair!");
    ScoreLbl.repaint();
    }

    fb1 = 0;
    fb2 = 0;

    }//end if strings are equal
    else if (!(f.equals(s)))
    {

    //pairRmatch = false;
    fb1 = fst;
    fb2 = snd;

    ScoreLbl.setText("Sorry, that wasn't a pair. Try again.");
    ScoreLbl.repaint();

    Score--;

    }//end elseif strings not equal

    fst = 0;
    snd = 0;

    boolean gameWon;
    if (Score == 0)
    {
    gameWon = false;
    gameEnd(gameWon);
    }//end if score is 0
    else if (pairsfound == 8)
    {
    gameWon = true;
    gameEnd(gameWon);
    }//end elseif pairsfound are 8

    //return pairRmatch;
    }//end rPairMatch

    public void gameEnd(boolean gameWon)
    {
    boolean inTopTen;

    if (gameWon = false)
    {
    //display "Sorry you lost!" in header
    header.setText("Sorry you lost!");
    header.repaint();
    inTopTen = false;

    }else if (gameWon = true)
    {
    //display Congratulations in header
    header.setText("Congratulations! Let's see if you're in the top ten!!");
    header.repaint();
    }

    //TopTen(inTopTen);
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void flipCards(int f1, int f2)
    {

    int fb = 0;

    int c;

    for (c=0;c<2;c++)
    {
    if (c==0)
    {
    fb = f1;
    }else if (c==1)
    {
    fb = f2;
    }

    switch(fb){

    case 1:
    cp1.addMouseListener(this);
    LCard1.setIcon(backF); break;
    case 2:
    cp2.addMouseListener(this);
    LCard2.setIcon(backF); break;
    case 3:
    cp3.addMouseListener(this);
    LCard3.setIcon(backF); break;
    case 4:
    cp4.addMouseListener(this);
    LCard4.setIcon(backF); break;
    /*
    *
    */
    case 5:
    cp5.addMouseListener(this);
    LCard5.setIcon(backF); break;
    case 6:
    cp6.addMouseListener(this);
    LCard6.setIcon(backF); break;
    case 7:
    cp7.addMouseListener(this);
    LCard7.setIcon(backF); break;
    case 8:
    cp8.addMouseListener(this);
    LCard8.setIcon(backF); break;
    /*
    *
    */
    case 9:
    cp9.addMouseListener(this);
    LCard9.setIcon(backF); break;
    case 10:
    cp10.addMouseListener(this);
    LCard10.setIcon(backF); break;
    case 11:
    cp11.addMouseListener(this);
    LCard11.setIcon(backF); break;
    case 12:
    cp12.addMouseListener(this);
    LCard12.setIcon(backF); break;
    /*
    *
    */
    case 13:
    cp13.addMouseListener(this);
    LCard13.setIcon(backF); break;
    case 14:
    cp14.addMouseListener(this);
    LCard14.setIcon(backF); break;
    case 15:
    cp15.addMouseListener(this);
    LCard15.setIcon(backF); break;
    case 16:
    cp16.addMouseListener(this);
    LCard16.setIcon(backF); break;
    default: System.out.println("Out of bounds");
    break;
    }//end switch (fst)

    }//end for

    }//end flipCards


    }//end class

    [/code]
  • Baka_yarouBaka_yarou Member Posts: 13
    Oops duplicate post- again!
  • Baka_yarouBaka_yarou Member Posts: 13
    I've tried this. I've set a method to reset all the variables, and card panels, and then to repaint the gPane (the container pane) which it does, but now the mouseClicked method seems to stop working after a couple of cards have been clicked. I'm not too sure as to why. Perhaps you can spot something I might have done to cause this.

    [code]
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == ResetBtn)
    {
    resetGame();

    }
    }//end actionPerformed


    public void resetGame()
    {
    /*
    *Currently this does reset the cards, however,
    *the mouseclick seems to stop working after 3 cards
    */


    plDeck = Pack.Deal();

    k = 0; fst = 0; snd = 0;
    fb1 = 0; fb2 = 0;

    pairsfound = 0;
    Score = 46;


    cp1.addMouseListener(this);
    LCard1.setIcon(backF);
    cp2.addMouseListener(this);
    LCard2.setIcon(backF);
    cp3.addMouseListener(this);
    LCard3.setIcon(backF);A
    cp4.addMouseListener(this);
    LCard4.setIcon(backF);

    cp5.addMouseListener(this);
    LCard5.setIcon(backF);
    cp6.addMouseListener(this);
    LCard6.setIcon(backF);
    cp7.addMouseListener(this);
    LCard7.setIcon(backF);
    cp8.addMouseListener(this);
    LCard8.setIcon(backF);

    cp9.addMouseListener(this);
    LCard9.setIcon(backF);
    cp10.addMouseListener(this);
    LCard10.setIcon(backF);
    cp11.addMouseListener(this);
    LCard11.setIcon(backF);
    cp12.addMouseListener(this);
    LCard12.setIcon(backF);

    cp13.addMouseListener(this);
    LCard13.setIcon(backF);
    cp14.addMouseListener(this);
    LCard14.setIcon(backF);
    cp15.addMouseListener(this);
    LCard15.setIcon(backF);
    cp16.addMouseListener(this);
    LCard16.setIcon(backF);

    header.setText("Can you find all the pairs before"
    +" the score reaches zero?");

    ScoreLbl.setText("Score "+Score);

    gPane.repaint();

    }//end resetGame
    [/code]
  • silveredge52silveredge52 Member Posts: 198
    I can run your file but the ImageIcon("JK.png") does not show on the window since I don't have that file. For further testing I would need it, it can be uploaded via the Upload Attachment button below. Really should have all the classes, ie. Pack & Class to reproduce the problem.

    I did click in the window and it kept working after 3 mouse clicks.

  • Baka_yarouBaka_yarou Member Posts: 13
    Thanks, that'd be greatly appreciated. I'll send through the image files as well.
  • silveredge52silveredge52 Member Posts: 198
    Baka_yarou,
    The "trick", I used to respond to the reset button was to "hide" the current window, ie. [code]this.setVisible(false);[/code]. I also changed the methods in Pack to be "not static" and created a Pack instance in the Game consturctor, ie. [code]Pack myPack = new Pack();
    plDeck = myPack.Deal();[/code]

    Made some other changes to get the icons to show in the window also. I've attached the modified code. ps. your code may not have used a package statement, which I did to keep all of the image files "seperate" from
    other code.

    I've learned a lot studying your code.

    Good luck,
    se52
  • Baka_yarouBaka_yarou Member Posts: 13
    So simple! Thank you. I don't know why, but I'm sure I thought of this.setVisible, but I was too stupid to think of how I could implement it properly.

    Wow, that's surprising to hear, that you could learn something from my code. I hope it's meant in a good way, so I'm going to take it as a compliment. Haha

    Thank you so much, again!
Sign In or Register to comment.