home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09961.iso / java / mojo1-2e.exe / MOJODISK / DATA.4 / plugins / clock.def < prev    next >
Encoding:
Text File  |  1996-07-01  |  4.9 KB  |  223 lines

  1. DEF_COMPONENTNAME
  2. Clock
  3. DEF_SUPERCLASS
  4. Applet
  5. DEF_SUPERCOMPONENT
  6.  
  7. DEF_PACKAGE
  8. plugins
  9. time
  10. DEF_ENDLIST
  11. DEF_SUBCOMPONENTLIST
  12. DEF_ENDLIST
  13. DEF_SUBCOMPONENTCLASSLIST
  14. DEF_ENDLIST
  15. DEF_CATEGORY
  16. Time
  17. DEF_BITMAP
  18. clockm.bmp
  19. DEF_THUMBNAIL_UP
  20. clock.bmp
  21. DEF_THUMBNAIL_DOWN
  22. 2-clock.bmp
  23. DEF_VISUAL
  24. DEF_TOOL
  25. DEF_IMPORTS
  26. java.util.*
  27. DEF_ENDLIST
  28. DEF_REQUIRES
  29. DEF_ENDLIST
  30. DEF_IMPLEMENTS
  31. Runnable
  32. DEF_ENDLIST
  33. DEF_DECLARATION
  34. // A class that produces a clock .
  35. // author: Rachel Gollub, 1995
  36. // modified 96/04/24 Jim Hagen : use getBackground()
  37. // modified 96/05/29 Rachel Gollub : add garbage collecting
  38. // revised 6/26/96 by Tony Hartzler
  39. // Time!
  40. //  Variable Declarations
  41.  
  42.   Thread timer = null;
  43.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  44.   Date dummy = new Date();
  45.   String lastdate = dummy.toLocaleString();
  46.   Font F = new Font("TimesRoman", Font.PLAIN, 14);
  47.   Date dat = null;
  48. DEF_ENDLIST
  49. DEF_METHOD
  50. public void init() {
  51.   start();
  52. }
  53. DEF_ENDLIST
  54. DEF_METHOD
  55. void initialize() {
  56.   int x,y;
  57.   // resize(300,300);              // Set clock window size
  58.   start();
  59. }
  60. DEF_ENDLIST
  61. DEF_METHOD
  62. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  63. // Plotpoints allows calculation to only cover 45 degrees of the circle,
  64. // and then mirror
  65.  
  66.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  67.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  68.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  69.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  70.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  71.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  72.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  73.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  74. }
  75. DEF_ENDLIST
  76. DEF_METHOD
  77. public void circle(int x0, int y0, int r, Graphics g) {
  78.   // Circle is just Bresenham's algorithm for a scan converted circle
  79.  
  80.   int x,y;
  81.   float d;
  82.  
  83.   x=0;
  84.   y=r;
  85.   d=5/4-r;
  86.   plotpoints(x0,y0,x,y,g);
  87.  
  88.   while (y>x){
  89.     if (d<0) {
  90.       d=d+2*x+3;
  91.       x++;
  92.     }
  93.     else {
  94.       d=d+2*(x-y)+5;
  95.       x++;
  96.       y--;
  97.     }
  98.     plotpoints(x0,y0,x,y,g);
  99.   }
  100. }
  101. DEF_ENDLIST
  102. DEF_METHOD
  103. public void paint(Graphics g) {
  104.   // Paint is the main part of the program
  105.  
  106.   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
  107.   String today;
  108.  
  109.   dat = new Date();
  110.   s = dat.getSeconds();
  111.   m = dat.getMinutes();
  112.   h = dat.getHours();
  113.   today = dat.toLocaleString();
  114.   xcenter=80;
  115.   ycenter=55;
  116.   
  117.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  118.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  119.   
  120.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  121.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  122.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  123.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  124.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  125.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  126.   
  127.   // Draw the circle and numbers
  128.   
  129.   g.setFont(F);
  130.   g.setColor(Color.blue);
  131.   circle(xcenter,ycenter,50,g);
  132.   g.setColor(Color.darkGray);
  133.   g.drawString("9",xcenter-45,ycenter+3); 
  134.   g.drawString("3",xcenter+40,ycenter+3);
  135.   g.drawString("12",xcenter-5,ycenter-37);
  136.   g.drawString("6",xcenter-3,ycenter+45);
  137.  
  138.   // Erase if necessary, and redraw
  139.   
  140.   g.setColor(getBackground());
  141.   if (xs != lastxs || ys != lastys) {
  142.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  143.     g.drawString(lastdate, 5, 125);
  144.   }
  145.   if (xm != lastxm || ym != lastym) {
  146.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  147.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  148.   if (xh != lastxh || yh != lastyh) {
  149.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  150.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  151.   g.setColor(Color.darkGray);
  152.   g.drawString(today, 5, 125);  
  153.   g.drawLine(xcenter, ycenter, xs, ys);
  154.   g.setColor(Color.blue);
  155.   g.drawLine(xcenter, ycenter-1, xm, ym);
  156.   g.drawLine(xcenter-1, ycenter, xm, ym);
  157.   g.drawLine(xcenter, ycenter-1, xh, yh);
  158.   g.drawLine(xcenter-1, ycenter, xh, yh);
  159.   lastxs=xs; lastys=ys;
  160.   lastxm=xm; lastym=ym;
  161.   lastxh=xh; lastyh=yh;
  162.   lastdate = today;
  163.   dat=null;
  164. }
  165. DEF_ENDLIST
  166. DEF_METHOD
  167. public void start() {
  168.   if(timer == null)
  169.     {
  170.       timer = new Thread(this);
  171.       timer.start();
  172.     }
  173. }
  174. DEF_ENDLIST
  175. DEF_METHOD
  176. public void stop() {
  177.   timer = null;
  178. }
  179. DEF_ENDLIST
  180. DEF_METHOD
  181. public void run() {
  182.   while (timer != null) {
  183.     try {Thread.sleep(100);} catch (InterruptedException e){}
  184.     repaint();
  185.   }
  186.   timer = null;
  187. }
  188. DEF_ENDLIST
  189. DEF_METHOD
  190. public void update(Graphics g) {
  191.   paint(g);
  192. }
  193. DEF_ENDLIST
  194. DEF_PROPERTY
  195. Top
  196. int
  197. move(bounds().x, AVALUE);
  198. AVALUE = bounds().y;
  199. 0
  200. DEF_ENDLIST
  201. DEF_PROPERTY
  202. Left
  203. int
  204. move(AVALUE, bounds().y);
  205. AVALUE = bounds().x;
  206. 0
  207. DEF_ENDLIST
  208. DEF_PROPERTY
  209. Height
  210. int
  211. resize(bounds().width, AVALUE);
  212. AVALUE = bounds().height;
  213. 150
  214. DEF_ENDLIST
  215. DEF_PROPERTY
  216. Width
  217. int
  218. resize(AVALUE, bounds().height);
  219. AVALUE = bounds().width;
  220. 150
  221. DEF_ENDLIST
  222. DEF_ENDCOMPONENT
  223.