-4

    import java.awt.BorderLayout;
            import java.applet.Applet;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.util.ArrayList;
            import javax.swing.*;
            import java.awt.FlowLayout;
            import java.awt.event.*;
            import java.awt.*;

            public class MemoryGame implements ActionListener 
            {
                Label mostra;           
                public int delay = 1000; //1000 milliseconds
                public void init() 
                {   
                    //add(mostra = new Label("     "+0));
                }

                public void Contador()
                {
                    ActionListener counter = new ActionListener() 
                    {
                        public void actionPerformed(ActionEvent e) 
                        {
                            tempo++;
                            TempoScore.setText("Tempo: " + tempo);
                        }
                    };
                    new Timer(delay, counter).start();
                }

                public void updateHitMiss() 
                {
                    HitScore.setText("Acertou: " + Hit);
                    MissScore.setText("Falhou: " + Miss);
                    PontosScore.setText("Pontos: " + Pontos);
                }

                private JFrame window = new JFrame("Jogo da Memoria");
                private static final int WINDOW_WIDTH = 500; // pixels
                private static final int WINDOW_HEIGHT = 500; // pixels
                private JButton exitBtn, baralharBtn, solveBtn, restartBtn;
                ImageIcon ButtonIcon = createImageIcon("card1.png");
                private JButton[] gameBtn = new JButton[16];
                private ArrayList<Integer> gameList = new ArrayList<Integer>();
                private int Hit, Miss, Pontos;
                public int tempo = 0;   
                private int counter = 0;
                private int[] btnID = new int[2];
                private int[] btnValue = new int[2];
                private JLabel HitScore, MissScore,TempoScore, PontosScore;
                private JPanel gamePnl = new JPanel();
                private JPanel buttonPnl = new JPanel();
                private JPanel scorePnl = new JPanel();

                protected static ImageIcon createImageIcon(String path) 
                {
                    java.net.URL imgURL = MemoryGame.class.getResource(path);
                    if (imgURL != null) 
                    {
                        return new ImageIcon(imgURL);
                    } 
                    else 
                    {
                        System.err.println("Couldn't find file: " + path);
                        return null;
                    }
                }

                public MemoryGame()
                {
                    createGUI();
                    createJPanels();
                    setArrayListText();
                    window.setTitle("Jogo da Memoria");
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                    window.setVisible(true);
                    Contador();
                }

                public void createGUI()
                {
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                        gameBtn[i] = new JButton(ButtonIcon);
                        gameBtn[i].addActionListener(this);
                    }
                    HitScore = new JLabel("Acertou: " + Hit);
                    MissScore = new JLabel("Falhou: " + Miss);
                    TempoScore = new JLabel("Tempo: " + tempo);
                    PontosScore = new JLabel("Pontos: " + Pontos);
                    exitBtn = new JButton("Sair");
                    exitBtn.addActionListener(this);
                    baralharBtn = new JButton("Baralhar");
                    baralharBtn.addActionListener(this);
                    solveBtn = new JButton("Resolver");
                    solveBtn.addActionListener(this);
                    restartBtn = new JButton("Recomecar");
                    restartBtn.addActionListener(this);
                }

                public void createJPanels()
                {
                    gamePnl.setLayout(new GridLayout(4, 4));
                    for (int i = 0; i < gameBtn.length; i++)
                    {
                        gamePnl.add(gameBtn[i]);
                    }
                    buttonPnl.add(baralharBtn);
                    buttonPnl.add(exitBtn);
                    buttonPnl.add(solveBtn);
                    buttonPnl.add(restartBtn);
                    buttonPnl.setLayout(new GridLayout(1, 0));
                    scorePnl.add(HitScore);
                    scorePnl.add(MissScore);
                    scorePnl.add(TempoScore);
                    scorePnl.add(PontosScore);
                    scorePnl.setLayout(new GridLayout(1, 0));
                    window.add(scorePnl, BorderLayout.NORTH);
                    window.add(gamePnl, BorderLayout.CENTER);
                    window.add(buttonPnl, BorderLayout.SOUTH);
                }

                public void setArrayListText()
                {
                    for (int i = 0; i < 2; i++)
                    {
                        for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++)
                        {
                            gameList.add(ii);
                        }
                    }
                }

                public boolean sameValues()
                {
                    if (btnValue[0] == btnValue[1])
                    {
                        return true;
                    }
                    return false;
                }

                public void actionPerformed(ActionEvent e)
                {
                        if (exitBtn == e.getSource())
                        {
                            System.exit(0);
                        }

                        if (baralharBtn == e.getSource())
                        {
                            //
                        }

                        if (solveBtn == e.getSource())
                        {
                            for (int i = 0; i < gameBtn.length; i++)
                            {
                                gameBtn[i].setText("" + gameList.get(i));
                                gameBtn[btnID[0]].setEnabled(false);
                                gameBtn[btnID[1]].setEnabled(false);
                            }
                        }

                        for (int i = 0; i < gameBtn.length; i++)
                        {
                            if (gameBtn[i] == e.getSource())
                            {
                                gameBtn[i].setText("" + gameList.get(i));
                                gameBtn[i].setEnabled(false);
                                counter++;
                                if (counter == 3)
                                {
                                    if (sameValues())
                                    {
                                        gameBtn[btnID[0]].setEnabled(false);
                                        gameBtn[btnID[1]].setEnabled(false);
                                        gameBtn[btnID[0]].setVisible(false);
                                        gameBtn[btnID[1]].setVisible(false);
                                        Hit = Hit +1;
                                        Pontos = Pontos + 25;
                                    }
                                    else
                                    {
                                        gameBtn[btnID[0]].setEnabled(true);
                                        gameBtn[btnID[0]].setText("");
                                        gameBtn[btnID[1]].setEnabled(true);
                                        gameBtn[btnID[1]].setText("");
                                        Miss = Miss +1;
                                        Pontos = Pontos - 5;                       
                                    }
                                    counter = 1; //permite 2(3) clikes de cada vez
                                }
                                /*if (Pontos <= 0)
                                {
                                    Pontos=0;
                                } */
                                if (counter == 1) // se carregar 1º botão
                                {
                                    btnID[0] = i;
                                    btnValue[0] = gameList.get(i);
                                }
                                if (counter == 2) // se carregar 2º botão
                                {
                                    btnID[1] = i;
                                    btnValue[1] = gameList.get(i);
                                }
                            }
                        }

                        if (restartBtn == e.getSource())
                        { 
                            Hit=0;
                            Miss=0;
                            tempo=-1;
                            Pontos=0;
                            for (int i = 0; i < gameBtn.length; i++)  /* what i want to implement(restart button) goes here, this is incomplete because it  only clean numbers and "transforms" into regular JButton, the last two clicks
on the 4x4 grid (btnID[0] and btnID[1]), but i want to clean all the visible
 numbers in the grid 4x4, and want to transform all grid 4x4 into default (or
  regular color, white and blue) JButton, the grid numbers stay in same order 
    or position. */
                            {

                                gameBtn[btnID[0]].setEnabled(true); 
                                gameBtn[btnID[0]].setText(""); 
                                gameBtn[btnID[1]].setEnabled(true); 
                                gameBtn[btnID[1]].setText("");  
                            }

                            /*
                            restartBtn.addActionListener(new ActionListener() 
                            { 
                                JFrame.dispatchEvent(new WindowEvent(JFrame, WindowEvent.WINDOW_CLOSING));  // this line is getting errors (MemoryGame.java:233: error: <identifier> expected
                                JFrame.dispatchEvent(new WindowEvent(JFrame, WindowEvent.WINDOW_CLOSING));
             illegal start of type,')' expected, ';' expected all in the same line )
                                private JFrame window = new JFrame("Jogo da Memoria");
                                // something not right, i think frame is replaced by JFrame but i dont know, be 
                            }); */
                        }  
                    updateHitMiss();
                }

                public static void main(String[] args)
                {      
                    new MemoryGame();
                }
            }

        </code>

