import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class SimpleAnimation extends JPanel implements ActionListener {
    Timer timer;
    double x = 0, y = 150, dx = 2;
    int interval = 20;
    
    public SimpleAnimation() {
	setBackground(Color.white);
	timer = new Timer(interval, this);
	timer.setInitialDelay(0);
    }

    public void actionPerformed(ActionEvent event) {
	x += dx;
	repaint();
    }

    public void start() { 
	if (!timer.isRunning()) timer.start(); 
    }
    public void stop() { 
	if (timer.isRunning()) timer.stop(); 
    }
    public void reset() { stop(); x = 0; repaint(); }

    public void paintComponent(Graphics gfx) {
	super.paintComponent(gfx);
	Graphics2D g = (Graphics2D) gfx;
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			   RenderingHints.VALUE_ANTIALIAS_ON);
	double r = 3;
	Ellipse2D.Double point = new Ellipse2D.Double(x - r, y - r, 2*r, 2*r);
	g.setPaint(Color.blue);     g.fill(point);
	g.setColor(Color.black);    g.draw(point);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SimpleAnimation");
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	SimpleAnimation animation = new SimpleAnimation();
	animation.setPreferredSize(new Dimension(300, 300));
        frame.getContentPane().add(animation, BorderLayout.CENTER);

	AnimationControl control = new AnimationControl(animation);

	JButton start = new JButton("Start");
	JButton stop = new JButton("Stop");
	JButton reset = new JButton("Reset");
	start.addActionListener(control);
	stop.addActionListener(control);
	reset.addActionListener(control);

	JPanel buttonPanel = new JPanel();
	buttonPanel.add(start);
	buttonPanel.add(stop);
	buttonPanel.add(reset);
        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.show();
    }

}

class AnimationControl implements ActionListener {
    SimpleAnimation animation;
    AnimationControl(SimpleAnimation sa) {
	animation = sa;
    }
    public void actionPerformed(ActionEvent event) {
	String actionCommand = event.getActionCommand();
	if (actionCommand.equals("Start")) animation.start();
	else if (actionCommand.equals("Stop")) animation.stop();
	else if (actionCommand.equals("Reset")) animation.reset();
    }
}


	

    
