/* * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * */ public class BetterAnimation { /** * entry point, create an instance and pass on to createAndShowGUI() */ public static void main (String[] args) { final BetterAnimation p = new BetterAnimation(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { p.createAndShowGUI(); } }); p.go(); } public void go() { ActionListener incrementYPosition = new ActionListener() { public void actionPerformed(ActionEvent evt) { if (_y < 400) { _y++; if (_frame != null) _frame.repaint(); } else { _timer.stop(); } } }; _timer = new javax.swing.Timer(10,incrementYPosition); _timer.start(); } /** * sets up everything, and then makes Frame visible */ public void createAndShowGUI() { //Create and set up the window. _frame = new JFrame("BetterAnimation") { public void paint(Graphics g) { g.setColor(Color.BLUE); g.clearRect(0,0,400,400); g.fillOval(_x-_radius,_y-_radius,2*_radius,2*_radius); System.err.println("x " + _x + " y " + _y + " r " + _radius); } }; _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); _frame.setPreferredSize(new Dimension(400,400)); _frame.pack(); _frame.setVisible(true); } private int _x = 200; private int _y = 0; private int _radius = 20; private JFrame _frame; private javax.swing.Timer _timer; }