1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.Timer;
public class testt extends Frame implements ActionListener {
private static final long serialVersionUID = -8079663597720234003L;
private JButton b1,b2,b3;
private Timer horloge;
Label tirage;
Label pi;
int paintaiguille=0;
int stoppaintaiguille=0;
public testt(String titre) {
setTitle("Aiguille de Buffon");
this.setBackground(Color.white);
Panel pb= new Panel();
pb.setBackground(Color.darkGray);
pb.setLayout( new FlowLayout(FlowLayout.CENTER));
b1 = new JButton("Quitter");
b1.addActionListener(this);pb.add(b1);
b2 = new JButton("Démarrer");
b2.addActionListener(this);pb.add(b2);
b3= new JButton("Arrêter");
b3.addActionListener(this);pb.add(b3);
tirage=new Label("Nombre de tirage:");add(tirage);
//pi=new Label("Approximation de Pi: ");//add(pi);
this.add("South",tirage);
this.add("North",pb);
horloge=new Timer(1000,this);
}
private static final int UNITE = 20;//largeur latte
private static final int WIDTH = 10 * UNITE;//parquet
private static final int HEIGHT = 10 * UNITE;//parquet
private static final int SIZE = UNITE / 2;//aiguille
private static Line2D creerAiguille() {
double x = Math.random() * WIDTH + 100;//position aleatoire point aiguille
double y = Math.random() * HEIGHT + 100;
Point2D.Double p1 = new Point2D.Double(x, y);
Point2D.Double p2 = new Point2D.Double(x + SIZE, y);
AffineTransform.getRotateInstance(Math.random() * 2 * Math.PI, x, y)
.transform(p2, p2);
return new Line2D.Double(p1, p2);
}
public void paint(Graphics g){
g.drawRect(100, 100, WIDTH, HEIGHT);
int X=100;
while (X!=300){X=X+UNITE;
g.drawLine(100,X,300,X);}
if (paintaiguille==1){
Graphics2D ca=(Graphics2D)g;
ca.draw(creerAiguille());
}}
public static void main (String args[]) {
testt f = new testt("");
f.setBounds(200, 200, 500, 400);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}});}
public void actionPerformed(ActionEvent e) {
String bouton=e.getActionCommand();
if (bouton.equals("Quitter")){System.exit(0);}
{if (bouton.equals("Démarrer")){paintaiguille=1;repaint();horloge.start();}}
if (bouton.equals("Arrêter")){}
}
} |
Partager