import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;class MyB extends Button implements ActionListener {
Timer timer;
int i, x, y;
Color[] color = { Color.green, Color.yellow, Color.red };
Color c = color[0]; MyB() {
this.setBackground(Color.cyan);
timer = new Timer(1000, this);
x = y = 10;
this.setSize(35, 85);
timer.start();
//this.addActionListener(this);
} public void paint(Graphics g) {
g.setColor(c);
g.fillOval(x, y, 20, 20);
this.validate();
} public void update(Graphics g) {
g.clearRect(x, y, 20, 20);
this.paint(g);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == timer) {
if (y > 50)
y = 10;
else
y += 25;
// System.out.println(y);
i++;
c = color[i % 3];
this.validate();
this.repaint();
}
}
}class MyF extends JFrame {
Container con;
MyB myb; MyF() {
con = this.getContentPane();
myb = new MyB();
this.setLayout(null);
this.add(myb);
myb.setBounds(10, 10, 50, 85);
this.setBounds(10, 10, 100, 200);
this.setVisible(true);
this.validate();
}
}public class Test {
public static void main(String[] args) {
new MyF();
}
}