// tirageurne.java - jpq - 14/10/99 import java.awt.event.* ; import java.awt.* ; import java.applet.* ; import java.util.Random ; public class tirageurne extends java.applet.Applet { controles C ; dessin D ; public void init () { setLayout (new BorderLayout ()) ; int NTR = 1 ; String s = getParameter ("NTR") ; if (s != null) { try { NTR = Integer.parseInt (s) ; } catch (NumberFormatException nfe) { } if (NTR < 0) NTR = 0 ; } int NTV = 1 ; s = getParameter ("NTV") ; if (s != null) { try { NTV = Integer.parseInt (s) ; } catch (NumberFormatException nfe) { } if (NTV < 0) NTV = 0 ; } int somme = NTR + NTV ; int n = 1 ; s = getParameter ("n") ; if (s != null) { try { n = Integer.parseInt (s) ; } catch (NumberFormatException nfe) { } if (n < 0) n = 0 ; else if (n > somme) n = somme ; } D = new dessin (NTR, NTV, n) ; C = new controles (D) ; add (C, BorderLayout.NORTH) ; add (D, BorderLayout.CENTER) ; } public void destroy () { remove (D) ; remove (C) ; } public String getAppletInfo () { return "tirageurne par j.-p. Quelen" ; } //////////////////////////////////////////////////////////////////////////////// protected class dessin extends Canvas { Image img ; Graphics g ; int NTR, NTV, n, v, r, Somme ; boolean retrace, modeplus ; int L, H ; Random rnd ; public dessin (int NTR, int NTV, int n) { this.NTR = NTR ; this.NTV = NTV ; this.n = n ; rnd = new Random () ; } private void affiche (Graphics g, int a, int b) { for (int i = 0 ; i < a ; i ++) { int x = (int)(rnd.nextDouble () * (L - 220)) + 110 ; int y = (int)(rnd.nextDouble () * 20.0) + H - 80 ; g.fillRect (x, y, 3, 3) ; } for (int i = a ; i < b ; i ++) { int x = (int)(rnd.nextDouble () * (L - 220)) + 110 ; int y = (int)(rnd.nextDouble () * (H - 190)) + 60 ; g.fillRect (x, y, 3, 3) ; } } private void alea () { if ((int)(rnd.nextDouble () * Somme --) + 1 <= NTR - r) r ++ ; else v ++ ; } public void update (Graphics g) { paint (g) ; } public void paint (Graphics g1) { if (img == null) { L = getSize().width ; H = getSize().height ; img = createImage (L, H) ; g = img.getGraphics () ; g.setColor (Color.black) ; g.drawRect (0, 0, -- L, -- H) ; L -- ; H -- ; } g.setColor (Color.white) ; g.fillRect (1, 1, L, H) ; g.setColor (Color.blue) ; g.drawLine (L / 2 - 20, H - 100, L / 2 - 20, H - 120) ; g.drawLine (L / 2 - 20, H - 120, 100, H - 120) ; g.drawLine (100, H - 120, 100, 50) ; g.drawLine (100, 50, L - 100, 50) ; g.drawLine (L - 100, 50, L - 100, H - 120) ; g.drawLine (L - 100, H - 120, L / 2 + 20, H - 120) ; g.drawLine (L / 2 + 20, H - 120, L / 2 + 20, H - 100) ; g.drawLine (100, H - 80, 100, H - 50) ; g.drawLine (100, H - 50, L - 100, H - 50) ; g.drawLine (L - 100, H - 50, L - 100, H - 80) ; if (retrace) { if (modeplus) { modeplus = false ; alea () ; } else { Somme = NTR + NTV ; v = 0 ; r = 0 ; for (int i = 0 ; i < n ; i ++) if ((int)(rnd.nextDouble () * Somme --) + 1 <= NTR - r) r ++ ; else v ++ ; } g.setColor (Color.red) ; affiche (g, r, NTR) ; g.drawString (Integer.toString (r) + " boule(s) rouge(s)", 20, H - 20) ; g.setColor (Color.green) ; affiche (g, v, NTV) ; g.drawString (Integer.toString (v) + " boule(s) verte(s)", L / 2, H - 20) ; } g1.drawImage (img, 0, 0, this) ; } } //////////////////////////////////////////////////////////////////////////////// protected class controles extends Panel implements ActionListener { dessin D ; TextField tNTR, tNTV, tn ; Button ok, plus ; private TextField ajouttf (Font f, String s, int i) { Label l ; add (l = new Label (s)) ; l.setBackground (Color.lightGray) ; l.setFont (f) ; TextField T = new TextField (Integer.toString (i), 5) ; add (T) ; T.setFont (f) ; return T ; } public controles (dessin D) { this.D = D ; setLayout (new FlowLayout()) ; setBackground (Color.lightGray) ; Font f = new Font ("Arial", Font.PLAIN, 10) ; tNTR = ajouttf (f, "rouges :", D.NTR) ; tNTV = ajouttf (f, "vertes :", D.NTV) ; tn = ajouttf (f, "n =", D.n) ; add (ok = new Button ("Ok")) ; ok.addActionListener (this) ; add (plus = new Button ("+")) ; plus.addActionListener (this) ; } private int maj (TextField T, int n) { try { n = Integer.parseInt (T.getText ()) ; } catch (NumberFormatException nfe) { } if (n < 0) n = 0 ; return n ; } public void actionPerformed (ActionEvent e) { if (e.getSource () == ok) { D.NTR = maj (tNTR, D.NTR) ; D.NTV = maj (tNTV, D.NTV) ; tNTR.setText (Integer.toString (D.NTR)) ; tNTV.setText (Integer.toString (D.NTV)) ; int n = maj (tn, D.n) ; int Somme = D.NTR + D.NTV ; if (n > Somme) n = Somme ; D.n = n ; tn.setText (Integer.toString (n)) ; D.retrace = true ; D.repaint () ; } else if (e.getSource () == plus) { tNTR.setText (Integer.toString (D.NTR)) ; tNTV.setText (Integer.toString (D.NTV)) ; int Somme = D.NTR + D.NTV ; if (D.n < Somme) { D.n ++ ; D.modeplus = true ; } tn.setText (Integer.toString (D.n)) ; D.retrace = true ; D.repaint () ; } } } }