import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; public class RandomWalkT extends JPanel implements ActionListener { //JButton b = new JButton("Walk"); *** Won't be needed *** int size = 400; int psize = 16; int xpos = size/2-psize/2, ypos = size/2-psize/2; int step = 30; Random random = new Random(); int delay = 300; Timer timer = new Timer(delay,this); // *** create Timer, timer // *** will use this ActionListener public RandomWalkT () { setBackground(Color.white); //add(b); // *** not needed any more //b.addActionListener(this); // *** don't listen to the button timer.start(); // *** start Timer } public void actionPerformed(ActionEvent e) { xpos += random.nextInt(step)-step/2; ypos += random.nextInt(step)-step/2; repaint(); if (delay > 10) { // *** Change speed, speed up delay -= 10; // *** movement, faster and faster timer.setDelay(delay); // *** until delay time is 10ms. } } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(Color.magenta); g.fillOval(xpos, ypos, 16, 16); } public static void main(String[] u) { JFrame f = new JFrame(); f.setSize(400, 400); f.add(new RandomWalkT()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }