/** Ce programme est écrit en java, // Auteur : Serge Derhy , le 4/09/96 (sderhy@imaginet.fr ) */ /*************************************************************************/ import java.awt.* ; import java.util.Random; import java.applet.AudioClip; public class RollDice extends java.applet.Applet implements Runnable { // /**********************Instances variables**************************/ Font theFont = new Font("TimesRoman",Font.BOLD, 72); Random rnd= new Random(); Thread runner; AudioClip bgsound; int xpos = 30 ; String DiceValue ="" ; //*************************< méthode init() >********************/ public void init() { resize (300,200); setBackground(Color.pink); setForeground(Color.blue); add(new Button("Jeter le d\u00e9")); // \caractère unicode de é bgsound = getAudioClip(getCodeBase(), "audio/son.au");//son.au } /*****************************< methode paint() >***********/ public void paint(Graphics g) { g.setFont(theFont); g.drawString( DiceValue ,xpos,120); } //***************************start et stop overriding***************/ public void stop() { if (runner != null) { runner.stop(); runner = null; } } //********************************Méthode rollDice*****************/ public void rollDice () { //Pause de 200 millisecondes : try{Thread.sleep(100);} catch(InterruptedException e){} // jet de dé et repaint; int Dice = (rnd.nextInt() % 6) ; if (Dice < 0) Dice = -Dice ; Dice++; DiceValue = String.valueOf( Dice ); repaint(); } // fin de la méthode rollDice /******************Où on s'occupe du bouton ***********************/ public boolean action(Event evt, Object arg) { String nomDuBouton = null; if ( evt.target instanceof Button ) nomDuBouton=(String)arg ; if(nomDuBouton.equals("Jeter le d\u00e9")) { if (runner != null) { runner.stop(); runner = null ; } runner = new Thread(this); runner.start(); } return true; } /******************run()******************************/ public void run() { for (xpos= 80;xpos <180 ; xpos+=40) { if (bgsound != null) bgsound.play(); for ( int j = 1; j< 6 ; j++) rollDice(); } xpos = 80 ; } //**************************surclassement de update****/ public void update(Graphics g) { g.setColor(Color.white); g.fillRect (xpos-3, // x 120-77, // y 50, // width 120); // height g.setColor(Color.red); /* g.clipRect ( 0, // x 40, // y 300, // width 260); // height */ paint(g); } }// fin d'Applet.