home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Netobjs / Install.exe / data1.cab / Components / TimeBasedApplet / TimeBased.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-16  |  8.3 KB  |  256 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Rectangle;
  10. import java.awt.image.ImageObserver;
  11. import java.net.URL;
  12. import java.util.Date;
  13.  
  14. public class TimeBased extends Applet implements Runnable {
  15.    Thread m_TimeBased;
  16.    private Graphics m_Graphics;
  17.    private Image[] m_Images;
  18.    private int m_nCurrImage;
  19.    private int m_nImgWidth;
  20.    private int m_nImgHeight;
  21.    private boolean m_fAllLoaded;
  22.    private boolean NetworkFiles;
  23.    private boolean didClick;
  24.    private int numImages;
  25.    private String[] imageNames;
  26.    private String[] imageURLs;
  27.    private int[] imageStarts;
  28.    private MediaTracker tracker;
  29.    protected Color backgroundColor;
  30.    protected Image backgroundImage;
  31.    protected int xOffset;
  32.    protected int yOffset;
  33.  
  34.    public void start() {
  35.       if (this.m_TimeBased == null) {
  36.          this.m_TimeBased = new Thread(this);
  37.          this.m_TimeBased.start();
  38.       }
  39.  
  40.    }
  41.  
  42.    private int convertBGRtoRGB(int BGRColor) {
  43.       int r = (BGRColor & 255) << 16;
  44.       int g = BGRColor & '\uff00';
  45.       int b = (BGRColor & 16711680) >> 16;
  46.       return r + g + b;
  47.    }
  48.  
  49.    public void stop() {
  50.       if (this.m_TimeBased != null) {
  51.          this.m_TimeBased.stop();
  52.          this.m_TimeBased = null;
  53.       }
  54.  
  55.    }
  56.  
  57.    public boolean mouseExit(Event evt, int x, int y) {
  58.       this.didClick = false;
  59.       return true;
  60.    }
  61.  
  62.    private String modifyStringContext(String param) {
  63.       if (param.startsWith(".")) {
  64.          param = param.replace('\\', '/');
  65.          String fullBase = ((Applet)this).getDocumentBase().toString();
  66.          param = fullBase.substring(0, fullBase.lastIndexOf(47)) + "/" + param;
  67.       } else if (param.startsWith("#")) {
  68.          param = ((Applet)this).getDocumentBase().toString() + param;
  69.       } else if (param.compareTo("javascript:void(0)") == 0) {
  70.          param = null;
  71.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  72.          param = "http://" + param;
  73.       }
  74.  
  75.       return param;
  76.    }
  77.  
  78.    public boolean mouseDown(Event evt, int x, int y) {
  79.       this.didClick = true;
  80.       return true;
  81.    }
  82.  
  83.    public boolean mouseEnter(Event evt, int x, int y) {
  84.       return true;
  85.    }
  86.  
  87.    private void displayImage(Graphics g) {
  88.       if (this.m_fAllLoaded) {
  89.          if (this.backgroundImage != null) {
  90.             int imHeight = this.backgroundImage.getHeight(this);
  91.             int imWidth = this.backgroundImage.getWidth(this);
  92.             Dimension d = ((Component)this).size();
  93.             int x = (d.width + this.xOffset) / imWidth + 1;
  94.             int y = (d.height + this.yOffset) / imHeight + 1;
  95.  
  96.             for(int i = 0; i < y; ++i) {
  97.                for(int j = 0; j < x; ++j) {
  98.                   g.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  99.                }
  100.             }
  101.          } else {
  102.             g.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  103.          }
  104.  
  105.          g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  106.       }
  107.    }
  108.  
  109.    public String getAppletInfo() {
  110.       return "Name: TimeBased\r\n";
  111.    }
  112.  
  113.    public boolean mouseUp(Event evt, int x, int y) {
  114.       if (this.didClick) {
  115.          try {
  116.             ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]));
  117.          } catch (Exception var6) {
  118.             ((Applet)this).getAppletContext().showStatus("Bad URL: " + this.imageURLs[this.m_nCurrImage]);
  119.          }
  120.       }
  121.  
  122.       return true;
  123.    }
  124.  
  125.    public void run() {
  126.       Date today = new Date(System.currentTimeMillis());
  127.       int currentHours = today.getHours();
  128.  
  129.       for(this.m_nCurrImage = 0; this.m_nCurrImage < this.numImages - 1 && this.imageStarts[this.m_nCurrImage + 1] <= currentHours; ++this.m_nCurrImage) {
  130.       }
  131.  
  132.       if (!this.m_fAllLoaded) {
  133.          ((Component)this).repaint();
  134.          this.m_Graphics = ((Component)this).getGraphics();
  135.          this.m_Images = new Image[this.numImages];
  136.          this.tracker = new MediaTracker(this);
  137.  
  138.          for(int i = 1; i <= this.numImages; ++i) {
  139.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), this.imageNames[i - 1]);
  140.             this.tracker.addImage(this.m_Images[i - 1], i - 1);
  141.          }
  142.  
  143.          try {
  144.             this.tracker.waitForID(this.m_nCurrImage);
  145.             this.m_fAllLoaded = !this.tracker.isErrorID(this.m_nCurrImage);
  146.          } catch (InterruptedException var5) {
  147.          }
  148.  
  149.          if (!this.m_fAllLoaded) {
  150.             this.m_Graphics.drawString("Error loading image!", 10, 40);
  151.             this.stop();
  152.             return;
  153.          }
  154.       }
  155.  
  156.       ((Component)this).repaint();
  157.  
  158.       while(true) {
  159.          try {
  160.             this.displayImage(this.m_Graphics);
  161.             today = new Date(System.currentTimeMillis());
  162.             currentHours = today.getHours();
  163.  
  164.             for(this.m_nCurrImage = 0; this.m_nCurrImage < this.numImages - 1 && this.imageStarts[this.m_nCurrImage + 1] <= currentHours; ++this.m_nCurrImage) {
  165.             }
  166.  
  167.             this.tracker.waitForID(this.m_nCurrImage);
  168.             this.m_fAllLoaded = !this.tracker.isErrorID(this.m_nCurrImage);
  169.             if (!this.m_fAllLoaded) {
  170.                this.stop();
  171.                this.m_Graphics.drawString("Error loading image!", 10, 40);
  172.                return;
  173.             }
  174.  
  175.             Thread.sleep(300000L);
  176.          } catch (InterruptedException var6) {
  177.             this.stop();
  178.          }
  179.       }
  180.    }
  181.  
  182.    public void destroy() {
  183.    }
  184.  
  185.    public void init() {
  186.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  187.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  188.       this.didClick = false;
  189.       this.numImages = Integer.parseInt(((Applet)this).getParameter("Number of Images"));
  190.       this.imageNames = new String[this.numImages];
  191.       this.imageURLs = new String[this.numImages];
  192.       this.imageStarts = new int[this.numImages];
  193.  
  194.       for(int cnt = 0; cnt < this.numImages; ++cnt) {
  195.          this.imageNames[cnt] = ((Applet)this).getParameter("Image " + (cnt + 1));
  196.          if (this.imageNames[cnt] != null && this.imageNames[cnt].startsWith("file://///")) {
  197.             this.NetworkFiles = true;
  198.          }
  199.  
  200.          this.imageURLs[cnt] = ((Applet)this).getParameter("URL for Image " + (cnt + 1));
  201.          if (this.imageURLs[cnt] != null) {
  202.             if (this.imageURLs[cnt].startsWith(".")) {
  203.                String theBase = ((Applet)this).getDocumentBase().toExternalForm();
  204.                int lastSlash = theBase.lastIndexOf(47);
  205.                this.imageURLs[cnt] = theBase.substring(0, lastSlash + 1) + this.imageURLs[cnt];
  206.             } else if (this.imageURLs[cnt].compareTo("javascript:void(0)") == 0) {
  207.                this.imageURLs[cnt] = null;
  208.             } else if (!this.imageURLs[cnt].startsWith("cid:") && !this.imageURLs[cnt].startsWith("lifn:") && !this.imageURLs[cnt].startsWith("java:") && !this.imageURLs[cnt].startsWith("irc:") && !this.imageURLs[cnt].startsWith("IOR:") && !this.imageURLs[cnt].startsWith("ilu:") && !this.imageURLs[cnt].startsWith("https:") && !this.imageURLs[cnt].startsWith("http:") && !this.imageURLs[cnt].startsWith("hdl:") && !this.imageURLs[cnt].startsWith("gopher:") && !this.imageURLs[cnt].startsWith("ftp:") && !this.imageURLs[cnt].startsWith("finger:") && !this.imageURLs[cnt].startsWith("file:") && !this.imageURLs[cnt].startsWith("data:") && !this.imageURLs[cnt].startsWith("clsid:") && !this.imageURLs[cnt].startsWith("md5:") && !this.imageURLs[cnt].startsWith("mailserver:") && !this.imageURLs[cnt].startsWith("mailto:") && !this.imageURLs[cnt].startsWith("mid:") && !this.imageURLs[cnt].startsWith("news:") && !this.imageURLs[cnt].startsWith("nntp:") && !this.imageURLs[cnt].startsWith("path:") && !this.imageURLs[cnt].startsWith("prospero:") && !this.imageURLs[cnt].startsWith("service:") && !this.imageURLs[cnt].startsWith("shttp") && !this.imageURLs[cnt].startsWith("snews") && !this.imageURLs[cnt].startsWith("STANF:") && !this.imageURLs[cnt].startsWith("telnet:") && !this.imageURLs[cnt].startsWith("vemmi:") && !this.imageURLs[cnt].startsWith("wais:") && !this.imageURLs[cnt].startsWith("whois++:")) {
  209.                this.imageURLs[cnt] = "http://" + this.imageURLs[cnt];
  210.             }
  211.          }
  212.  
  213.          this.imageStarts[cnt] = Integer.parseInt(((Applet)this).getParameter("Start Time for Image " + (cnt + 1)));
  214.       }
  215.  
  216.       if (((Applet)this).getParameter("BackgroundColor") != null) {
  217.          int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  218.          backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  219.          this.backgroundColor = new Color(backgroundcolor);
  220.          ((Component)this).setBackground(this.backgroundColor);
  221.       } else if (((Applet)this).getParameter("BackgroundImage") != null) {
  222.          String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  223.  
  224.          try {
  225.             this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  226.          } catch (Exception var6) {
  227.             System.out.println("Error forming URL for background image");
  228.             return;
  229.          }
  230.  
  231.          MediaTracker tracker = new MediaTracker(this);
  232.          tracker.addImage(this.backgroundImage, 0);
  233.  
  234.          try {
  235.             tracker.waitForID(0);
  236.          } catch (InterruptedException var5) {
  237.             System.out.println("Background image loading interrupted");
  238.          }
  239.       }
  240.  
  241.    }
  242.  
  243.    public void paint(Graphics g) {
  244.       if (this.m_fAllLoaded) {
  245.          Rectangle r = g.getClipRect();
  246.          g.clearRect(r.x, r.y, r.width, r.height);
  247.          this.displayImage(g);
  248.       } else if (this.NetworkFiles) {
  249.          g.drawString("Unable to load images over Windows network!", 10, 20);
  250.       } else {
  251.          g.drawString("Loading images...", 10, 20);
  252.       }
  253.  
  254.    }
  255. }
  256.