Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:33:00+00:00 2026-06-15T02:33:00+00:00

Hi I am reading the book Programming Video Games for the Evil Genius by

  • 0

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: the flickering.

My question is how, by using that same code (with little modifications obviously) can I make it stop flickering? My point is not to find another way around (modifying the whole code) because I am already able to create an entire game using JLabel, ImageIcon classes & spritesheets image files. My point is to solve the flickering problem without removing those shapes [Rectangle (fillRect & if possible drawImage)].

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!)

Thank You!

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
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:33:01+00:00Added an answer on June 15, 2026 at 2:33 am

    Don’t paint onto a top level container (like JFrame), they do not support double buffering.

    Instead, use something like a JPanel

    Convention prefers overriding paintComponent rather then paint. This is done mostly because paint is a actually a very complex method

    NEVER do this in any paint method:

    try {
        Thread.sleep(75);
    } catch (InterruptedException ex) {}
    repaint();
    

    Firstly, any delays should be handled by the game thread, calling Thread#sleep within the paint method will put the Event Dispatching Thread to sleep, which will prevent it from posting events (about mouse and keyboard interactions) and will make your program look non-responsive, which will cause you issues later on.

    Never call repaint (or any method that might call repaint) from inside a paint method. This is simply asking the repaint manager to schedule another paint cycle, which will chew up your CPU until you program becomes unresponsive. Again, this is function of your game thread.

    Remember, Swing is NOT thread save. All interactions with the UI MUST be made from within the context of the Event Dispatching Thread. Have a read through Concurrency in Swing for some more information

    You can take a look at How to make line animation smoother? and Java Bouncing Ball for an examples

    You might like to have a read through Performing Custom Painting and Painting in AWT and Swing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a book on memory as a programming concept. In one of the
I'm reading book about OO programming in JavaScript and get some strange behaviour: function
While reading the book Programming Ruby , one example shows how blocks can be
I'm reading a book on template programming, and one of the examples they have
Right now I'm reading a beginner-level book called 'Programming Embedded Systems'. It has one
Hi, guys! Currently I'm reading a book Programming in Objective-C by Stephen Kochan. Actually
im currently reading a book about programming Android and there is a nice little
I'm reading a book on scala programming (the Programming in Scala), and I've got
I have been reading from the book The C Programming Language learning C, and
I'm pretty new to web programming, reading a book on ASP.NET, and I notice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.