import java.awt.* ; import java.applet.* ; import java.util.* ; import packsprite.* ; public class montecarlo extends Applet { protected animation anim ; protected Label labNbPoints ; static Scrollbar cv ; /* initialisation */ public void init() { // definit le fond d'ecran setBackground(Color.white) ; // creation des objets utils pour l'interface graphique cv = new Scrollbar(Scrollbar.HORIZONTAL, 30, 10, 1, 400) ; courbepi cp = new courbepi() ; anim = new animation(cp) ; labNbPoints = new Label("n="+Long.toString(cv.getValue())) ; Button btStart = new Button("Start") ; Button btPause = new Button("Pause") ; Button btContinue = new Button("Continue") ; // interface graphique Panel paneau1 = new Panel() ; paneau1.add(btStart) ; paneau1.add(btPause) ; paneau1.add(btContinue) ; add(anim) ; add(cp) ; add(paneau1) ; } /* gere les evenements utlisateur */ public boolean handleEvent(Event e) { // si la barre de defilement est active... if (e.target instanceof Scrollbar) { // definit le label du nombre de range labNbPoints.setText("n="+Long.toString(cv.getValue())) ; return true ; } // evenement produit par un bouton... if (e.target instanceof Button) { // evenement de type click sur bouton if (e.id==Event.ACTION_EVENT) { // recuper le label du bouton Button bt = (Button)e.target ; /* si action sur le bouton debut */ if ("Start".equals(bt.getLabel())) { anim.effacerImage() ; anim.actif = true ; return true ; } if ("Pause".equals(bt.getLabel())) { anim.actif = false ; return true ; } if ("Continue".equals(bt.getLabel())) { anim.actif = true ; return true ; } } } // evenement produit si click sur le canvas if (e.target instanceof Canvas) { } // evenement non traite return false ; } } /* Objet animation : * extends Canvas * dessine des points aleatoires */ final class animation extends spritecanvas { /* animation active */ private int Ntirage = 0, Ntouche = 0 ; private courbepi cp ; private int compteur = 0 ; private Random r = new Random() ; /* creation animation */ public animation(courbepi cp) { super() ; resize(200,200) ; this.cp = cp ; delai = 20 ; setBackground(new Color(235,235,235)) ; start() ; actif = false ; } /* mise a jour */ public void update(Graphics g) { if (imageHorsEcran==null) { imageHorsEcran = createImage(size().width, size().height) ; Graphics graphicsHorsEcran = imageHorsEcran.getGraphics() ; graphicsHorsEcran.setColor(Color.white) ; graphicsHorsEcran.fillOval(0,0,size().width-1,size().height-1) ; } /* obtient le graphics de l'image hors ecran */ Graphics graphicsHorsEcran = imageHorsEcran.getGraphics() ; /* dessine une serie de points */ paint(graphicsHorsEcran) ; /* copie l'image hors ecran sur la zone visible */ g.drawImage(imageHorsEcran, 0, 0, this) ; } /* dessine un point */ static Color color1=new Color(100,100,250); public void paint(Graphics g) { // dessine le contour noir if (!actif) { if (imageHorsEcran==null) { imageHorsEcran = createImage(size().width, size().height) ; Graphics graphicsHorsEcran = imageHorsEcran.getGraphics() ; graphicsHorsEcran.setColor(new Color(235,235,235)); graphicsHorsEcran.fillRect(0,0,size().width,size().height) ; graphicsHorsEcran.setColor(Color.white) ; graphicsHorsEcran.fillOval(0,0,size().width-1,size().height-1) ; } g.drawImage(imageHorsEcran, 0, 0, this) ; } g.setColor(Color.black) ; g.drawRect(0,0,size().width-1,size().height-1) ; // dessine les points if (actif) { for (int i=0 ; i<10 ; i++) { g.setColor(color1) ; double posH = r.nextFloat() * size().width ; double posV = r.nextFloat() * size().height ; calculerProba(posH, posV, g) ; g.drawOval((int)posH, (int)posV, 1, 1) ; } } } /* calcul de pi */ static Color color2=new Color(250,100,100); private void calculerProba(double posH, double posV, Graphics g) { Ntirage++ ; posH = posH-(size().height/2) ; posV = posV-(size().height/2) ; if ((posH*posH)+(posV*posV)<(size().height/2)*(size().height/2)) { Ntouche++ ; g.setColor(color2) ; } double approximationPi = 4.*(double)Ntouche/(double)Ntirage ; // met a jour la courbe a intervalle regulier compteur++ ; if (compteur>20) { compteur = 0 ; cp.nouveauPoint(approximationPi, Ntirage) ; } } // efface tous les points public void effacerImage() { if (imageHorsEcran==null) { imageHorsEcran = createImage(size().width, size().height) ; Graphics graphicsHorsEcran = imageHorsEcran.getGraphics() ; graphicsHorsEcran.setColor(Color.green) ; graphicsHorsEcran.fillOval(0,0,size().width-1,size().height-1) ; } Graphics graphicsHorsEcran = imageHorsEcran.getGraphics() ; graphicsHorsEcran.setColor(getBackground()) ; graphicsHorsEcran.fillRect(0,0,size().width,size().height) ; graphicsHorsEcran.setColor(Color.white) ; graphicsHorsEcran.fillOval(0,0,size().width-1,size().height-1) ; cp.demarrage() ; Ntirage = 0 ; Ntouche = 0 ; } } /**** objet servant a tracer la courbe indiquant l'evolution de pi ****/ final class courbepi extends spritecanvas { // vecteur contenant tous les differentes valeurs de pi Vector vecPoints = new Vector() ; // nombre de tirage private int Ntirage = 0 ; // dernier point modifier et simplifier int dernierPointSimplifie = 0 ; // compteur qui sert a simplifier la courbe a des intervalles reguliers int compteur = 0 ; /* createur de coube pi */ public courbepi() { resize(200,100) ; delai = 100 ; setBackground(new Color(235,235,235)) ; start() ; } /* enregistre un nouveau point */ public synchronized void nouveauPoint(double newPi, int Ntirage) { vecPoints.addElement(new Double(newPi)) ; this.Ntirage = Ntirage ; } /* dessine la courbe representant l'evolution de pi */ public void paint(Graphics g) { double largeur = size().width ; double hauteur = size().height ; g.setColor(Color.red) ; // valeur de pi g.drawLine(0, (int)hauteur/2, (int)largeur, (int)hauteur/2) ; // trace la courbe double posH = 0, posV = 0 ; double precedentPosH = 0 , precedentPosV = hauteur/2 ; g.setColor(Color.black) ; double n = 1 ; if (vecPoints.size()>0) { g.drawString("Approximation de Pi : " + ((Double)vecPoints.lastElement()).toString(), 15, 15) ; g.drawString("Nombre de tirage : " + Long.toString(Ntirage), 15, (int)hauteur-5) ; g.setColor(Color.blue) ; if (vecPoints.size()>size().width) { n = (double)vecPoints.size()/(double)largeur ; double iVec = 0 ; for (posH=0 ; posH