home *** CD-ROM | disk | FTP | other *** search
/ Internet Gallery / INTERGAL.bin / intergal / prgs / idv21 / data.z / bigtime.java < prev    next >
Text File  |  1995-10-08  |  15KB  |  925 lines

  1. import java.awt.*;
  2.  
  3. import java.applet.*;
  4.  
  5. import java.util.Date;
  6.  
  7.  
  8.  
  9. /* 
  10.  
  11.   Date: February 8th, 1996
  12.  
  13.  
  14.  
  15.   Version 1.0b1
  16.  
  17.  
  18.  
  19.   Updateed:
  20.  
  21.  
  22.  
  23.     Encompassed all functionality in the same file. Previously
  24.  
  25.     I had a number of different object classes that did things
  26.  
  27.     like provide the behaviour of LEDs. But this seemed to make things
  28.  
  29.     a little difficult for people to install.
  30.  
  31.  
  32.  
  33.   Date: December 17th, 1995
  34.  
  35.  
  36.  
  37.   Version: 1.0a1
  38.  
  39.  
  40.  
  41.   Description:
  42.  
  43.      BigTime uses the BigLEDdigit class to display a rather
  44.  
  45.    large LED clock. BigLEDdigit is a metaphor of the 7 segment
  46.  
  47.    LED generally used in "old-fashioned" digital clocks.
  48.  
  49.      BigTime is based on the Clock class which basically
  50.  
  51.    implements a runnable thread that is executed every 1/4
  52.  
  53.    second, or so, in order to regularly update the appearance
  54.  
  55.    of the clock.
  56.  
  57.  
  58.  
  59.   Coder: John Criswick, Ottawa, criswick@conveyor.com
  60.  
  61.  
  62.  
  63.   Copyright (c) 1995, John R. Criswick. All rights reserved.
  64.  
  65.  
  66.  
  67. */
  68.  
  69.  
  70.  
  71. public class bigtime extends Applet implements Runnable {
  72.  
  73.   int      lastHour, lastMinute, lastSecond;
  74.  
  75.   Thread   tickTock;
  76.  
  77.   int      speed = 1000;  // Update every 1 second
  78.  
  79.   int      lastTenHour, lastTenMinute, lastTenSecond;
  80.  
  81.   Color    LEDColour;
  82.  
  83.   Color    BackGround;
  84.  
  85.   Color    FrameColour;
  86.  
  87.  
  88.  
  89.   public String getAppletInfo() {
  90.  
  91.  
  92.  
  93.     return new String("BigClock Ver 1.0b1 Feb.8.96 by John Criswick, Ottawa, criswick@conveyor.com");
  94.  
  95.   }
  96.  
  97.  
  98.  
  99.   public void init() {
  100.  
  101.     String  param;
  102.  
  103.  
  104.  
  105.     lastHour = 0;
  106.  
  107.     lastMinute=0;
  108.  
  109.     lastSecond=0;
  110.  
  111.     lastTenHour=0;
  112.  
  113.     lastTenMinute=0;
  114.  
  115.     lastTenSecond=0;
  116.  
  117.  
  118.  
  119.     param = getParameter("backcolor");
  120.  
  121.     BackGround = (param == null) ? ABackgroundcolor : GetColourFromString(param);
  122.  
  123.     param = getParameter("ledcolor");
  124.  
  125.     LEDColour = (param == null) ? AForegroundcolor : GetColourFromString(param);
  126.  
  127.     param = getParameter("framecolor");
  128.  
  129.     FrameColour = (param == null) ? AFramecolor : GetColourFromString(param);
  130.  
  131.  
  132.  
  133.     resize(120, 36);
  134.  
  135.  
  136.  
  137.   }
  138.  
  139.  
  140.  
  141.   /* Given a string like "Green" or "green" this function returns
  142.  
  143.     the actual Color value corresponding.
  144.  
  145.   */
  146.  
  147.   public Color GetColourFromString(String theParam) {
  148.  
  149.  
  150.  
  151.     if (theParam.equalsIgnoreCase("BLACK")) {
  152.  
  153.       return Color.black;
  154.  
  155.     } else if (theParam.equalsIgnoreCase("BLUE")) {
  156.  
  157.       return Color.blue;
  158.  
  159.     } else if (theParam.equalsIgnoreCase("CYAN")) {
  160.  
  161.       return Color.cyan;
  162.  
  163.     } else if (theParam.equalsIgnoreCase("DARKGRAY")) {
  164.  
  165.       return Color.darkGray;
  166.  
  167.     } else if (theParam.equalsIgnoreCase("GRAY")) {
  168.  
  169.       return Color.gray;
  170.  
  171.     } else if (theParam.equalsIgnoreCase("GREEN")) {
  172.  
  173.       return Color.green;
  174.  
  175.     } else if (theParam.equalsIgnoreCase("LIGHTGRAY")) {
  176.  
  177.       return Color.lightGray;
  178.  
  179.     } else if (theParam.equalsIgnoreCase("MAGENTA")) {
  180.  
  181.       return Color.magenta;
  182.  
  183.     } else if (theParam.equalsIgnoreCase("ORANGE")) {
  184.  
  185.       return Color.orange;
  186.  
  187.     } else if (theParam.equalsIgnoreCase("PINK")) {
  188.  
  189.       return Color.pink;
  190.  
  191.     } else if (theParam.equalsIgnoreCase("RED")) {
  192.  
  193.       return Color.red;
  194.  
  195.     } else if (theParam.equalsIgnoreCase("WHITE")) {
  196.  
  197.       return Color.white;
  198.  
  199.     } else if (theParam.equalsIgnoreCase("YELLOW")) {
  200.  
  201.       return Color.yellow;
  202.  
  203.     }
  204.  
  205.  
  206.  
  207.     return Color.black;   // as a default
  208.  
  209.  
  210.  
  211.   }
  212.  
  213.   public void paint(Graphics g) {
  214.  
  215.     Date  theDate = new Date();
  216.  
  217.     Color oldColour;
  218.  
  219.  
  220.  
  221.     oldColour = g.getColor();
  222.  
  223.     g.setColor(BackGround);
  224.  
  225.     g.fillRect(0, 0, bounds().width-1, bounds().height-1);
  226.  
  227.     g.setColor(FrameColour);
  228.  
  229.     g.drawRect(0, 0, bounds().width-1, bounds().height-1);
  230.  
  231.     DrawClock(g, theDate); 
  232.  
  233.     g.setColor(oldColour);
  234.  
  235.  
  236.  
  237.   }
  238.  
  239.  
  240.  
  241.   /* Updates the clock appearance. This method overides the
  242.  
  243.     same method in the Clock class since we only want to draw
  244.  
  245.     the portion of the clock that changed (as oposed to redrawing
  246.  
  247.     the entire clock as the Clock method would do).
  248.  
  249.   */
  250.  
  251.   public final synchronized void update (Graphics g) {
  252.  
  253.     int theHour, theMinute, theSecond;
  254.  
  255.     int theTenHour, theTenMinute, theTenSecond;
  256.  
  257.     Color oldColour;
  258.  
  259.     Date  aDate = new Date();
  260.  
  261.  
  262.  
  263.     theSecond = aDate.getSeconds();
  264.  
  265.     theMinute = aDate.getMinutes();
  266.  
  267.     theHour = aDate.getHours();
  268.  
  269.     theTenHour = theHour / 10;
  270.  
  271.     theHour = theHour - theTenHour * 10;
  272.  
  273.     theTenMinute = theMinute / 10;
  274.  
  275.     theMinute = theMinute - theTenMinute * 10;
  276.  
  277.     theTenSecond = theSecond / 10;
  278.  
  279.     theSecond = theSecond - theTenSecond * 10;
  280.  
  281.  
  282.  
  283.     /* Here we only draw digits that need redrawing because they
  284.  
  285.        have changed since the last time through this code.
  286.  
  287.     */
  288.  
  289.  
  290.  
  291.     oldColour = g.getColor();
  292.  
  293.  
  294.  
  295.     if (theSecond != lastSecond)
  296.  
  297.       {
  298.  
  299.       DrawSecond(theSecond);
  300.  
  301.       lastSecond = theSecond;
  302.  
  303.       }
  304.  
  305.     if (theTenSecond != lastTenSecond)
  306.  
  307.       {
  308.  
  309.       DrawTenSecond(theTenSecond);
  310.  
  311.       lastTenSecond = theTenSecond;
  312.  
  313.       }
  314.  
  315.     if (theMinute != lastMinute)
  316.  
  317.       {
  318.  
  319.       DrawMinute(theMinute);
  320.  
  321.       lastMinute = theMinute;
  322.  
  323.       }
  324.  
  325.     if (theTenMinute != lastTenMinute)
  326.  
  327.       {
  328.  
  329.       DrawTenMinute(theTenMinute);
  330.  
  331.       lastTenMinute = theTenMinute;
  332.  
  333.       }
  334.  
  335.     if (theHour != lastHour)
  336.  
  337.       {
  338.  
  339.       DrawHour(theHour);
  340.  
  341.       lastHour = theHour;
  342.  
  343.       }
  344.  
  345.     if (theTenHour != lastTenHour)
  346.  
  347.       {
  348.  
  349.       DrawTenHour(theTenHour);
  350.  
  351.       lastTenHour = theTenHour;
  352.  
  353.       }
  354.  
  355.  
  356.  
  357.     DrawColons();
  358.  
  359.  
  360.  
  361.     g.setColor(oldColour);
  362.  
  363.  
  364.  
  365.   }
  366.  
  367.  
  368.  
  369.   /* Draw everything in the clock. Typically called when we get
  370.  
  371.     a screen update. Not called when the time has changed.
  372.  
  373.   */
  374.  
  375.   public synchronized void DrawClock (Graphics g, Date aDate) {
  376.  
  377.     int theHour, theMinute, theSecond;
  378.  
  379.     int theTenHour, theTenMinute, theTenSecond;
  380.  
  381.  
  382.  
  383.     theSecond = aDate.getSeconds();
  384.  
  385.     theMinute = aDate.getMinutes();
  386.  
  387.     theHour = aDate.getHours();
  388.  
  389.     theTenHour = theHour / 10;
  390.  
  391.     theHour = theHour - theTenHour * 10;
  392.  
  393.     theTenMinute = theMinute / 10;
  394.  
  395.     theMinute = theMinute - theTenMinute * 10;
  396.  
  397.     theTenSecond = theSecond / 10;
  398.  
  399.     theSecond = theSecond - theTenSecond * 10;
  400.  
  401.  
  402.  
  403.     /* Now draw every digit in the clock
  404.  
  405.     */
  406.  
  407.     DrawSecond(theSecond);
  408.  
  409.     DrawTenSecond(theTenSecond);
  410.  
  411.     DrawMinute(theMinute);
  412.  
  413.     DrawTenMinute(theTenMinute);
  414.  
  415.     DrawHour(theHour);
  416.  
  417.     DrawTenHour(theTenHour);
  418.  
  419.     DrawColons();
  420.  
  421.  
  422.  
  423.   }
  424.  
  425.  
  426.  
  427.   /* Following set up size methods are used to draw each of the
  428.  
  429.     six digits in the clock. Each digit has its own draw method
  430.  
  431.     because we want to be able to draw only those parts of the
  432.  
  433.     clock that have changed.
  434.  
  435.   */
  436.  
  437.   public synchronized void DrawSecond(int theValue) {
  438.  
  439.     DrawSevenSegments(getGraphics(), theValue, 99, 5);
  440.  
  441.   }
  442.  
  443.  
  444.  
  445.   public synchronized void DrawTenSecond(int theValue) {
  446.  
  447.     DrawSevenSegments(getGraphics(), theValue, 81, 5);
  448.  
  449.   }
  450.  
  451.  
  452.  
  453.   public synchronized void DrawMinute(int theValue) {
  454.  
  455.     DrawSevenSegments(getGraphics(), theValue, 61, 5);
  456.  
  457.   }
  458.  
  459.  
  460.  
  461.   public synchronized void DrawTenMinute(int theValue) {
  462.  
  463.     DrawSevenSegments(getGraphics(), theValue, 43, 5);
  464.  
  465.   }
  466.  
  467.  
  468.  
  469.   public synchronized void DrawHour(int theValue) {
  470.  
  471.     DrawSevenSegments(getGraphics(), theValue, 23, 5);
  472.  
  473.   }
  474.  
  475.  
  476.  
  477.   public synchronized void DrawTenHour(int theValue) {
  478.  
  479.     DrawSevenSegments(getGraphics(), theValue, 5, 5);
  480.  
  481.   }
  482.  
  483.  
  484.  
  485.   /* Draw the 2 pairs of colons between the hours and minutes digits
  486.  
  487.     and the minutes and seconds digits.
  488.  
  489.   */
  490.  
  491.   public synchronized void DrawColons() {
  492.  
  493.     Graphics g = getGraphics();
  494.  
  495.  
  496.  
  497.     g.setColor(LEDColour);
  498.  
  499.     g.fillRect(39, 13, 2, 2);
  500.  
  501.     g.fillRect(39, 22, 2, 2);
  502.  
  503.     g.fillRect(77, 13, 2, 2);
  504.  
  505.     g.fillRect(77, 22, 2, 2);
  506.  
  507.  
  508.  
  509.   }
  510.  
  511.  
  512.  
  513.   private synchronized void DrawSevenSegments(Graphics g, int theValue, int x, int y) {
  514.  
  515.  
  516.  
  517.     //  Need to implement a state machine to turn on the correct LED
  518.  
  519.     // segments to draw the number correctly.
  520.  
  521.     //  This is a rather simple way to implement the state machine,
  522.  
  523.     // but it was quick to code. Its based on a switch statement for
  524.  
  525.     // each of ten possible digits.
  526.  
  527.  
  528.  
  529.     switch (theValue) {
  530.  
  531.       case 0:
  532.  
  533.         DrawSegment0On(g, x, y);
  534.  
  535.         DrawSegment1On(g, x, y);
  536.  
  537.         DrawSegment2On(g, x, y);
  538.  
  539.         DrawSegment3Off(g, x, y);
  540.  
  541.         DrawSegment4On(g, x, y);
  542.  
  543.         DrawSegment5On(g, x, y);
  544.  
  545.         DrawSegment6On(g, x, y);
  546.  
  547.         break;
  548.  
  549.       case 1:
  550.  
  551.         DrawSegment0Off(g, x, y);
  552.  
  553.         DrawSegment1Off(g, x, y);
  554.  
  555.         DrawSegment2On(g, x, y);
  556.  
  557.         DrawSegment3Off(g, x, y);
  558.  
  559.         DrawSegment4Off(g, x, y);
  560.  
  561.         DrawSegment5On(g, x, y);
  562.  
  563.         DrawSegment6Off(g, x, y);
  564.  
  565.         break;
  566.  
  567.       case 2:
  568.  
  569.         DrawSegment0On(g, x, y);
  570.  
  571.         DrawSegment1Off(g, x, y);
  572.  
  573.         DrawSegment2On(g, x, y);
  574.  
  575.         DrawSegment3On(g, x, y);
  576.  
  577.         DrawSegment4On(g, x, y);
  578.  
  579.         DrawSegment5Off(g, x, y);
  580.  
  581.         DrawSegment6On(g, x, y);
  582.  
  583.         break;
  584.  
  585.       case 3:
  586.  
  587.         DrawSegment0On(g, x, y);
  588.  
  589.         DrawSegment1Off(g, x, y);
  590.  
  591.         DrawSegment2On(g, x, y);
  592.  
  593.         DrawSegment3On(g, x, y);
  594.  
  595.         DrawSegment4Off(g, x, y);
  596.  
  597.         DrawSegment5On(g, x, y);
  598.  
  599.         DrawSegment6On(g, x, y);
  600.  
  601.         break;
  602.  
  603.       case 4:
  604.  
  605.         DrawSegment0Off(g, x, y);
  606.  
  607.         DrawSegment1On(g, x, y);
  608.  
  609.         DrawSegment2On(g, x, y);
  610.  
  611.         DrawSegment3On(g, x, y);
  612.  
  613.         DrawSegment4Off(g, x, y);
  614.  
  615.         DrawSegment5On(g, x, y);
  616.  
  617.         DrawSegment6Off(g, x, y);
  618.  
  619.         break;
  620.  
  621.       case 5:
  622.  
  623.         DrawSegment0On(g, x, y);
  624.  
  625.         DrawSegment1On(g, x, y);
  626.  
  627.         DrawSegment2Off(g, x, y);
  628.  
  629.         DrawSegment3On(g, x, y);
  630.  
  631.         DrawSegment4Off(g, x, y);
  632.  
  633.         DrawSegment5On(g, x, y);
  634.  
  635.         DrawSegment6On(g, x, y);
  636.  
  637.         break;
  638.  
  639.       case 6:
  640.  
  641.         DrawSegment0On(g, x, y);
  642.  
  643.         DrawSegment1On(g, x, y);
  644.  
  645.         DrawSegment2Off(g, x, y);
  646.  
  647.         DrawSegment3On(g, x, y);
  648.  
  649.         DrawSegment4On(g, x, y);
  650.  
  651.         DrawSegment5On(g, x, y);
  652.  
  653.         DrawSegment6On(g, x, y);
  654.  
  655.         break;
  656.  
  657.       case 7:
  658.  
  659.         DrawSegment0On(g, x, y);
  660.  
  661.         DrawSegment1Off(g, x, y);
  662.  
  663.         DrawSegment2On(g, x, y);
  664.  
  665.         DrawSegment3Off(g, x, y);
  666.  
  667.         DrawSegment4Off(g, x, y);
  668.  
  669.         DrawSegment5On(g, x, y);
  670.  
  671.         DrawSegment6Off(g, x, y);
  672.  
  673.         break;
  674.  
  675.       case 8:
  676.  
  677.         DrawSegment0On(g, x, y);
  678.  
  679.         DrawSegment1On(g, x, y);
  680.  
  681.         DrawSegment2On(g, x, y);
  682.  
  683.         DrawSegment3On(g, x, y);
  684.  
  685.         DrawSegment4On(g, x, y);
  686.  
  687.         DrawSegment5On(g, x, y);
  688.  
  689.         DrawSegment6On(g, x, y);
  690.  
  691.         break;
  692.  
  693.       case 9:
  694.  
  695.         DrawSegment0On(g, x, y);
  696.  
  697.         DrawSegment1On(g, x, y);
  698.  
  699.         DrawSegment2On(g, x, y);
  700.  
  701.         DrawSegment3On(g, x, y);
  702.  
  703.         DrawSegment4Off(g, x, y);
  704.  
  705.         DrawSegment5On(g, x, y);
  706.  
  707.         DrawSegment6Off(g, x, y);
  708.  
  709.         break;
  710.  
  711.     }
  712.  
  713.       
  714.  
  715.   }
  716.  
  717.  
  718.  
  719.   /* Segments 0, 3 and 6 are the horizontal line segments. Segments
  720.  
  721.     1, 2, 4 and 5 are the vertical line segments.
  722.  
  723.   */
  724.  
  725.   private synchronized void DrawSegment0On(Graphics g, int x, int y) {
  726.  
  727.     g.setColor(LEDColour);
  728.  
  729.     DrawHorizontalLine(g, x+3, y+1, 9);
  730.  
  731.   }
  732.  
  733.   private synchronized void DrawSegment0Off(Graphics g, int x, int y) {
  734.  
  735.     g.setColor(BackGround);
  736.  
  737.     DrawHorizontalLine(g, x+3, y+1, 9);
  738.  
  739.   }
  740.  
  741.   private synchronized void DrawSegment3On(Graphics g, int x, int y) {
  742.  
  743.     g.setColor(LEDColour); 
  744.  
  745.     DrawHorizontalLine(g, x+3, y+12, 9); 
  746.  
  747.   }
  748.  
  749.   private synchronized void DrawSegment3Off(Graphics g, int x, int y) {
  750.  
  751.     g.setColor(BackGround);
  752.  
  753.     DrawHorizontalLine(g, x+3, y+12, 9);
  754.  
  755.   }
  756.  
  757.   private synchronized void DrawSegment6On(Graphics g, int x, int y) {
  758.  
  759.     g.setColor(LEDColour); 
  760.  
  761.     DrawHorizontalLine(g, x+3, y+23, 9); 
  762.  
  763.   }
  764.  
  765.   private synchronized void DrawSegment6Off(Graphics g, int x, int y) {
  766.  
  767.     g.setColor(BackGround);
  768.  
  769.     DrawHorizontalLine(g, x+3, y+23, 9);
  770.  
  771.   }
  772.  
  773.  
  774.  
  775.   private synchronized void DrawSegment1On(Graphics g, int x, int y) { 
  776.  
  777.     g.setColor(LEDColour);  
  778.  
  779.     DrawVerticalLine(g, x+1, y+3, 9);  
  780.  
  781.   }
  782.  
  783.   private synchronized void DrawSegment1Off(Graphics g, int x, int y) { 
  784.  
  785.     g.setColor(BackGround);
  786.  
  787.     DrawVerticalLine(g, x+1, y+3, 9); 
  788.  
  789.   }
  790.  
  791.   private synchronized void DrawSegment2On(Graphics g, int x, int y) { 
  792.  
  793.     g.setColor(LEDColour);  
  794.  
  795.     DrawVerticalLine(g, x+12, y+3, 9);  
  796.  
  797.   }
  798.  
  799.   private synchronized void DrawSegment2Off(Graphics g, int x, int y) { 
  800.  
  801.     g.setColor(BackGround);
  802.  
  803.     DrawVerticalLine(g, x+12, y+3, 9); 
  804.  
  805.   }
  806.  
  807.   private synchronized void DrawSegment4On(Graphics g, int x, int y) { 
  808.  
  809.     g.setColor(LEDColour);  
  810.  
  811.     DrawVerticalLine(g, x+1, y+14, 9);  
  812.  
  813.   }
  814.  
  815.   private synchronized void DrawSegment4Off(Graphics g, int x, int y) { 
  816.  
  817.     g.setColor(BackGround);
  818.  
  819.     DrawVerticalLine(g, x+1, y+14, 9); 
  820.  
  821.   } 
  822.  
  823.   private synchronized void DrawSegment5On(Graphics g, int x, int y) { 
  824.  
  825.     g.setColor(LEDColour);  
  826.  
  827.     DrawVerticalLine(g, x+12, y+14, 9);  
  828.  
  829.   }
  830.  
  831.   private synchronized void DrawSegment5Off(Graphics g, int x, int y) { 
  832.  
  833.     g.setColor(BackGround);
  834.  
  835.     DrawVerticalLine(g, x+12, y+14, 9); 
  836.  
  837.   } 
  838.  
  839.  
  840.  
  841.   private synchronized void DrawHorizontalLine(Graphics g, int x, int y, int width) {
  842.  
  843.     g.drawLine(x+1, y-1, x+width-3, y-1);
  844.  
  845.     g.drawLine(x, y, x+width-1, y);
  846.  
  847.     g.drawLine(x+1, y+1, x+width-3, y+1);
  848.  
  849.   }
  850.  
  851.  
  852.  
  853.   private synchronized void DrawVerticalLine(Graphics g, int x, int y, int height) {
  854.  
  855.     g.drawLine(x-1, y+1, x-1, y+height-3);
  856.  
  857.     g.drawLine(x, y, x, y+height-1);
  858.  
  859.     g.drawLine(x+1, y+1, x+1, y+height-3);
  860.  
  861.   }
  862.  
  863.  
  864.  
  865.  
  866.  
  867.   /* Code to operate the thread
  868.  
  869.   */
  870.  
  871.   public void start() {
  872.  
  873.     if (tickTock == null) {
  874.  
  875.       tickTock = new Thread(this);
  876.  
  877.       tickTock.start();
  878.  
  879.     }
  880.  
  881.   }
  882.  
  883.  
  884.  
  885.   public void stop() {
  886.  
  887.     if (tickTock != null) {
  888.  
  889.       tickTock.stop();
  890.  
  891.       tickTock = null;
  892.  
  893.     }
  894.  
  895.   }
  896.  
  897.  
  898.  
  899.   public void run() {
  900.  
  901.  
  902.  
  903.     while (true) {
  904.  
  905.       try {
  906.  
  907.         Thread.currentThread().sleep(speed);
  908.  
  909.       }  
  910.  
  911.       catch (InterruptedException e) {
  912.  
  913.       }  
  914.  
  915.       super.repaint();
  916.  
  917.     }
  918.  
  919.   }
  920.  
  921.  
  922.  
  923. }
  924.  
  925.