i am create animation in japplet and i want to know if iam using paint method and draphics2d the right way.

public void paint(Graphics g)
    {   
         super.paint(g);
        Graphics2D g2d = (Graphics2D) g; 


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

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

another question i had was in run method. i set up thread to do animations. let me know if there is a beater way to do this.

   /*** 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 ***/

reason iam asking for a better way bc this code does move the Rect() but it is flicking.

Thread sleep isn't a good way to go, the Java API includes Timer classes that you can use to schedule updates to your animation at regular intervals. The paint looks OK. Here's the simplest possible runnable example of a good approach

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

public class Animation0 extends JFrame implements ActionListener {
   // absolutely minimal example of how to structure animation in Swing.
   // One method that steps the simulation model through time
   // One method that updates the screen with the latest data
   // close window to exit.

   public static void main(String[] args) {
      new Animation0();
   }

   Animation0() {
      // build a minimal working window ...
      setTitle("Animation 0 (close window to exit)");
      setMinimumSize(new Dimension(400, 150));
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      // start Timer to call actionPerformed every 30 milliseconds...
      new Timer(30, this).start();
   }

   // this is the "model" - just a single object moving L-R at constant speed
   private int xPos = 0; // current position(s) of animated object(s)

   public void actionPerformed(ActionEvent arg0) {
      // reliably called by Timer every 30 milliseconds
      xPos += 1; // update position(s) of object(s)
      repaint(); // notify that screen refresh is now needed
   }

   // this is the GUI - draws the model on the screen

   @Override
   public void paint(Graphics g) {
      // screen refresh - called by Swing as needed
      // Never update positions etc here because there are no
      // guarantees about when or how many times this will be called.
      super.paint(g); // ensure background etc is painted
      g.fillOval(xPos, 35, 100, 100); // draw object(s) at latest position(s)
   }

}

thanks, only thing is i cant seem to fix the flicking of animation.
i used japplet with thread. still flicking
i used japplet with timer. still flicking
i used jframe with timer. still flicking.

sso may be some thing wrong with my eclipse?

Did you run the code I posted? Did that flicker?
What version of Java are you running?

yes i did run it and it is flicking.

iam running "elcipse indigo".

i looked on "http://javatester.org/version.html"
and it said iam using "Java Version 1.6.0_35 from Sun Microsystems Inc."

i also checked my .eclipseproduct file and it say

name=Eclipse Platform
id=org.eclipse.platform
version=3.7.0

i am not sure if this will help but when every i create a project in eclipse. it add two files called ".classpath", ".project" in my project.

That all looks OK, no problems there.
Are you using an old or slow machine? That code gives no visible flickering on my Core i3 2125 (not specially fast by 2012 standtads!), in Eclipse or running from a jar.

using core i7-2720. i am running in eclipse.

its wired bc if i do same thing in applet. it work fine and no flicking. but when i use japplet than i start having problems.

can you try runing this code on ur computer. if you get no flicking than it mean the problem is in my eclipse. plz let me know.

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.Timer;



public class main extends JApplet  implements ActionListener
{
    //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()
    {
        Timer timer = new Timer(30, this); //set up timer
        timer.start();                   //start timer jump inside actionperformed method
    }/*** end of start method ***/


    //############################################
    /*** game loop ***/
    public void actionPerformed(ActionEvent e)
    {

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

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

            repaint();


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




    //###############################
    /*** paint method ***/ 
    public void paint(Graphics g)
    {   
        super.paint(g); //redraw paint method. so it doesnt draw on top of each other

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

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

I'll try that for you, but it won't be for a couple of hours now...

OK. Tried it, got a small amount of flicker (maybe 2 - 4 flickers in total). I think its because you are forcing a repaint of the whole JApplet each time.
I hacked in a change to do the drawing in a JPanel in the JApplet, so it's only the panel that's redrawn. Fore me this eliminated all the fickers. I also used a java.util.Timer to keep the animation logic clear of the swing GUI thread.
Here's the hacked code (not an example of good coding standards!).

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;



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

    AniPan panel = new AniPan();

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



    //####################################
    /*** stat method ***/
    public void start()
    {
       java.util.Timer timer = new java.util.Timer();
       TimerTask timerTask = new TimerTask() {
          @Override
          public void run() {
             updateSimulation();
          }
       };
       timer.scheduleAtFixedRate(timerTask, 0, 30);
    }/*** end of start method ***/


    //############################################
    /*** game loop ***/
    public void updateSimulation()
    {

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

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

            panel.repaint();


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


    class AniPan extends JPanel {
       public void paintComponent(Graphics g)
       {   
           super.paintComponent(g); //redraw paint method. so it doesnt draw on top of each other

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

       }/** end of paint method ***/
    }
}
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.