Hello, I would like to get some help please.

I am a newbie in Java and i am stuck. Actually I wrote a code in which i want to draw a circle and then move that circle downwards like free fall. I can deal with the coordinates but i don't have any clue to refresh my JPanel. The following code shows the final position of the circle only after its complete motion. I read somewhere that "**JPanelObject.revalidate() **" can help me in this Click Here.
But I don't know how to use it. Please expalin.

Also, provide any link or any tutorial to add gravity and velocity components to 2d objects.

Thanks in advance.

My Code:

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

public class MyPanel extends JPanel
{
    int x,y;

    public int getx(int ux,int t)
    {
        int x=ux*t;

        return x;
    }

    public int gety(int uy,int t)
    {
        int y=(int)(uy*t+0.5*9.8*t*t);

        return y;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int r=25;

        x=50;
        y=50;

        for(int t=0;t<20;t++)
        {
            g.drawOval(x,y,25,25);

            x=x+5;
            y=y+5;
            try
            {
                Thread.sleep(100);
            }

            catch(Exception e)
            {

            }
            this.revalidate();

        }


    }
    public static void main(String args[])
    {
        MyPanel b = new MyPanel();

        JFrame f = new JFrame();

        f.setContentPane(b);
        f.setLayout(new FlowLayout());
        f.setSize(500,500);
        f.setVisible(true);

    }
}

The thread sleep method doesn't work well, if at all, in Swing. Your thread blocks Swing's own thread from executing, so the screen never gets updated. You have to use a swing Timer to update your animation every "n" miliisecs. Here's a trivial exaxmple that shows the correct technique:

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

public class MiniAnimation extends JFrame implements ActionListener {
   // absolutely minimal example of how to structure animation in Swing

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

   private int xPos = 0; // current position(s) of animated object(s)


   MiniAnimation() {
      // build a minimal working window...
      super("Minimal Swing Animation Demo");
      setMinimumSize(new Dimension(300, 200));
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      // start Timer to call actionPerformed 20 times/sec...
      new javax.swing.Timer(50, this).start();
   }

   public void actionPerformed(ActionEvent arg0) {
      // called by Timer 20 times/sec
      xPos += 1; // update position(s) of object(s)
      repaint(); // request screen refresh
   }

   @Override
   public void paint(Graphics g) {
      // screen refresh - called by Swing as needed
      super.paint(g); // ensure background etc is painted
      g.fillRect(xPos, 50, 100, 100); // paint object(s) at latest position(s)
   }

}
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.