home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / main / tsoltime.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.1 KB  |  120 lines

  1. // TsolTime.java
  2. // 22.03.96
  3. //
  4. // one of the times at which the metro comes
  5.  
  6. package cybcerone.main;
  7.  
  8. import java.io.DataInputStream;
  9. import java.io.IOException;
  10. import java.awt.Graphics;
  11. import java.awt.image.ImageObserver;
  12.  
  13. import cybcerone.utils.Date;
  14. import cybcerone.utils.Literate;
  15. import cybcerone.utils.Scaler;
  16.  
  17. /** 
  18.  * My own little time class to make keeping track of the Tsol easier.
  19.  */
  20. class TsolTime implements Literate {
  21.   private int hour;
  22.   private int minute;
  23.   private int second;
  24.  
  25.   /* value entirely in seconds */
  26.   private int value;
  27.  
  28.   /* to speed up painting */
  29.   private int min1, min2, sec1, sec2;
  30.  
  31.   private int xIncr1 = Scaler.scale (15);
  32.   private int xIncr2 = Scaler.scale (30);
  33.   private int xIncr3 = Scaler.scale (40);
  34.   private int xIncr4 = Scaler.scale (55);
  35.   private int xIncr5 = Scaler.scale (73);
  36.  
  37.   private String line;  /* for reading */
  38.  
  39.   private TsolTime (int hour, int minute, int second, int value) {
  40.     this.hour = hour;
  41.     this.minute = minute;
  42.     this.second = second;
  43.     this.value = value;
  44.  
  45.     min1 = minute / 10;
  46.     min2 = minute % 10;
  47.     sec1 = second / 10;
  48.     sec2 = second % 10;
  49.   }
  50.  
  51.   public TsolTime (int hour, int minute, int second) {
  52.     this (hour, minute, second, secondValue (hour, minute, second));
  53.   }
  54.   
  55.   private static int secondValue (int hour, int minute, int second) {
  56.     return (hour * 3600 + minute * 60 + second);
  57.   }
  58.  
  59.   public TsolTime (int hour, int minute) {
  60.     this (hour, minute, 0);
  61.   }
  62.  
  63.   /* in seconds, from midnight */
  64.   public TsolTime (int value) {
  65.     this (value / 3600, (value % 3600) / 60, (value % 3600) % 60, value);
  66.   }
  67.  
  68.   public TsolTime (Date theDate) {
  69.     this (theDate.getHours (), theDate.getMinutes (), theDate.getSeconds ());
  70.   }
  71.   
  72.   /* for reading */
  73.   public TsolTime () {
  74.   }
  75.   
  76.   /* return true if this is before other */
  77.   public boolean before (TsolTime other) {
  78.     return (this.value < other.value);
  79.   }
  80.  
  81.   /* returns true if this is after other */
  82.   public boolean after (TsolTime other) {
  83.     return (this.value > other.value);
  84.   }
  85.   
  86.   /* this minus other, the difference in time */
  87.   public TsolTime minus (TsolTime other) {
  88.     return new TsolTime (this.value - other.value);
  89.   }
  90.   
  91.   public TsolTime minus (Date other) {
  92.     return this.minus (new TsolTime (other));
  93.   }
  94.   
  95.   public Object read (DataInputStream inStream) throws IOException {
  96.     line = inStream.readLine ();
  97.     
  98.     if (line != null)
  99.       return (new TsolTime (Integer.parseInt (line.substring (0, 2)),
  100.                 Integer.parseInt (line.substring (3))));
  101.     else
  102.       return null;
  103.   }
  104.   
  105.   public String toString () {
  106.     return ("TsolTime[" + hour + ":" + minute + ":" + second + "]");
  107.   }
  108.   
  109.   public void paint (Graphics g, int x, int y, 
  110.              TsolNumImages images, ImageObserver parent) {
  111.     g.drawImage (images.getDigit (min1), x, y, parent);
  112.     g.drawImage (images.getDigit (min2), x + xIncr1, y, parent);
  113.     g.drawImage (images.getMinutes (), x + xIncr2, y, parent);
  114.     g.drawImage (images.getDigit (sec1), x + xIncr3, y, parent);
  115.     g.drawImage (images.getDigit (sec2), x + xIncr4, y, parent);
  116.     g.drawImage (images.getSeconds (), x + xIncr5, y, parent);
  117.   }
  118.   
  119. }
  120.