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);
}
}