title: How can i draw something only once in Java using Graphics
body: <p>I am practicing Java, trying to get back into OOP programming. I decided to recreate snake using a tutorial online. I am using Graphics for my code and i was wondering if the paintComponent() method is called 60 times a second or something similar. My probelm is that i am building some walls, if the snake collides he dies, the walls however i only want them drawn once, but it seems that the walls are drawn over and over again (I tested this using sysout). Some code is provided below:</p>
  <pre><code>    public void paintComponent(Graphics g) {
          super.paintComponent(g);
          draw(g);
      }
  </code></pre>
  <p>Inside the draw function</p>
  <pre><code>//Draw wall
  wall1 = new Walls(10, 10, 10, 20, UNIT_SIZE, g);
  </code></pre>
  <p>The Walls constructor</p>
  <pre><code>Walls(int startX, int startY, int endX, int endY, int UNIT_SIZE, Graphics g)
      {
          this.startX = startX;
          this.startY = startY;
          this.endX = endX;
          this.endY = endY;
          g.setColor(Color.GRAY);
          for(int i = startY; i&lt;=endY; i++)
          {
              for(int j = startX; j&lt;=endX; j++)
              {
                  g.fillRect(UNIT_SIZE*j, UNIT_SIZE*i, UNIT_SIZE, UNIT_SIZE);
              }
          }
      }
  </code></pre>