home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Netobjs / Install.exe / data1.cab / Components / RotatingPictureComp / RotatingPicture.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-16  |  8.3 KB  |  247 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.  
  13. public class RotatingPicture extends Applet implements Runnable {
  14.    Thread m_RotatingPicture;
  15.    private Graphics m_Graphics;
  16.    private Image[] m_Images;
  17.    private int m_nCurrImage;
  18.    private int m_nImgWidth;
  19.    private int m_nImgHeight;
  20.    private boolean m_fAllLoaded;
  21.    private boolean NetworkFiles;
  22.    private boolean didClick;
  23.    private int pauseTime;
  24.    private int numImages;
  25.    private String[] imageNames;
  26.    private String[] imageURLs;
  27.    protected Color backgroundColor;
  28.    protected Image backgroundImage;
  29.    protected int xOffset;
  30.    protected int yOffset;
  31.  
  32.    public void start() {
  33.       if (this.m_RotatingPicture == null) {
  34.          this.m_RotatingPicture = new Thread(this);
  35.          this.m_RotatingPicture.start();
  36.       }
  37.  
  38.    }
  39.  
  40.    private int convertBGRtoRGB(int BGRColor) {
  41.       int r = (BGRColor & 255) << 16;
  42.       int g = BGRColor & '\uff00';
  43.       int b = (BGRColor & 16711680) >> 16;
  44.       return r + g + b;
  45.    }
  46.  
  47.    public void stop() {
  48.       if (this.m_RotatingPicture != null) {
  49.          this.m_RotatingPicture.stop();
  50.          this.m_RotatingPicture = null;
  51.       }
  52.  
  53.    }
  54.  
  55.    public boolean mouseExit(Event evt, int x, int y) {
  56.       this.didClick = false;
  57.       return true;
  58.    }
  59.  
  60.    private String modifyStringContext(String param) {
  61.       if (param.startsWith(".")) {
  62.          param = param.replace('\\', '/');
  63.          String fullBase = ((Applet)this).getDocumentBase().toString();
  64.          param = fullBase.substring(0, fullBase.lastIndexOf(47)) + "/" + param;
  65.       } else if (param.startsWith("#")) {
  66.          param = ((Applet)this).getDocumentBase().toString() + param;
  67.       } else if (param.compareTo("javascript:void(0)") == 0) {
  68.          param = null;
  69.       } 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++:")) {
  70.          param = "http://" + param;
  71.       }
  72.  
  73.       return param;
  74.    }
  75.  
  76.    public boolean mouseDown(Event evt, int x, int y) {
  77.       this.didClick = true;
  78.       return true;
  79.    }
  80.  
  81.    public boolean mouseEnter(Event evt, int x, int y) {
  82.       return true;
  83.    }
  84.  
  85.    private void displayImage(Graphics g) {
  86.       if (this.m_fAllLoaded) {
  87.          if (this.backgroundImage != null) {
  88.             int imHeight = this.backgroundImage.getHeight(this);
  89.             int imWidth = this.backgroundImage.getWidth(this);
  90.             Dimension d = ((Component)this).size();
  91.             Image theBuffer = ((Component)this).createImage(d.width, d.height);
  92.             Graphics gr = theBuffer.getGraphics();
  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.                   gr.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  99.                }
  100.             }
  101.  
  102.             gr.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);
  103.             g.drawImage(theBuffer, 0, 0, this);
  104.          } else {
  105.             g.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  106.             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);
  107.          }
  108.  
  109.       }
  110.    }
  111.  
  112.    public String getAppletInfo() {
  113.       return "Name: RotatingPicture\r\n" + "Author: Alex Kogon\r\n" + "";
  114.    }
  115.  
  116.    public boolean mouseUp(Event evt, int x, int y) {
  117.       if (this.didClick) {
  118.          try {
  119.             String target = ((Applet)this).getParameter("Target" + (this.m_nCurrImage + 1));
  120.             if (target != null) {
  121.                ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]), target);
  122.             } else {
  123.                ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]));
  124.             }
  125.          } catch (Exception var6) {
  126.             ((Applet)this).getAppletContext().showStatus("Bad URL: " + this.imageURLs[this.m_nCurrImage]);
  127.          }
  128.       }
  129.  
  130.       return true;
  131.    }
  132.  
  133.    public void run() {
  134.       this.m_nCurrImage = 0;
  135.       if (!this.m_fAllLoaded) {
  136.          ((Component)this).repaint();
  137.          this.m_Graphics = ((Component)this).getGraphics();
  138.          this.m_Images = new Image[this.numImages];
  139.          MediaTracker tracker = new MediaTracker(this);
  140.  
  141.          for(int i = 1; i <= this.numImages; ++i) {
  142.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), this.imageNames[i - 1]);
  143.             tracker.addImage(this.m_Images[i - 1], 0);
  144.          }
  145.  
  146.          try {
  147.             tracker.waitForAll();
  148.             this.m_fAllLoaded = !tracker.isErrorAny();
  149.          } catch (InterruptedException var4) {
  150.          }
  151.  
  152.          if (!this.m_fAllLoaded) {
  153.             this.stop();
  154.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  155.             return;
  156.          }
  157.       }
  158.  
  159.       ((Component)this).repaint();
  160.  
  161.       while(true) {
  162.          try {
  163.             this.displayImage(this.m_Graphics);
  164.             Thread.sleep((long)this.pauseTime);
  165.             ++this.m_nCurrImage;
  166.             if (this.m_nCurrImage == this.numImages) {
  167.                this.m_nCurrImage = 0;
  168.             }
  169.          } catch (Exception var5) {
  170.             this.stop();
  171.          }
  172.       }
  173.    }
  174.  
  175.    public void destroy() {
  176.    }
  177.  
  178.    public void init() {
  179.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  180.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  181.       this.didClick = false;
  182.       this.pauseTime = 1000 * Integer.parseInt(((Applet)this).getParameter("Pause Time"));
  183.       this.numImages = Integer.parseInt(((Applet)this).getParameter("Number of Images"));
  184.       this.imageNames = new String[this.numImages];
  185.       this.imageURLs = new String[this.numImages];
  186.  
  187.       for(int cnt = 0; cnt < this.numImages; ++cnt) {
  188.          this.imageNames[cnt] = ((Applet)this).getParameter("Image " + (cnt + 1));
  189.          if (this.imageNames[cnt] != null && this.imageNames[cnt].startsWith("file://///")) {
  190.             this.NetworkFiles = true;
  191.          }
  192.  
  193.          this.imageURLs[cnt] = ((Applet)this).getParameter("URL for Image " + (cnt + 1));
  194.          if (this.imageURLs[cnt] != null) {
  195.             if (this.imageURLs[cnt].startsWith(".")) {
  196.                String theBase = ((Applet)this).getDocumentBase().toExternalForm();
  197.                int lastSlash = theBase.lastIndexOf(47);
  198.                this.imageURLs[cnt] = theBase.substring(0, lastSlash + 1) + this.imageURLs[cnt];
  199.             } else if (this.imageURLs[cnt].compareTo("javascript:void(0)") == 0) {
  200.                this.imageURLs[cnt] = null;
  201.             } 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++:")) {
  202.                this.imageURLs[cnt] = "http://" + this.imageURLs[cnt];
  203.             }
  204.          }
  205.       }
  206.  
  207.       if (((Applet)this).getParameter("BackgroundColor") != null) {
  208.          int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  209.          backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  210.          this.backgroundColor = new Color(backgroundcolor);
  211.          ((Component)this).setBackground(this.backgroundColor);
  212.       } else if (((Applet)this).getParameter("BackgroundImage") != null) {
  213.          String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  214.  
  215.          try {
  216.             this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  217.          } catch (Exception var6) {
  218.             System.out.println("Error forming URL for background image");
  219.             return;
  220.          }
  221.  
  222.          MediaTracker tracker = new MediaTracker(this);
  223.          tracker.addImage(this.backgroundImage, 0);
  224.  
  225.          try {
  226.             tracker.waitForID(0);
  227.          } catch (InterruptedException var5) {
  228.             System.out.println("Background image loading interrupted");
  229.          }
  230.       }
  231.  
  232.    }
  233.  
  234.    public void paint(Graphics g) {
  235.       if (this.m_fAllLoaded) {
  236.          Rectangle r = g.getClipRect();
  237.          g.clearRect(r.x, r.y, r.width, r.height);
  238.          this.displayImage(g);
  239.       } else if (this.NetworkFiles) {
  240.          g.drawString("Unable to load images over Windows network!", 10, 20);
  241.       } else {
  242.          g.drawString("Loading images...", 10, 20);
  243.       }
  244.  
  245.    }
  246. }
  247.