import java.awt.*; import java.awt.event.* ; import java.util.Random; /** * trace l'ensemble de Juia avec des couleurs aléatoires * possibilité de zoomer et de changer de paramètres * adapté jdk 1.2 le 01/04/99 - 01/04/03 * adapté jdk 6 le 12/03/2009 * * @ author Jean-Paul Quelen */ public class julia2 extends java.applet.Applet implements Runnable, MouseListener, ActionListener { int xmax, ymax, Niter, NCouleur, x, y, n, nm1 ; double Zoom, Rayon2, pas, xo, yo, parmjuliax, parmjuliay ; double Cy, Cx, Zx, Zy, Z ; int [] [] TCouleur ; Random r ; TextField tfx, tfy ; Label mult ; Button b ; Thread trace ; private double gparmd (String s, double d) { s = getParameter (s) ; if (s != null) { try { d = new Double (s) . doubleValue () ; } catch (NumberFormatException nfe) { } } return d ; } private double sd (String s) { return new Double (s).doubleValue () ; } public void init () { setCursor (new Cursor (Cursor.CROSSHAIR_CURSOR)) ; xmax = getSize().width; ymax = getSize().height; parmjuliax = gparmd ("parmjuliax", 0.1) ; parmjuliay = gparmd ("parmjuliay", 0.6) ; add (mult = new Label ()) ; Font f = new Font ("Arial", Font.PLAIN, 10) ; mult.setFont (f) ; mult.setBackground (Color.black) ; mult.setForeground (Color.white) ; add (b = new Button ("Nouveau tracé")); tfx = new TextField (10); tfx.setBackground (Color.white) ; // ajout 16/03/2000 add (tfx); tfy = new TextField (10); tfy.setBackground (Color.white) ; // ajout 16/03/2000 add (tfy) ; NCouleur = 256; Niter = 256 ; b.addActionListener (this) ; addMouseListener (this) ; setBackground (Color.black) ; // ajout 16/03/2000 init1 (); trace = new Thread (this) ; trace.start () ; } private void init1 () { // (ré)initialisation xo = yo = 0 ; Zoom = 1.0 ; y = ymax ; Rayon2 = 4.0 ; pas = 4.0 / Zoom / (xmax > ymax ? xmax : ymax) ; mult.setText ("x 1 ") ; // nouvelles couleurs TCouleur = new int [Niter] [3]; r = new Random (); int Niterm1 = Niter - 1; TCouleur [Niterm1] [0] = TCouleur [Niterm1] [1] = TCouleur [Niterm1] [2] = 0; for (n = 0; n < Niterm1; n++) for (int p = 0; p < 3; p++) TCouleur [n] [p] = Math.abs (r.nextInt ()) % NCouleur ; // affichage des paramètres tfx.setText (Double.toString (parmjuliax)) ; tfy.setText (Double.toString (parmjuliay)) ; } public void run () { while (true) repaint () ; } public void update (Graphics g) { paint (g) ; } public void paint (Graphics g) { if (y >= ymax) { y = 0 ; Cy = yo - ymax * pas / 2.0 ; } Cx = xo - xmax * pas / 2.0; for (x = 0 ; x < xmax ; x ++ ) { Zx = Cx ; Zy = Cy ; n = 1 ; while (((Zx * Zx + Zy * Zy) < Rayon2) & (n < Niter)) { Z = Zx * Zx - Zy * Zy + parmjuliax ; Zy = 2.0 * Zx * Zy + parmjuliay ; Zx = Z ; n++ ; } nm1 = n - 1; g.setColor (new Color (TCouleur [nm1] [0], TCouleur [nm1] [1], TCouleur [nm1] [2])) ; g.drawLine (x, y, x, y) ; Cx = Cx + pas ; } Cy = Cy + pas; y ++ ; } public void actionPerformed (ActionEvent e) { if (e.getSource () == b) { parmjuliax = sd (tfx.getText ()) ; parmjuliay = sd (tfy.getText ()) ; init1 () ; } } public void mousePressed (MouseEvent e) { e.consume () ; parmjuliax = sd (tfx.getText ()) ; parmjuliay = sd (tfy.getText ()) ; xo = pas * (e.getX () - xmax / 2.0) + xo ; yo = pas * (e.getY () - ymax / 2.0) + yo ; Zoom = Zoom * 2.0 ; pas = 4.0 / Zoom / (xmax > ymax ? xmax : ymax) ; y = ymax ; mult.setText ("x " + Integer.toString ((int)Zoom)) ; } public void mouseReleased (MouseEvent e) { } public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) { } public String getAppletInfo () { return "julia par j.-p. Quelen" ; } }