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

Recommended Answers

All 7 Replies

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
fdc8b0ec066f40c4e1056a98aa1cf8d4

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()
    {
        loadPicture();

        rect = new Rectangle(250, 93, 50, 50);
        this.addKeyListener(this);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.green);
        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();
        }

        repaint();
    }

    public void keyReleased(KeyEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {

    }

}

06c40990dfbd9634e3c595eecce514ef

if I use image of a car then it flickers

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.

ok I will try & reply if it works

  • 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

You just have one paintComponent() method (for your JPanel) and no paint() methods. That's all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.