title: Java : How to Avoid flickering on shape-made only animation (no image file)?
body: <p>Hi I am reading the book "Programming Video Games for the Evil Genius" by Ian Cinnamon. In one of the projects he explains how to create some animation for a racing game. I wrote a simple code below (removing "almost" everything unnecessary). But the result is the same: <strong>the flickering</strong>.</p>
  
  <p>My question is how, <strong>by using that same code</strong> (with little modifications obviously) can I make it stop flickering? My point is <strong>not to find another way around</strong> (modifying the whole code) because I am already able to create an entire game using <strong>JLabel</strong>, <strong>ImageIcon</strong> classes &amp; spritesheets image files. My point is to solve the flickering problem without removing those shapes [<strong>Rectangle</strong> (fillRect &amp; if possible drawImage)].</p>
  
  <p>By the way if this is not possible "Why would you do that!?" or "It's impossible" are also good answers. (let's try not to use them!)</p>
  
  <p>Thank You!</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.io.*;
  import java.net.*;
  
  public class Race extends JFrame {
      public static void main(String[] args){
          new Race();
      }
          private Rectangle r1 = new Rectangle(0,0,800,100);
          private Rectangle r2 = new Rectangle(0,100,100,400);
          Image img = null;
  
      public Race(){
          super("Some Title");
          setVisible(true);
          setSize(800,600);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          try{
               URL url= this.getClass().getResource("someImage.png");
              img =Toolkit.getDefaultToolkit().getImage(url);
          }catch(Exception e){}
          Race.GameLoop gameHeart = new Race.GameLoop();
          gameHeart.run();
      }
  
      public void paint(Graphics g){
          super.paint(g);
          g.setColor(Color.GREEN);
          g.fillRect(r1.x, r1.y, r1.width, r1.height);
          g.setColor(Color.RED);
          g.fillRect(r2.x, r2.y, r2.width, r2.height);
          g.drawImage(img, r2.x, r2.y, r2.width, r2.height, this);
          try {
              Thread.sleep(75);
          } catch (InterruptedException ex) {}
          repaint();
      }
  
      public class GameLoop extends Thread {
          public void run(){
              while(true){
                  //game animations and logic
                  //even if I put repaint() here it still flicker
              }
          }
      }
  }
  </code></pre>