Jean-Louis
PATANÉ

jlp photo

[ CV ]

trefle
Fondation de la Vocation

Les Pages Web
Les Pages Web

Mailto
m'écrire

NETSCAPE 2
il y a longtemps...

worldnet
mon ISP 1996-2002 Worldnet
 
maintenant chez
free
mon ISP 2002 FREE



accueil sites java faq vocation jeux musique bouquins photos

La page Java

embauchez moi... (CV)

dernière mise à jour de ce trop vieux site : le 27 Octobre 2005

Je m'étais fait un petit plaisir, pour les fêtes de fin d'année (1996), en réalisant l'Applet de Noël qui orna mes pages durant quelques semaines.

sapin
Applet de Noël

Quelques Emails sympas me les ont demandés, alors voici les sources.

J'ai tout d'abord pompé une image de sapin sur le net J'ai ensuite dessiné la neige tout seul comme un grand :-) J'ai enfin pu m'amuser à pondre le code comme il me plaît.

/**********************************************************/
/*                                                        */
/*    fichier JLP0appletSapin.java                        */
/*                                                        */
/*        créé en Décembre 1996 par Jean-Louis PATANÉ     */
/*        Web   : http://home.worldnet.fr/jlp             */
/*        Email : jlp@worldnet.fr                         */
/*                                                        */
/**********************************************************/

import java.awt.Graphics;
import java.awt.Image ;
import java.awt.MediaTracker ;
import java.awt.Color ;
import java.util.Random ;
import java.lang.Math ;

public class JLP0appletSapin
   extends java.applet.Applet
   implements Runnable
   {
   // variables de classe
   private static Image sapin = null ;
   private static Image neige = null ;
   private static Image zbuffer = null ;
   private static Graphics z ;
   private static final int[] tx = { 25,22,32,20,30,25,35,
      23,18,29,21,35,38,12,22,39,31,34,24,16 } ;
   private static final int[] ty = { 15,23,24,27,29,31,34,
      37,40,44,46,47,48,53,53,53,56,57,58,61 } ;
   private static final int[] tc = {  0, 1, 1, 2, 3, 4, 4,
       1, 4, 5, 5, 2, 1, 3, 2, 0, 6, 5, 4, 4 } ;
   private static final int nb = tx.length ;
   private static Color[] tb_color =
      {
      new Color(255,255,255),   // 0 blanc
      new Color(255,  0,  0),   // 1 rouge
      new Color(255,255,  0),   // 2 jaune
      new Color(255,  0,255),   // 3 magenta
      new Color(  0,255,255),   // 4 cyan
      new Color(  0,255,  0),   // 5 vert
      new Color(  0,127,255)    // 6 bleu
      } ;
   private static int[] tt =
      {
      0,   0,-3, 0, 3,   // e == 1
      1,   0,-5, 0, 5,   // e == 2
      2,   0,-7, 0, 7,   // e == 3
      3,  -2,-7, 2, 7,   // e == 4
      3,  -5,-7, 5, 7,   // e == 5
      2,  -7,-7, 7, 7,   // e == 6
      1,  -5,-5, 5, 5,   // e == 7
      0,  -3,-3, 3, 3    // e == 8
      } ;
   private static boolean flag_load = true ;
   // variables d'instances
   private Graphics g ;
   private Thread runner ;
   private MediaTracker tracker ;
   private int x,y ;
   private Random r ;
   private int[] te = {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
   private boolean flag_init = true ;

   public void init()
      {
      if (flag_init)
         {
         flag_init = false ;
         resize(51,80) ;
         x = 0 ;
         y = 0 ;
         r = new Random(System.currentTimeMillis()) ;
         g = getGraphics() ;
         if (flag_load)
            {
            zbuffer = createImage(51,80) ;
            z = zbuffer.getGraphics() ;
            tracker = new MediaTracker(this) ;
            sapin = getImage(getCodeBase(),"sapin1.gif") ;
            neige = getImage(getCodeBase(),"neige.gif") ;
            tracker.addImage(sapin,0) ;
            tracker.addImage(neige,0) ;
            }
         }
      }

   public void paint(Graphics _g)
      {
      }

   int rand_free()
      {
      int i = Math.abs(r.nextInt()) % nb ;
      while (i < nb && te[i] != 0)
         {
         i ++ ;
         }
      if (i >= nb)
         {
         i = 0 ;
         while (i < nb && te[i] != 0)
            {
            i ++ ;
            }
         }
      return i ;
      }

   void rand_ajout()
      {
      int n = Math.abs(r.nextInt()) % 12 ;
      if (n < 3)
         {
         while (n > 0)
            {
            int i = rand_free() ;
            if (i < nb)
               {
               te[i] = 1 ;
               }
            n -- ;
            }
         }
      }

   void maj_spots(Graphics g)
      {
      rand_ajout() ;
      for ( int i = 0 ; i < nb ; i ++ )
         {
         if (te[i] > 0)
            {
            te[i] ++ ;
            if (spot(g,tx[i],ty[i],tc[i],te[i]))
               {
               te[i] = 0 ;
               }
            }
         }
      }

   boolean spot(Graphics g, int x, int y, int c, int e)
      {
      // e = 1 + (e - 1) / 2 ;
      int i = (e - 1) * 5 ;
      g.setColor(tb_color[c]) ;
      int r = tt[i] ;
      g.fillOval(x-r,y-r,2*r,2*r) ;
      r *= 3 ;
      g.drawLine(x+tt[i+1],y+tt[i+2],x+tt[i+3],y+tt[i+4]) ;
      g.drawLine(x+tt[i+2],y+tt[i+3],x+tt[i+4],y+tt[i+1]) ;
      return e >= 8 ;
      }

   public void start()
      {
      if (runner == null)
         {
         runner = new Thread(this) ;
         runner.start() ;
         }
      }

   public void stop()
      {
      if (runner != null)
         {
         runner.stop() ;
         runner = null ;
         }
      }

   public void destroy()
      {
      if (runner != null)
         {
         runner.stop() ;
         runner = null ;
         }
      }

   void incr()
      {
      y ++ ;
      if (y >= 80)
         {
         y = 0 ;
         }
      x ++ ;
      if (x >= 51)
         {
         x = 0 ;
         }
      }

   public void run()
      {
      if (flag_load)
         {
         try
            {
            tracker.waitForID(0) ;
            flag_load = false ;
            }
         catch (InterruptedException e)
            {
            }
         }
      g = getGraphics() ;
      while (true)
         {
         try
            {
            Thread.sleep(50) ;
            }
         catch (InterruptedException e)
            {
            }
         z.drawImage(sapin,0,0,this) ;
         maj_spots(z) ;
         z.drawImage(neige,x-51,y   ,this) ;
         z.drawImage(neige,x-51,y-80,this) ;
         z.drawImage(neige,x   ,y   ,this) ;
         z.drawImage(neige,x   ,y-80,this) ;
         incr() ;
         g.drawImage(zbuffer,0,0,this) ;
         }
      }

   }

/**********************************************************/
/*                                                        */
/*    fin du fichier JLP0appletSapin.java                 */
/*                                                        */
/*        créé en Décembre 1996 par Jean-Louis PATANÉ     */
/*        Web   : http://home.worldnet.fr/jlp             */
/*        Email : jlp@worldnet.fr                         */
/*                                                        */
/**********************************************************/

accueil sites java faq vocation jeux musique bouquins photos

...