Hi, I want to add a restart button (mouse click), i am running notepadd++, when i click on restart button (recomeçar), it should restart this memory game, all counters set to zero etc, clean the grid 4x4, all numbers that were visible when i click the restart the button they all disaapear (but they are in same place or order) thus only show regular JButtons, the game should go to the starting point like when it first open the application, all numbers are not visible. (the functionality of restart button is equal to exit application and then restart).

Restart button code in lines 215 to 239, issues reported on comments between those lines.

  • 2
    So what is your question? – khelwood Apr 11 '15 at 18:36
  • Can you point out what part of your code is most likely the source of the issue? – alainlompo Apr 11 '15 at 18:38
  • 2
    1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 11 '15 at 18:54
  • possible duplicate of [How can I restart a Java application?](http://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application) – Jonas Czech Apr 11 '15 at 18:55
  • 2
    BTW - don't mix Swing and AWT components. Use Swing consistently. – Andrew Thompson Apr 11 '15 at 18:55
  • The part of my code that i am having issues is this: if (restartBtn == e.getSource()) { Hit=0; Miss=0; tempo=-1; Pontos=0; // the code i want to implement goes here } – insyspower Apr 12 '15 at 11:58

1 Answers1

0

I'm not sure if I fully understand your question, but here's what i think would help:

Add an ActionListener to your button with: button.addActionListener(new ActionListener() { });, with button being the name of your JButton.

Within your ActionListener, call frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));, with frame being the name of your JFrame.

Then, run all of the methods you use to initialize your game (including the one that opens the JFrame).

Comment if you need clarification or more help.

Lucas Baizer
  • 305
  • 2
  • 5
  • 13