title: Clear all graphics from screen Java
body: <p>I'm working on a small game and I want to set the condition for defeat. If defeat is true, I want all the graphics on the screen to be cleared so I can make way for some output text on the screen.</p>
  
  <p>I would assume there is a conventional way to do this (which I would rather know than having to put in unnecessary code). Thanks in advance!</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>public void paintComponent(Graphics g){
          if (!defeat){
              super.paintComponent(g);
              square.display(g);
              wall.display(g);
              for (Circle circle: circleArray){
  
                  circle.display(g);
              }
  
          }else if(defeat){
  
              g.drawString("You have been defeated", 300, 300);
          }
  </code></pre>