1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
<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, nx - 800, 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//KDW.png").getImage();
        }
        if( e.getKeyCode() == KeyEvent.VK_UP )
        {
            ny = ny + 20;
            player = new ImageIcon("KD//KDN.png").getImage();
        }
        if( e.getKeyCode() == KeyEvent.VK_DOWN )
        {
            ny = ny - 20;
            player = new ImageIcon("KD//KDS.png").getImage();
        }

        repaint();
    }

    public void keyReleased(KeyEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {

    }

}

The problem is when I move my car on the applet screen it flickers. Is there any solution to this. How can I make it Flicker free screen. I have searched on most of the sites but the way they showed it didn't worked that much

thnx for help in advance

  • I see you're working on a game. If you're working on an applet game project and you have the freedom to use any library, or are generally interested in game development, you might like to check out libGDX which is a more-performant multi-platform framework for making Java games. – DeejUK Jul 10 '13 at 12:53

2 Answers2

5

You should load all your images at the begining instead of instantiating them every time you need them.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • Do it mean that I have to load all images in the init() & call them as per requirements in the keyPressed() – Vinod Bhosale Jul 10 '13 at 10:39
  • Yes. When you call new `ImageIcon("path/to/file.png");`, you actually read data on the hard drive and that's a very long operation : that's why the images are flickering. You already did it for `RaceBack.png` and `KDE.png`, you should also do it for the other images. In almost all applications, you have a loading bar which is precisely intended to make you wait that all such resources are loaded. – Arnaud Denoyelle Jul 10 '13 at 12:08
1

Apart from the fact that you're loading images on KeyEvents, you're overriding paint which does not take advantage of Swing's optimized double buffering paint mechanism. Overriding paint rather than paintComponent causes flickering.

Move your paint functionality to a a new class subclassing JComponent and override paintComponent rather than paint remembering to invoke super.paintComponent(g)

public class ImageComponent extends JComponent {

    private Image player;
    private Image backgroundImage; // formerly bg
    private int nx = 800;
    private int ny = 0;
    private Rectangle rect;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.green);
        g.fillRect(0, 0, 34567, 34567);
        g.drawImage(backgroundImage, nx - 800, ny, null);
        g.drawImage(player, rect.x, rect.y, null);

    }

    public void setPlayer(Image player) {
        this.player = player;
    }

    public void setBackgroundImage(Image backgroundImage) {
        this.backgroundImage = backgroundImage;
    }
}

Not related but consider using Key Bindings rather KeyListeners handling interaction with KeyStrokes

Reimeus
  • 158,255
  • 15
  • 216
  • 276