import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
/**
*
* @author camickr
*/
public class TimerAnimation extends JLabel implements ActionListener{
int deltaX = 2;
int deltaY = 3;
int directionX = 1;
int directionY = 1;
public TimerAnimation( int startX, int startY, int deltaX, int deltaY,
int directionX, int directionY, int delay){
this.deltaX = deltaX;
this.deltaY = deltaY;
this.directionX = directionX;
this.directionY = directionY;
try {
setIcon( new ImageIcon(new URL("http://www.fh-bochum.de/fb3/images/red-ball.gif") ));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
setSize( getPreferredSize() );
setLocation(startX, startY);
new javax.swing.Timer(delay, this).start();
}
public void actionPerformed(ActionEvent e) {
Container parent = getParent();
// Determine next X position
int nextX = getLocation().x + (deltaX * directionX);
if (nextX < 0) {
nextX = 0;
directionX *= -1;
}
if ( nextX + getSize().width > parent.getSize().width) {
nextX = parent.getSize().width - getSize().width;
directionX *= -1;
}
// Determine next Y position
int nextY = getLocation().y + (deltaY * directionY);
if (nextY < 0) {
nextY = 0;
directionY *= -1;
}
if ( nextY + getSize().height > parent.getSize().height) {
nextY = parent.getSize().height - getSize().height;
directionY *= -1;
}
setLocation(nextX, nextY); // Move the label
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().setLayout(null);
frame.getContentPane().add( new TimerAnimation(10, 10, 2, 3, 1, 1, 10) );
frame.getContentPane().add( new TimerAnimation(300, 100, 3, 2, -1, 1, 20) );
frame.setSize(400, 400);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}