title: Add restart button when game is over
body: <p>I am a beginner in Java.Swing and have tried making my own Snake Game based on an example I found on the internet, and hence learn through this process. It went all good, game works, but now I am trying to add a restart button at the "game over" screen. The program is structured as following:</p>
  
  <p>Main program</p>
  
  <pre><code>public Snake() {
      add(new Board());
      setResizable(false);
      pack();
      setTitle("Snake");
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
              JFrame ex = new Snake();
              ex.setVisible(true);
          }
      });
  
  }
  </code></pre>
  
  <p>Board: (trying to only paste the relevant code)</p>
  
  <pre><code>Board() {
      initializeVariables();
      addKeyListener(new EventHandler()); 
      setBackground(Color.black);
      setFocusable(true);
      setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
      loadImages();
  
      timer = new Timer(delay, this);
      timer.start();
  }
  </code></pre>
  
  <p>with override on the following methods:</p>
  
  <pre><code>@Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      doDrawing(g);
  }
  @Override
  public void actionPerformed(ActionEvent e) {
      ///
      repaint();
  }
  </code></pre>
  
  <p>When the game is over, the snake isn't drawn anymore, but the following method is called inside paintComponent/doDrawing:</p>
  
  <pre><code>private void gameOver(Graphics g) {
      String msg = "Game Over";
      String msg2 = "New Game?";
      Font small = new Font("Helvetica", Font.BOLD, DOT_SIZE);
      FontMetrics metr = getFontMetrics(small);
  
      g.setColor(Color.white);
      g.setFont(small);
      g.drawString(msg, (BOARD_WIDTH - metr.stringWidth(msg)) / 2, BOARD_HEIGHT / 2);
  
      g.drawString(msg2, (BOARD_WIDTH - metr.stringWidth(msg)) / 2, (BOARD_HEIGHT / 2) + DOT_SIZE*2);}
  </code></pre>
  
  <p>Really, what I need is that the second string "New Game?" be responsive and restart the game. How would I do that?</p>
  
  <p>Of course, I thought about using a JButton. However, I am still unsure about how JFrame and JPanel work. I tried creating the button in actionPerformed(), only when the game is over, and use add() to add it to the screen. It does not show, and I believe the reason is that the JPanel is already taking the entire screen (JFrame?) and so the Button would need to be drawn after it but that would be outside of the JFrame.</p>
  
  <p>May I have any advice on how to proceed further?</p>
  
  <p>Thank you.</p>