0

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 ***/
}
2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by zeroliken
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.