how to fix animation flickering

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JApplet;


public class main extends JApplet implements Runnable
{
    //player information variable
    int x = 10;
    int y = 50;
    int width = 30;
    int height = 30;
    int dx = 1;

    //###########################################################################################################
    /*** init method ***/
    public void init()
    {
        setSize(800, 400);
    }/*** end of init method ***/



    //###########################################################################################################
    /*** stat method ***/
    public void start()
    {
        Thread thread = new Thread(this); //set up thread
        thread.start();                   //start thread jump inside run method
    }/*** end of start method ***/


    //###########################################################################################################
    /*** run method ***/
    public void run()
    {

        while(true) //main game loop
        {
            //move image here
            x += dx;

            if(x+width >= getWidth())
            {
                x -= dx;
            }

            repaint();

            try
            {
                Thread.sleep(17);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }

        }//end of while loop
    }/*** end of run method ***/




    //###########################################################################################################
    /*** paint method ***/ 
    public void paint(Graphics g)
    {   
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g; 


        g.fillRect(x, y, width, height);   //player 

    }/** end of paint method ***/
}

Recommended Answers

All 3 Replies

i think japple already has build in double buffering. you use double buffering for applet.

i think japple already has build in double buffering'

it's something you implement for your program
if you want a built in implementation you can use BufferStrategy

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.