title: How do I add a score counter to this snake game?
body: <p>I want to be able to display the score to players, and have the score go up by 10 every time you get an apple. To do this though, I need to add extra space on the window to allow room for the score counter, but I can't find where to do that in my code.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.util.ArrayList;
  import java.util.Random;
  import javax.swing.JPanel;
  
  public class Screen extends JPanel implements Runnable {
  
      private static final long serialVersionUID = 1L;
  
      public static final int WIDTH = 800, HEIGHT = 900;
      private Thread thread;
      private boolean running = false;
  
      private BodyPart b;
      private ArrayList&lt;BodyPart&gt; snake;
  
      private Apple apple;
      private ArrayList&lt;Apple&gt; apples;
  
      private Random r;
  
      private int xCoor = 20, yCoor = 20;
      private int size = 10;
  
      private boolean right = true, left = false, up = false, down = false;
  
      private int ticks = 0;
  
      private Key key;
  
      public Screen() {
          setFocusable(true);
          key = new Key();
          addKeyListener(key);
          setPreferredSize(new Dimension(WIDTH, HEIGHT));
  
          r = new Random();
  
          snake = new ArrayList&lt;BodyPart&gt;();
          apples = new ArrayList&lt;Apple&gt;();
  
          start();
      }
  
      public void reset() {
          snake.clear();
          apples.clear();
          xCoor = 20;
          yCoor = 20;
          size = 10;
          running = true;
      }
  
      public void tick() {
          if(snake.size() == 0) {
              b = new BodyPart(xCoor, yCoor, 20);
              snake.add(b);
          }
  
          if(apples.size() == 0) {
              int xCoor = r.nextInt(40);
              int yCoor = r.nextInt(40);
  
              apple = new Apple(xCoor, yCoor, 20);
              apples.add(apple);
          }
  
          for(int i = 0; i &lt; apples.size(); i++) {
              if(xCoor == apples.get(i).getxCoor() &amp;&amp; yCoor ==         apples.get(i).getyCoor()) {
                  size++;
                  apples.remove(i);
                  i--;
              }
          }
  
          for(int i = 0; i &lt; snake.size(); i++) {
              if(xCoor == snake.get(i).getxCoor() &amp;&amp; yCoor ==  snake.get(i).getyCoor()) {
                  if(i != snake.size() - 1) {
                      reset();
                  }
              }
          }
  
          if(xCoor &lt; 0) xCoor = 40;
          if(xCoor &gt; 40) xCoor = 0;
          if(yCoor &lt; 0) yCoor = 40;
          if(yCoor &gt; 40) yCoor = 0;
  
          ticks++;
  
          if(ticks &gt; 250000) {
              if(right) xCoor++;
              if(left) xCoor--;
              if(up) yCoor--;
              if(down) yCoor++;
  
              ticks = 185000;
  
              b = new BodyPart(xCoor, yCoor, 20);
              snake.add(b);
  
              if(snake.size() &gt; size) {
                  snake.remove(0);
              }
          }
      }
  
      public void paint(Graphics g) {
          g.clearRect(0, 0, WIDTH, HEIGHT);
  
          g.setColor(new Color(20, 50, 0));
          g.fillRect(0, 0, WIDTH, HEIGHT);
  
          g.setColor(Color.BLACK);
          for(int i = 0; i &lt; WIDTH / 20; i++) {
              g.drawLine(i * 20, 0, i * 20, HEIGHT);
          }
  
          for(int i = 0; i &lt; HEIGHT / 20; i++) {
              g.drawLine(0, i * 20, WIDTH, i * 20);
          }
  
          for(int i = 0; i &lt; snake.size(); i++) {
              snake.get(i).draw(g);
          }
          for(int i = 0; i &lt; apples.size(); i++) {
              apples.get(i).draw(g);
          }
  
      }
  
      public void start() {
          running = true;
          thread = new Thread(this, "Game Loop");
          thread.start();
      }
  
      public void stop() {
          running = false;
          try {
              thread.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
      public void run() {
          while(running) {
              tick();
              repaint();
          }
      }
  
      private class Key implements KeyListener {
  
          public void keyPressed(KeyEvent e) {
              int key = e.getKeyCode();
  
              if(key == KeyEvent.VK_RIGHT &amp;&amp; !left) { 
                  up = false;
                  down = false;
                  right = true;
              }
  
              if(key == KeyEvent.VK_LEFT &amp;&amp; !right) { 
                  up = false;
                  down = false;
                  left = true;
              }
  
              if(key == KeyEvent.VK_UP &amp;&amp; !down) {
                  left = false;
                  right = false;
                  up = true;
              }
  
              if(key == KeyEvent.VK_DOWN &amp;&amp; !up) {
                  left = false;
                  right = false;
                  down = true;
              }
  
          }
  
          @Override
          public void keyReleased(KeyEvent arg0) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void keyTyped(KeyEvent arg0) {
              // TODO Auto-generated method stub
  
          }
  
          }
  
          public void keyReleased(KeyEvent arg0) {
              // TODO Auto-generated method stub
  
          }
  
          public void keyTyped(KeyEvent arg0) {
              // TODO Auto-generated method stub
  
          }
  
      }
  </code></pre>