I am trying to make some animation in which a ball is falling from some height. During the fall, ball immediately gets to the bottom of the screen. So, to make it like a natural free falling object I tried adding delay using Thread.sleep(1000) inside the while loop. But as I added this delay( ), my whole program is freezing. Also the ball does not fall and even the close operation of the screen is deactivated.
NOTE:
For Thread.delay( ) , I had to put it in try/catch block. When this try/catch block and thread.delay() statements are removed, the program runs finely. In nutshell, all I want is to add delay during the falling of ball.
NOTE : In my code below, I have added different System.out.println() statements just to check which part of the program is running and which is not. So, you can ignore that.
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Resume extends JFrame
{
JLabel ball , hanger ;
int flag = 0 ;
public Resume(String title)
{
super(title);
hanger = new JLabel(new ImageIcon("crane-hanger.jpg"));
hanger.setBounds(0 , -200 , 600 , 600 );
hanger.setVisible(true);
//hanger.setSize(100, 100);
ball = new JLabel(new ImageIcon("snooker-red-ball.png"));
ball.setBounds(10 , 60 , 400 , 400 );
ball.setVisible(true);
ball.isOptimizedDrawingEnabled();
ball.requestFocusInWindow();
//ball.requestFocus();
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{
/* if(k.getKeyCode() == KeyEvent.VK_DOWN)
{
ball.setLocation(ball.getX(), ball.getY()+ 10 );
repaint();
}
if(k.getKeyCode() == KeyEvent.VK_UP)
{
ball.setLocation(ball.getX(), ball.getY()-10);
repaint();
}*/
if(k.getKeyCode() == KeyEvent.VK_LEFT)
{
if(flag == 1)
{
ball.removeKeyListener(this);
}
else
{
ball.setLocation(ball.getX()-10, ball.getY());
repaint();
}
}
if(k.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(ball.getX()>=400)
{
ball.removeKeyListener(this);
System.out.println(ball.getY());
ball_fall();
}
else
{
ball.setLocation(ball.getX()+10, ball.getY());
repaint();
}
}
}
});
setLayout(null);
setSize(this.getMaximumSize());
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setResizable(false);
setVisible(true);
//setBackground(Color.black);
//requestFocus();
System.out.println("fall");
getContentPane().add(ball);
getContentPane().add(hanger);
//add(l);
}
public void ball_fall()
{
System.out.println("Ball falling");
try
{
while(ball.getY()!= 400)
{
ball.setLocation(ball.getX(), ball.getY()+100);
Thread.sleep(100);
repaint();
System.out.println("while falling");
}
flag = 1 ;
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
Resume r = new Resume("RESUME");
System.out.println("falling");
}
}