import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class BoutonCadre extends JFrame{
    int x = 0;
    int y = 0;
    Timer timer;
    Bouton bouton;

    public BoutonCadre() {

	// operation de fermeture
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
	// extraire les dimensions
	Toolkit kit=Toolkit.getDefaultToolkit();
	Dimension screenSize = kit.getScreenSize();

	// centrer l'image
	setSize(screenSize.width/2,screenSize.height/2);
	setLocation(screenSize.width/4,screenSize.height/4);
    
    // definir l'icone
	setTitle("Un titre");

    // ajouter le panneau au cadre
	add(new Panneau());

	// ajouter un timer
	timer = new Timer(50,new BougeAction());
	timer.start();

	// ajouter le bouton stop
	bouton = new Bouton();
	add(bouton,BorderLayout.NORTH);

	setVisible(true);
    }

    class Panneau extends JPanel
    {
	public Panneau()
	{
	    setBackground(Color.white);
	}
		
	public void paintComponent(Graphics g){
	    super.paintComponent(g);

	    Graphics2D g2=(Graphics2D) g;

	    Rectangle2D rec = new Rectangle2D.Double(x,y,20,20);
	    g2.fill(rec);
	}
    }
	
    public class BougeAction implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    x += 10;
	    x = x % getWidth();
	    y += 10;
	    y = y %getHeight();
	    repaint();
	}
    }

    public class Bouton extends JButton {
	public Bouton() {
	    super("Stop");
	    addActionListener(new Stop());
	}
    }
    
    public class Stop implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    if (timer.isRunning()) {
		timer.stop();
		bouton.setText("Start");
	    }
	    else {
		timer.start();
		bouton.setText("Stop");
	    }
	}
    }

    public static void main(String[] args){
	BoutonCadre cadre = new BoutonCadre();
    }
}
