home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / MORPHTEA.EXE / MORPHTEA.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-01  |  7.7 KB  |  305 lines

  1. import java.applet.Applet;
  2. import java.awt.Button;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Event;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.MediaTracker;
  10. import java.awt.Rectangle;
  11. import java.awt.image.ImageObserver;
  12.  
  13. public class MORPHTEA extends Applet implements Runnable {
  14.    Thread m_MORPHTEA;
  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 int m_nDelay = 50;
  22.    private int m_nOffset = 15;
  23.    private Button m_PrevButton = new Button("Prev");
  24.    private Button m_NextButton = new Button("Next");
  25.    private Button m_playButton = new Button("Play");
  26.    private String m_sbuttonStatus = "Play";
  27.    private int m_frps = 20;
  28.    private String m_imageFile = "";
  29.    private String m_imageType = "jpg";
  30.    private int m_numImages = 16;
  31.    private Color m_backgroundColor;
  32.    private String m_sOrder;
  33.    private String m_sShowButtons;
  34.    private final String PARAM_frps;
  35.    private final String PARAM_imageFile;
  36.    private final String PARAM_imageType;
  37.    private final String PARAM_numImages;
  38.    private final String PARAM_backgroundColor;
  39.    private final String PARAM_imageOrder;
  40.    private final String PARAM_showButtons;
  41.  
  42.    public boolean mouseMove(Event evt, int x, int y) {
  43.       this.m_nDelay = 1000 / (x * 50 / ((Component)this).size().width + 1);
  44.       return true;
  45.    }
  46.  
  47.    public void stop() {
  48.       if (this.m_MORPHTEA != null) {
  49.          this.m_MORPHTEA.stop();
  50.          this.m_MORPHTEA = null;
  51.       }
  52.  
  53.    }
  54.  
  55.    public boolean mouseEnter(Event evt, int x, int y) {
  56.       return true;
  57.    }
  58.  
  59.    public boolean mouseExit(Event evt, int x, int y) {
  60.       this.m_nDelay = 1000 / this.m_frps;
  61.       return true;
  62.    }
  63.  
  64.    public void paint(Graphics g) {
  65.       if (this.m_fAllLoaded) {
  66.          Rectangle r = g.getClipRect();
  67.          g.clearRect(r.x, r.y, r.width, r.height);
  68.          this.displayImage(g);
  69.       } else {
  70.          g.drawString("Loading images...", 10, 40);
  71.       }
  72.  
  73.    }
  74.  
  75.    public boolean mouseUp(Event evt, int x, int y) {
  76.       return true;
  77.    }
  78.  
  79.    public String[][] getParameterInfo() {
  80.       String[][] info = new String[][]{{"frps", "int", "Frames pr second"}, {"imageFile", "String", "Image Files"}, {"imageType", "String", "Image Type"}, {"numImages", "int", "Number of Images"}, {"backgroundColor", "Color", "Background Color"}, {"imageOrder", "String", "Image Order"}, {"showButtons", "String", "Show Buttons"}};
  81.       return info;
  82.    }
  83.  
  84.    public void destroy() {
  85.    }
  86.  
  87.    private void displayImage(Graphics g) {
  88.       if (this.m_fAllLoaded) {
  89.          if (this.m_sOrder.compareTo("circular") != 0 && this.m_sOrder.compareTo("linear") != 0) {
  90.             g.drawImage(this.m_Images[(int)(Math.random() * (double)this.m_numImages)], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2, (ImageObserver)null);
  91.          } else if (this.m_nCurrImage >= this.m_numImages && this.m_sOrder.compareTo("linear") != 0) {
  92.             g.drawImage(this.m_Images[2 * this.m_numImages - 2 - this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2 + this.m_nOffset, (ImageObserver)null);
  93.          } else {
  94.             g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2 + this.m_nOffset, (ImageObserver)null);
  95.          }
  96.  
  97.       }
  98.    }
  99.  
  100.    public void start() {
  101.       if (this.m_MORPHTEA == null) {
  102.          this.m_MORPHTEA = new Thread(this);
  103.          this.m_MORPHTEA.start();
  104.       }
  105.  
  106.    }
  107.  
  108.    public String getAppletInfo() {
  109.       return "Name: MORPHTEA\r\n" + "Author: Jim Andrews (jandrews@islandnet.com)";
  110.    }
  111.  
  112.    public boolean action(Event event, Object obj) {
  113.       Object oTarget = event.target;
  114.       if (!(oTarget instanceof Button)) {
  115.          return false;
  116.       } else {
  117.          Button buttonTarget = (Button)oTarget;
  118.          String sButtonString = buttonTarget.getLabel();
  119.          if (sButtonString.compareTo("Prev") == 0) {
  120.             if (this.m_sbuttonStatus != "Play") {
  121.                this.m_nCurrImage += -1;
  122.                if (this.m_nCurrImage == -1) {
  123.                   this.m_nCurrImage = 0;
  124.                }
  125.             }
  126.  
  127.             this.m_sbuttonStatus = "Prev";
  128.             return true;
  129.          } else if (sButtonString.compareTo("Next") != 0) {
  130.             if (sButtonString.compareTo("Play") == 0) {
  131.                this.m_sbuttonStatus = "Play";
  132.                return true;
  133.             } else {
  134.                return false;
  135.             }
  136.          } else {
  137.             if (this.m_sbuttonStatus != "Play") {
  138.                ++this.m_nCurrImage;
  139.                if (this.m_nCurrImage > 2 * this.m_numImages - 3 && this.m_sOrder.compareTo("circular") == 0) {
  140.                   this.m_nCurrImage = 2 * this.m_numImages - 3;
  141.                } else if (this.m_sOrder.compareTo("linear") == 0 && this.m_nCurrImage > this.m_numImages - 1) {
  142.                   this.m_nCurrImage = 0;
  143.                }
  144.             }
  145.  
  146.             this.m_sbuttonStatus = "Next";
  147.             return true;
  148.          }
  149.       }
  150.    }
  151.  
  152.    public boolean mouseDown(Event evt, int x, int y) {
  153.       return true;
  154.    }
  155.  
  156.    public void run() {
  157.       this.m_nCurrImage = 0;
  158.       if (!this.m_fAllLoaded) {
  159.          ((Component)this).repaint();
  160.          this.m_Graphics = ((Component)this).getGraphics();
  161.          this.m_Images = new Image[this.m_numImages];
  162.          MediaTracker tracker = new MediaTracker(this);
  163.  
  164.          for(int i = 0; i < this.m_numImages; ++i) {
  165.             String strImage = this.m_imageFile + (i < 10 ? "0" : "") + i + "." + this.m_imageType;
  166.             this.m_Images[i] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), strImage);
  167.             tracker.addImage(this.m_Images[i], 0);
  168.          }
  169.  
  170.          try {
  171.             tracker.waitForAll();
  172.             this.m_fAllLoaded = !tracker.isErrorAny();
  173.          } catch (InterruptedException var5) {
  174.          }
  175.  
  176.          if (!this.m_fAllLoaded) {
  177.             this.stop();
  178.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  179.             return;
  180.          }
  181.  
  182.          this.m_nImgWidth = this.m_Images[0].getWidth(this);
  183.          this.m_nImgHeight = this.m_Images[0].getHeight(this);
  184.       }
  185.  
  186.       ((Component)this).repaint();
  187.  
  188.       while(true) {
  189.          try {
  190.             this.displayImage(this.m_Graphics);
  191.             if (this.m_sbuttonStatus == "Play") {
  192.                ++this.m_nCurrImage;
  193.             }
  194.  
  195.             if (this.m_sOrder.compareTo("circular") == 0 && this.m_nCurrImage > 2 * this.m_numImages - 3) {
  196.                this.m_nCurrImage = 0;
  197.             } else if (this.m_nCurrImage > this.m_numImages - 1 && this.m_sOrder.compareTo("linear") == 0) {
  198.                this.m_nCurrImage = 0;
  199.             }
  200.  
  201.             Thread.sleep((long)this.m_nDelay);
  202.          } catch (InterruptedException var6) {
  203.             this.stop();
  204.          }
  205.       }
  206.    }
  207.  
  208.    public void init() {
  209.       String param = ((Applet)this).getParameter("frps");
  210.       if (param != null) {
  211.          this.m_frps = Integer.parseInt(param);
  212.          this.m_nDelay = 1000 / this.m_frps;
  213.       }
  214.  
  215.       param = ((Applet)this).getParameter("imageFile");
  216.       if (param != null) {
  217.          this.m_imageFile = param;
  218.       }
  219.  
  220.       param = ((Applet)this).getParameter("imageType");
  221.       if (param != null) {
  222.          this.m_imageType = param;
  223.       }
  224.  
  225.       param = ((Applet)this).getParameter("numImages");
  226.       if (param != null) {
  227.          this.m_numImages = Integer.parseInt(param);
  228.       }
  229.  
  230.       param = ((Applet)this).getParameter("imageOrder");
  231.       if (param != null) {
  232.          this.m_sOrder = param;
  233.       }
  234.  
  235.       param = ((Applet)this).getParameter("showButtons");
  236.       if (param != null) {
  237.          this.m_sShowButtons = param;
  238.       }
  239.  
  240.       param = ((Applet)this).getParameter("backgroundColor");
  241.       if (param != null) {
  242.          if (param.compareTo("black") == 0) {
  243.             this.m_backgroundColor = Color.black;
  244.          } else if (param.compareTo("blue") == 0) {
  245.             this.m_backgroundColor = Color.blue;
  246.          } else if (param.compareTo("cyan") == 0) {
  247.             this.m_backgroundColor = Color.cyan;
  248.          } else if (param.compareTo("darkGray") == 0) {
  249.             this.m_backgroundColor = Color.darkGray;
  250.          } else if (param.compareTo("gray") == 0) {
  251.             this.m_backgroundColor = Color.gray;
  252.          } else if (param.compareTo("green") == 0) {
  253.             this.m_backgroundColor = Color.green;
  254.          } else if (param.compareTo("lightGray") == 0) {
  255.             this.m_backgroundColor = Color.lightGray;
  256.          } else if (param.compareTo("magenta") == 0) {
  257.             this.m_backgroundColor = Color.magenta;
  258.          } else if (param.compareTo("orange") == 0) {
  259.             this.m_backgroundColor = Color.orange;
  260.          } else if (param.compareTo("pink") == 0) {
  261.             this.m_backgroundColor = Color.pink;
  262.          } else if (param.compareTo("red") == 0) {
  263.             this.m_backgroundColor = Color.red;
  264.          } else if (param.compareTo("white") == 0) {
  265.             this.m_backgroundColor = Color.white;
  266.          } else if (param.compareTo("yellow") == 0) {
  267.             this.m_backgroundColor = Color.yellow;
  268.          } else {
  269.             this.m_backgroundColor = Color.blue;
  270.          }
  271.       }
  272.  
  273.       ((Component)this).setBackground(this.m_backgroundColor);
  274.       if (this.m_sShowButtons.compareTo("true") == 0 && (this.m_sOrder.compareTo("circular") == 0 || this.m_sOrder.compareTo("linear") == 0)) {
  275.          ((Container)this).add(this.m_PrevButton);
  276.          ((Container)this).add(this.m_NextButton);
  277.          ((Container)this).add(this.m_playButton);
  278.       }
  279.  
  280.       if (this.m_sShowButtons.compareTo("true") == 0) {
  281.          this.m_nOffset = 15;
  282.       } else {
  283.          this.m_nOffset = 0;
  284.       }
  285.  
  286.    }
  287.  
  288.    public boolean mouseDrag(Event evt, int x, int y) {
  289.       return true;
  290.    }
  291.  
  292.    public MORPHTEA() {
  293.       this.m_backgroundColor = Color.blue;
  294.       this.m_sOrder = "circular";
  295.       this.m_sShowButtons = "true";
  296.       this.PARAM_frps = "frps";
  297.       this.PARAM_imageFile = "imageFile";
  298.       this.PARAM_imageType = "imageType";
  299.       this.PARAM_numImages = "numImages";
  300.       this.PARAM_backgroundColor = "backgroundColor";
  301.       this.PARAM_imageOrder = "imageOrder";
  302.       this.PARAM_showButtons = "showButtons";
  303.    }
  304. }
  305.