I am creating a game of Racing cars in java
But the problem is that my car when moved in any direction makes flickering on the screen.
As the project is a Core java project it can only be made in a Windows application form.

I set the speed of my car movement to 20px/keypress to 1px/keypress but the problem that still occur is flickering in the screen.
How can I solve this????
I have attached the files in zip.
Please help me

Edited 3 Years Ago by vinnitro

I coudb't see any source code in that zip. How are you handling the screenpaints? Are you overriding Swing's default double-buffering in any way?

There is RaceApplet.java file which is source code
4 images for car direction & 1 for background

I compile it like : javac RaceApplet.java
& then I view it on applet using : appletviewer RaceApplet.java

The code works fine but the applet flickers

Daniweb is not allowing me to upload the .java & .png files

This is the track file resized which is actually quite big i.e. 2400 x 1800 size which daniweb does not allow me

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;

/*<applet height=800 width=600 code="RaceApplet.java"></applet>*/

public class RaceApplet extends JApplet implements KeyListener
    private Image player;
    private Image bg;
    private int nx = 800;
    private int ny = 0;
    private Rectangle rect;

    private void loadPicture()
        bg = new ImageIcon("RaceBack.png").getImage();
        player = new ImageIcon("KD//KDE.png").getImage();

    public void init()

        rect = new Rectangle(250, 93, 50, 50);

    public void paint(Graphics g)
        g.fillRect(0, 0, 34567, 34567);
        g.drawImage(bg, 800-nx, ny, null);
        g.drawImage(player, rect.x, rect.y, null);

    public void keyPressed(KeyEvent e)
        if( e.getKeyCode() == KeyEvent.VK_RIGHT )
            nx = nx + 20;
            player = new ImageIcon("KD//KDE.png").getImage();
        if( e.getKeyCode() == KeyEvent.VK_LEFT )
            nx = nx - 20;
            player = new ImageIcon("KD//KDE.png").getImage();
        if( e.getKeyCode() == KeyEvent.VK_UP )
            ny = ny + 20;
            player = new ImageIcon("KD//KDE.png").getImage();
        if( e.getKeyCode() == KeyEvent.VK_DOWN )
            ny = ny - 20;
            player = new ImageIcon("KD//KDE.png").getImage();


    public void keyReleased(KeyEvent e)


    public void keyTyped(KeyEvent e)




if I use image of a car then it flickers

Edited 3 Years Ago by vinnitro

Attachments KDE.png 0.33 KB

You are forcing a repaint of the whole applet every time, which may be the cause of your flickering.
In any case it's much better to put your animation in a JPanel, and override paintComponent for the JPanel to do your drawing. Add that JPanel to your applet and don't mess with the applet painting at all (at line 62 just call repaint() on yur JPanel).

ps Sorry, the .java was there and I was being stupid.

Edited 3 Years Ago by JamesCherrill

  • put there JPanel, don't to paint to the Top-Level Container directly

  • override for JPanel

    1. getPreferredSize, then all coordinates for painting are based on getWeight/Height

    2. use paintComponent() instead of paint()

  • don't to use Keylistener, use KeyBindings added to JPanel

  • don't to provide FileIO (ImageIcon), load, set any Objects inside paintComponent() / paint()

  • use (for paint) BufferedImage (as local variable) inside paintComponent()

should I have to replace the paint() & keep only paintComponent()
or I have to keep both methods

This question has already been answered. Start a new discussion instead.