title: Why is Java Graphics2D 'drawString' stopping repaint()?
body: <p>I'm currently in the process of creating a game with Java Swing. I need a score counter, so I'm using the Graphics2D method <code>drawString</code> to draw it.</p>
  
  <p>I have removed everything that doesn't change the problem in my code, and eventually I found out the problem was the <code>drawString</code> method.</p>
  
  <p>This is my full code: (Problem/src/defaultpackage/Problem.java)</p>
  
  <pre><code>// All the imports are here
  public class Problem extends JPanel {
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          Problem problem = new Problem();
          frame.add(problem);
  
          frame.setTitle("Problem");
          frame.setSize(350, 720);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.requestFocus();
          frame.setVisible(true);
      }
      public Problem() {
          Timer timer = new Timer();
          timer.scheduleAtFixedRate(new ScheduleTask(),0,20);
          // Changing '20' doesn't work
      }
      private void draw(Graphics g) {
          System.out.println("draw");
          Graphics2D g2d = (Graphics2D) g;
          g2d.drawString("S", 100, 100);
          // If I remove this and replace it by:
          //g2d.fillRect(0, 0, 100, 100);
          // it works just fine
      }
      @Override
      public void paintComponent(Graphics g) {
          System.out.println("paintcomponent");
          super.paintComponent(g);
          draw(g);
      }
      private class ScheduleTask extends TimerTask {
          @Override
          public void run() {
              System.out.println("repaint");
              repaint();
          }
      }
  } 
  </code></pre>
  
  <p>I would expect the output to be:</p>
  
  <pre><code>repaint
  paintcomponent
  draw
  repaint
  paintcomponent
  draw
  </code></pre>
  
  <p>and so on. But instead, for the first two seconds that the programm is running, the output is just:</p>
  
  <pre><code>repaint
  repaint
  repaint
  </code></pre>
  
  <p>And after that two seconds everything works as expected.</p>
  
  <p>Why would this happen?</p>