home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Cdrom / Main / Intro.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  12.2 KB  |  518 lines

  1. package tiongson.expo;
  2.  
  3. // Intro.java
  4.  
  5. import java.applet.Applet;
  6. import java.awt.*;
  7. import java.net.URL;
  8. import java.net.MalformedURLException;
  9. import java.util.*;
  10.  
  11. /** Intro.class
  12.  *  I grant anyone a royalty-free license
  13.  *  to use this applet or source code for non-commercial 
  14.  *  use ONLY.
  15.  *  
  16.  *  This copyright must be included in any copies of this code.
  17.  *
  18.  *  All other rigths reserved. Copyright phillip r. tiongson, 1997.
  19.  *  
  20.  *  In other words, if you aren't making money from selling this code
  21.  *  it is fine to use it. If you are making money, please give me some.
  22.  *  thanks, if you want to use it, contact me, 
  23.  *  phillip tiongson, at sdallas@mit.edu.
  24.  *
  25.  *  @author   Phillip R. Tiongson
  26.  *  @version  1.0
  27.  */
  28. public class Intro
  29.     extends java.applet.Applet
  30.     implements java.lang.Runnable
  31. {
  32.     public static final String name    = "Intro";
  33.     public static final String version = "1.0";
  34.  
  35.     public boolean DEBUG = false;
  36.  
  37.     Thread    runner;
  38.     Image background;
  39.     protected MediaTracker tracker;
  40.     
  41.     int[] mouseX = {200, 200, 200, 200, 200}, mouseY = {200, 200, 200, 200, 200};
  42.     
  43.     Vector rects;
  44.     
  45.     /** Performs applet initialization.
  46.      */
  47.     public void init()
  48.     {
  49.         this.setBackground(Color.black);
  50.         this.setForeground(Color.white);
  51.         
  52.         if (getParameter("DEBUG") != null) {
  53.             
  54.             dPrint("Debugging is on");    
  55.             DEBUG = true;
  56.         }    
  57.         
  58.         offscreensize = size();
  59.         rects = new Vector();
  60.         
  61.         parseHTMLFile(rects);
  62.         
  63.         getImages();    
  64.         
  65.         showStatus("ready");
  66.         
  67.     }
  68.  
  69.     /** to prevent flicker
  70.     */
  71.     public void update (Graphics g) {
  72.         paint(g);
  73.     }
  74.  
  75.  
  76.     // These are for the offscreen drawing buffer.
  77.     Image offscreen;
  78.     Dimension offscreensize;
  79.     Graphics offgraphics;
  80.     
  81.     
  82.     /** This method overrides the paint method. It double buffers the drawing by drawing first
  83.     to the offscreen buffer, then blasting it to the Graphics g.
  84.     */
  85.     int backX = 0, backY = 0;
  86.  
  87.     boolean initme = true;
  88.     
  89.     public void paint(Graphics g) {
  90.         Dimension d = size();
  91.  
  92.         // if no offscreen buffer image, make a new one
  93.         if ((offscreen == null) || (d.width != offscreensize.width) 
  94.             || (d.height != offscreensize.height)) {
  95.             
  96.             offscreen = createImage(d.width, d.height);
  97.             offscreensize = d;
  98.             offgraphics = offscreen.getGraphics();
  99.             offgraphics.setFont(getFont());
  100.         }
  101.         
  102.         if (initme) {
  103.             offgraphics.setColor(getBackground());
  104.             offgraphics.fillRect(0,0,d.width, d.height);
  105.             offgraphics.setColor(Color.red);
  106.             offgraphics.drawString("Loading images...please wait.", 20, 20);
  107.             g.drawImage(offscreen, 0, 0, this);
  108.             return;
  109.         }
  110.         
  111.         offgraphics.clipRect(0,0,d.width,d.height);
  112.  
  113.          
  114.         offgraphics.drawImage(background, backX, backY, this);
  115.         
  116.         for(Enumeration e = rects.elements(); e.hasMoreElements();) {
  117.             ((HotSpot) e.nextElement()).paint(offgraphics, backX, backY, DEBUG);
  118.         }
  119.  
  120.         
  121.             
  122.         // Blast image to Graphics g.
  123.         g.drawImage(offscreen, 0, 0, this);
  124.     
  125.     }
  126.  
  127.     /** The main entry point for the applet.
  128.      */
  129.     public void start()
  130.     {
  131.         repaint();
  132.         if (runner == null)
  133.         {
  134.             runner = new Thread(this);
  135.             runner.start();
  136.         }
  137.     }
  138.  
  139.     /** Called when you create a new thread with this class.
  140.      */
  141.     int decay;
  142.     float incrementX = 0, incrementY = 0;
  143.     int idleCount = 0;
  144.     //int lastX = 0, lastY = 0;
  145.     //boolean hit;
  146.     HotSpot On;
  147.     
  148.     public void run()
  149.     {
  150.         //runner.setPriority(Thread.MIN_PRIORITY);
  151.     
  152.         while (Thread.currentThread() == runner) {
  153.             try {
  154.                 runner.sleep(100);
  155.              } catch (InterruptedException e) {
  156.                  System.err.println("insomnia "+e);
  157.                  };
  158.             
  159.             
  160.             
  161.             if (decay == 1) {
  162.                 if (Math.abs(incrementX) < 1) incrementX = 0;
  163.                 if (Math.abs(incrementY) < 1) incrementY = 0;
  164.                 incrementX *= .75f;
  165.                 incrementY *= .75f;
  166.             } if (decay == 0) {
  167.                 incrementX = -(float)(Math.sin((mouseX[2] - mouseX[0]) * Math.PI/2000.0f) * 100.0f);
  168.                 incrementY = -(float)(Math.sin((mouseY[2] - mouseY[0]) * Math.PI/2000.0f) * 50.0f);
  169.             } if (decay == 2) {
  170.                 incrementX = 0;
  171.                 incrementY = 0;
  172.                 
  173.             }
  174.             
  175.             // System.out.println("dx = "+backX+ " inc "+incrementX);
  176.             
  177.             backX += (int) incrementX;
  178.             backX = (int) Math.min(Math.max(-backW+offscreensize.width, backX), 0);
  179.  
  180.             backY += (int) incrementY;
  181.             backY = (int)Math.min(Math.max(-backH+offscreensize.height, backY), 0);
  182.             //boolean nohit = true;
  183.             if (idleCount > 1) {
  184.                 if (idleCount < 25) {
  185.                     for (Enumeration e = rects.elements(); e.hasMoreElements();) {
  186.                         HotSpot h = (HotSpot) e.nextElement();
  187.                         
  188.                         
  189.                         if (h.inside(mouseX[4], mouseY[4])) { 
  190.                             dPrint("inside." +idleCount);
  191.                             On = h;
  192.                             h.show();
  193.                             //hit = true;
  194.                             //nohit = false;
  195.                             continue;
  196.                         } // if
  197.                         h.hide();    
  198.                     } // for
  199.                 } // if
  200.                 
  201.             } // if
  202.             
  203.             //if (nohit) repaint();
  204.             
  205.             dPrint("idle = "+idleCount);
  206.             
  207.             if (idleCount < 0) {
  208.                 /*
  209.                 for (Enumeration e = rects.elements(); e.hasMoreElements();) {
  210.                     HotSpot h = (HotSpot) e.nextElement();
  211.                     h.hide();    
  212.                 } //for
  213.                 */
  214.                 if (On != null) On.hide();
  215.             } // if
  216.             
  217.             idleCount++;
  218.             
  219.             //if (!((lastX == backX) && (lastY == backY) && !hit)) {
  220.                 
  221.  
  222.                 repaint();
  223.                 //hit = false;
  224.             //}
  225.             //lastX = backX;
  226.             //lastY = backY;
  227.         }
  228.     }
  229.  
  230.     /** Stops the applet from executing.
  231.      */
  232.     public void stop()
  233.     {
  234.         if (runner != null)
  235.         {
  236.             runner.stop();
  237.             runner = null;
  238.         }
  239.     }
  240.  
  241.     /** Free up any applet resources.
  242.      */
  243.     public void destroy()
  244.     {
  245.         background = null;
  246.         for (Enumeration e = rects.elements(); e.hasMoreElements(); ) {
  247.             ((HotSpot) e.nextElement()).image = null;
  248.         }
  249.         
  250.     }
  251.  
  252.     /** Returns a text description of the applet.
  253.      */
  254.     public String getAppletInfo()
  255.     {
  256.         return name + " version " + version;
  257.     }
  258.  
  259.     public boolean handleEvent(Event event) {
  260.         
  261.         switch (event.id) {
  262.             case Event.KEY_PRESS:
  263.  
  264.                 // Utils.dPrint("Got a key: "+(char)event.key);
  265.                 switch((char) event.key){
  266.                     case 'q':
  267.                         stop();
  268.                         break;
  269.                     case 'r':
  270.                         start();
  271.                         break;
  272.                     default:
  273.                         break;
  274.                 } // switch
  275.  
  276.                 repaint();
  277.                 return true;            
  278.             
  279.             case Event.MOUSE_DOWN:
  280.                 idleCount = -1;
  281.                 
  282.                 mouseX[0] = event.x;
  283.                 mouseY[0] = event.y;
  284.                 decay = 2;
  285.  
  286.                 dPrint("mouse down (x,y): ("+mouseX[0]+","+mouseY[0]+")");
  287.                 return true;
  288.             case Event.MOUSE_UP:
  289.                 idleCount = -1;
  290.                 mouseX[1] = event.x;
  291.                 mouseY[1] = event.y;
  292.                 
  293.                 if ((mouseX[1] == mouseX[0]) && (mouseY[1] == mouseY[0])) {
  294.                 
  295.  
  296.                     mouseX[3] = mouseX[1] - backX;
  297.                     mouseY[3] = mouseY[1] - backY;
  298.                     
  299.                     for (Enumeration e = rects.elements(); e.hasMoreElements();) {
  300.                         HotSpot h = (HotSpot) e.nextElement();
  301.                         
  302.                         if (h.inside(mouseX[3], mouseY[3])) { 
  303.                             dPrint("got it.");
  304.                             h.openHREF(this); 
  305.                             // repaint();
  306.                             break; 
  307.                         }
  308.                         
  309.                     }
  310.                     
  311.                     dPrint("Clicked at abs ("+mouseX[3]+","+mouseY[3]+").");
  312.                     decay = 2;
  313.                 } else {
  314.                     decay = 1;
  315.                 }
  316.  
  317.                 mouseX[2] = mouseX[0];
  318.                 mouseY[2] = mouseX[0];
  319.     
  320.                 return true;
  321.  
  322.             case Event.MOUSE_DRAG:
  323.                 idleCount = -1;
  324.                 
  325.                 mouseX[2] = event.x;
  326.                 mouseY[2] = event.y;
  327.                 decay = 0;
  328.  
  329.                 dPrint("mouse drag (x,y): ("+mouseX[2]+","+mouseY[2]+")");
  330.                 return true;    
  331.                 
  332.             case Event.MOUSE_MOVE:
  333.                 
  334.                 // To fix netscape bug, netscape produces erroneous Mouse_move messages.
  335.                 if ((mouseX[4] == (event.x - backX)) && (mouseY[4] == (event.y - backY))) {
  336.                     //System.out.println("breaking");
  337.                     break;
  338.                 }
  339.                 
  340.                 idleCount = -1;
  341.                 mouseX[4] = event.x - backX;
  342.                 mouseY[4] = event.y - backY;
  343.  
  344.                 dPrint("mouse move (x,y): ("+mouseX[4]+","+mouseY[4]+")");
  345.             
  346.                 return true;
  347.                     
  348.  
  349.         } // switch
  350.         
  351.         return super.handleEvent(event);
  352.     } // handleEvent 
  353.  
  354.  
  355.     private void parseHTMLFile(Vector destination) {
  356.         Vector nodes = new Vector();
  357.         int number = Integer.parseInt(getParameter("NumberOfNodes"));
  358.         
  359.         for (int i = 0; i < number; i++) {
  360.             String value = getParameter("Node"+i);
  361.             
  362.             if (value != null) { nodes.addElement(value); }
  363.                 else { System.err.println("The parameters for Node "+i+" were missing");}
  364.         } // for
  365.         
  366.         
  367.         for (Enumeration e = nodes.elements(); e.hasMoreElements(); ) {
  368.             String text = (String) e.nextElement();
  369.             StringTokenizer parser = new StringTokenizer(text, ",;");
  370.             
  371.             boolean absolute = false;
  372.             boolean newBrowser = false;
  373.             boolean sound = false;
  374.             String url = "";
  375.             int x = 0;
  376.             int y = 0;
  377.             Rectangle rect = null;
  378.             Polygon poly = null;
  379.             URL href = null;
  380.             Image image = null;;
  381.             
  382.             try {
  383.                 String value = parser.nextToken();
  384.                 if (value.equals("1")) {absolute = true; dPrint("Absolute. "); }
  385.                 if (value.equals("2")) {sound = true; dPrint("sound. "); }
  386.                 if (parser.nextToken().equals("1")) {newBrowser = true; dPrint("newBrowser. "); }
  387.                 
  388.                 url = parser.nextToken();
  389.                 if (!url.equals("none")) {
  390.                     href = new URL(getDocumentBase(), url);
  391.                     dPrint("HREF = "+url+" "+href);
  392.                 }
  393.                             
  394.                 url = parser.nextToken();
  395.                 if (!url.equals("none")) {
  396.                     URL imageLoc = new URL(getDocumentBase(), url);
  397.                     image = getImage(imageLoc);
  398.                     dPrint("IMAGEURL = "+url+" "+image);
  399.                 } // if
  400.                 
  401.                 x = Integer.parseInt(parser.nextToken());
  402.                 y = Integer.parseInt(parser.nextToken());
  403.                 
  404.                 int numberLeft = parser.countTokens();
  405.                 
  406.                 if (numberLeft > 4) {
  407.                     poly = new Polygon();
  408.                     
  409.                     for (int i = 0; i < numberLeft/2; i++) {
  410.                         poly.addPoint(Integer.parseInt(parser.nextToken()), Integer.parseInt(parser.nextToken()));
  411.                     }
  412.                     
  413.                 } else {
  414.                     int x1 = Integer.parseInt(parser.nextToken());
  415.                     int y1 = Integer.parseInt(parser.nextToken());
  416.                     int w = Math.abs(x1 - Integer.parseInt(parser.nextToken()));
  417.                     int h =  Math.abs(y1 - Integer.parseInt(parser.nextToken()));
  418.                 
  419.                     rect = new Rectangle(x1, y1, w, h);
  420.                 }
  421.                 
  422.                 
  423.                 if (poly != null) {
  424.                     destination.addElement(new HotSpot(absolute, newBrowser, sound, href, image, x, y, poly));
  425.                     dPrint("added a poly");
  426.                 } else {
  427.                     destination.addElement(new HotSpot(absolute, newBrowser, sound, href, image, x, y, rect));
  428.                     dPrint("added a rect");
  429.                 }// else            
  430.             } // try
  431.             catch (MalformedURLException mue) { System.err.println("This is not a good url: "+url); }
  432.             catch (NoSuchElementException nsee) { System.err.println("not enough tokens in: "+text); }
  433.             catch (NumberFormatException mie) { System.err.println("Number not corrent "+mie); }
  434.         
  435.         } // for
  436.     
  437.     } // parseHTML 
  438.     
  439.     int backW = 1, backH = 1;
  440.     
  441.     
  442.     private void getImages() {
  443.     
  444.         String toOpen = "background.jpg";
  445.         String tempP = getParameter("background");
  446.         if (tempP != null) toOpen = tempP;
  447.         background = getImage(getDocumentBase(), toOpen);
  448.         
  449.         
  450.         tracker = new MediaTracker(this);
  451.         int i = 0;
  452.     
  453.         tracker.addImage(background, i);
  454.         i++;
  455.         
  456.         for (Enumeration e = rects.elements(); e.hasMoreElements(); i++) {
  457.             HotSpot h = (HotSpot) e.nextElement();
  458.             
  459.             if (h.image == null) continue;
  460.             
  461.             // Image temp = getImage(h.imageLoc);
  462.             tracker.addImage(h.image, i);
  463.         }
  464.  
  465.         try { 
  466.             tracker.waitForAll();                        
  467.         } // try
  468.         
  469.         catch (InterruptedException e) { 
  470.             System.err.println("Got interrupted while waiting.");
  471.             showStatus("Can't read images.");
  472.         } // catch
  473.  
  474.         if (tracker.isErrorID(0)) {
  475.             System.err.println("We can't get the background image sorry.");
  476.             showStatus("Can't read background image, sorry.");
  477.         } // if        
  478.         
  479.         initme = false;
  480.         
  481.         backW = background.getWidth(this);
  482.         backH = background.getHeight(this);
  483.     
  484.         
  485.     } // getImages
  486.  
  487.     private static Random r = new Random();
  488.         
  489.     /** returns a random int from 0 to i.
  490.     if you pass it a 1, it returns 0 half the time and 1 the other half.
  491.     
  492.     Also, if i = -1, it computes +1 or -1 for you.
  493.     
  494.     */
  495.     public static int RandomInt(int i) {
  496.     
  497.         int range = Math.round((i * r.nextFloat()));
  498.  
  499.         //dPrint("int: "+i+"\t got: "+range);
  500.         if ((i == -1) && (range == 0)) {
  501.             //dPrint("+1 :-)");
  502.             return 1;
  503.         }
  504.         
  505.         return range;    
  506.     }
  507.  
  508.     /** A private method to print debug statements 
  509.     */
  510.     private void dPrint(String s) {
  511.         if ( DEBUG == true )
  512.             System.out.println(s);
  513.     } // dPrint
  514.  
  515. } // Intro
  516.  
  517.  
  518.