acNeilson.java
上传用户:fulorde
上传日期:2007-01-07
资源大小:10k
文件大小:6k
源码类别:

Applet

开发平台:

Java

  1. /* acNeilson.java  bugly@interserv.com March 1996
  2.  *
  3.  *                 Changed name from CrazyCounter.java to acNeilson.java
  4.  *                 Improved readability (because I'm so Alden Bugly) maybe
  5.  *
  6.  *                 Note, number is not a parem
  7.  *
  8.  *                 Calling syntax that works for me is:
  9.  *
  10.  *   <applet codebase="file:///c|/Html/AldenBugly/Class"
  11.  *           code=acNeilson.class 
  12.  *           width=75 
  13.  *           height=20
  14.  *           >
  15.  *           <param name=NUMBER_FRAMES value=10>
  16.  *           <param name=NUMBER_WIDTH value=15>
  17.  *           <param name=NUMBER_HEIGHT value=20>
  18.  *           <param name=HOW_MANY value=5>
  19.  *           <param name=DELAY value=1000>
  20.  *           <parem name=VALUE value=777777>
  21.  *   </applet>
  22.  *
  23.  *   Passing VALUE (priming the pump) doesn't seem to work.
  24.  *
  25.  *   Renamed Numbers.gif to acNeilson.gif and PSP'd it so the counter
  26.  *   and my schtick are harmonious.
  27. */
  28. /* CrazyCounter.java */
  29. /* 
  30.  * Copyright (C) 1996 Mark Boyns <boyns@sdsu.edu>
  31.  *
  32.  * CrazyCounter
  33.  * <URL:http://www.sdsu.edu/~boyns/java/CrazyCounter/>
  34.  *
  35.  * This program is free software; you can redistribute it and/or modify
  36.  * it under the terms of the GNU General Public License as published by
  37.  * the Free Software Foundation; either version 2 of the License, or
  38.  * (at your option) any later version.
  39.  *
  40.  * This program is distributed in the hope that it will be useful,
  41.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43.  * GNU General Public License for more details.
  44.  *
  45.  * You should have received a copy of the GNU General Public License
  46.  * along with this program; if not, write to the Free Software
  47.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  48.  */
  49. import java.applet.*;
  50. import java.awt.*;
  51. import java.net.*;
  52. public class acNeilson extends java.applet.Applet implements Runnable
  53. {
  54.     Thread thread = null;
  55.     MediaTracker tracker = null;
  56.     Image numbers;
  57.     int number_frames;
  58.     int number_width;
  59.     int number_height;
  60.     int how_many;
  61.     int delay;
  62.     String value;
  63.     boolean first = true;
  64.     int state = 0;
  65.     public void init ()
  66.     {
  67. String param;
  68. tracker = new MediaTracker (this);
  69. try
  70. {
  71.     numbers = getImage(getCodeBase(),"../Images/acNeilson.gif");
  72.     tracker.addImage (numbers, 0);
  73.     
  74.     param = getParameter ("NUMBER_FRAMES");
  75.     number_frames = Integer.parseInt (param);
  76.     
  77.     param = getParameter ("NUMBER_WIDTH");
  78.     number_width = Integer.parseInt (param);
  79.     
  80.     param = getParameter ("NUMBER_HEIGHT");
  81.     number_height = Integer.parseInt (param);
  82.     param = getParameter ("HOW_MANY");
  83.     how_many = Integer.parseInt (param);
  84.     param = getParameter ("DELAY");
  85.     delay = Integer.parseInt (param);
  86.     value = getParameter ("VALUE");
  87.     if (value == null)
  88.     {
  89. value = String.valueOf ((long)(Math.random () * (long)(Math.pow (10, how_many))));
  90.     }
  91. }
  92. catch (Exception e)
  93. {
  94.     return;
  95. }
  96. resize ((how_many * number_width), number_height);
  97. thread = new Thread (this);
  98. thread.start ();
  99.     }
  100.     public void stop ()
  101.     {
  102. if (thread != null)
  103. {
  104.     thread.stop ();
  105.     thread = null;
  106. }
  107.     }
  108.     public void run ()
  109.     {
  110. tracker.checkAll (true);
  111. for (;;)
  112. {
  113.     repaint ();
  114.     try
  115.     {
  116. Thread.sleep (delay);
  117.     }
  118.     catch (Exception e)
  119.     {
  120.     }
  121. }
  122.     }
  123.     /* Handle mouse events. */
  124.     public boolean mouseDown (Event e, int x, int y)
  125.     {
  126. if (thread != null)
  127.         {
  128.             thread.stop ();
  129.             thread = null;
  130.         }
  131.         else
  132.         {
  133.             thread = new Thread (this);
  134.             thread.start ();
  135.         }
  136. return true;
  137.     }
  138.     
  139.     /* Handle keyboard events. */
  140.     public boolean keyDown (Event e, int key)
  141.     {
  142. switch (key)
  143. {
  144. case '+': // faster
  145.     delay -= 100;
  146.     if (delay < 10)
  147.     {
  148. delay = 10;
  149.     }
  150.     break;
  151. case '-': // slower
  152.     delay += 100;
  153.     break;
  154. case '0':
  155.     value = String.valueOf (0);
  156.     state = 0;
  157.     break;
  158. default:
  159.     value = String.valueOf ((long)(Math.random () * (long)(Math.pow (10, how_many))));
  160.     state = 0;
  161.     break;
  162. }
  163. return true;
  164.     }
  165.     /* Don't clear the screen; just call paint. */
  166.     public void update (Graphics g)
  167.     {
  168. paint (g);
  169.     }
  170.     /* Paint the screen. */
  171.     public void paint (Graphics g)
  172.     {
  173. int i;
  174. int digit;
  175. boolean scroll;
  176. if (tracker.checkAll () == false)
  177. {
  178.     g.setColor (Color.black);
  179.     g.fillRect (0, 0, (how_many * number_width), number_height);
  180.     return;
  181. }
  182. int zeros = how_many - value.length ();
  183. for (i = 0; i < zeros; i++)
  184. {
  185.     Graphics gc = g.create (i * number_width, 0, number_width, number_height);
  186.     gc.drawImage (numbers, 0, 0, this);
  187.     gc.dispose ();
  188. }
  189. scroll = true;
  190. for (i = value.length () - 1; i >= 0; i--)
  191. {
  192.     digit = value.charAt (i) - '0';
  193.     
  194.     Graphics gc = g.create ((zeros+i) * number_width, 0, number_width, number_height);
  195.     if (scroll)
  196.     {
  197. switch (state)
  198. {
  199. case 0:
  200.     if (digit == 0)
  201.     {
  202. gc.drawImage (numbers, 0, -((9 * number_height) + number_height/2), this);
  203.     }
  204.     gc.drawImage (numbers, 0, -((digit * number_height) - number_height/2), this);
  205.     break;
  206. case 1:
  207.     gc.drawImage (numbers, 0, -(digit * number_height), this);
  208.     break;
  209. case 2:
  210.     gc.drawImage (numbers, 0, -((digit * number_height) + number_height/2), this);
  211.     if (digit == 9)
  212.     {
  213. gc.drawImage (numbers, 0, -((0 * number_height) - number_height/2), this);
  214.     }
  215.     break;
  216. }
  217.     }
  218.     else
  219.     {
  220. gc.drawImage (numbers, 0, -(digit * number_height), this);
  221.     }
  222.     
  223.     gc.dispose ();
  224.     if (digit != 9)
  225.     {
  226. scroll = false;
  227.     }
  228. }
  229. if (state >= 2)
  230. {
  231.     state = 0;
  232.     long l = Long.parseLong (value);
  233.     l++;
  234.     value = String.valueOf (l);
  235. }
  236. state++;
  237.     }
  238. }
  239. /*
  240. Local variables:
  241. eval: (progn (make-local-variable 'compile-command) (setq compile-command (concat "javac " buffer-file-name)))
  242. End:
  243. */