home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Cdrom / Pavilions / BrainOpera / online / net-music / rhythm-tree / rhythmtree.java < prev    next >
Encoding:
Java Source  |  2017-09-21  |  16.9 KB  |  650 lines

  1. /***********
  2.  Rhythm Tree
  3. ***********/
  4.  
  5. /* basically, this is MultiMouse with a bunch of "regions" defined,
  6.    that you can click on and something will happen
  7.  
  8. */
  9.  
  10.  
  11. import java.util.StringTokenizer;
  12. import java.util.Vector;
  13. import java.lang.Integer;
  14. import java.util.Random;
  15.  
  16.  
  17. import java.net.*;
  18. import java.io.*;
  19. import java.applet.*;
  20. import java.awt.*;
  21. import java.applet.Applet;
  22.  
  23. public class RhythmTree extends Applet implements Runnable, WebStarHandler
  24. {
  25.   Graphics offscreenGraphics;  /* for double buffered graphics */
  26.   Image offscreenImage;
  27.   Font myFont;
  28.  
  29.   int MyX, MyY;                /* for drawing the pointer */
  30.   Color MyColor;
  31.  
  32.   String comment = "";         /* network, messaging stuff */
  33.   Thread kicker = null;
  34.   StarClient sc;
  35.   int myStarID;
  36.  
  37.   boolean empowered;           /* passed in through parameters */
  38.   String colorname;
  39.   String MyName;
  40.  
  41.   Image MyImage;               /* which picture shall we display? */
  42.   Image tree;
  43.   Vector dudes;  /* tracking number of users */
  44.  
  45.   Vector regions = new Vector();
  46.   int numclicks[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  47.  
  48.   AudioClip sounds[];
  49.   Image images[];
  50.   /*
  51.     int spot1a[] = {2, 2, 1,};
  52.     int spot1b[] = {2, 3, 1};
  53.     int spot2a[] = {4, 2, 4, 3, 1};
  54.     int spot2b[] = {4, 3, 5, 2, 1};
  55.     int spot3a[] = {6, 3, 5, 6, 4, 2, 1};
  56.     int spot3b[] = {6, 2, 4, 7, 5, 3, 1};
  57.     int spot4a[] = {8, 6, 9, 7, 4, 3, 5, 2, 1};
  58.     int spot4b[] = {8, 7, 8, 6, 5, 2, 4, 3, 1};
  59.     int spot5a[] = {11, 8, 10, 9, 7, 8, 6, 5, 4, 2, 3, 1};
  60.     int spot5b[] = {11, 9, 10, 8, 6, 9, 7, 4, 5, 3, 2, 1};
  61.     */
  62.   Thread threads[];
  63.   Thread box1=null;
  64.   Thread box2=null;
  65.   Thread box3=null;
  66.   Thread box4=null;
  67.   Thread box5=null;
  68.   Thread box6=null;
  69.   Thread box7=null;
  70.   Thread box8=null;
  71.   Thread box9=null;
  72.  
  73.   int xcoordinates[]= {0, 100, 200, 0, 100, 200, 0, 100, 200};
  74.   int ycoordinates[]= {0, 0, 0, 100, 100, 100, 200, 200, 200};
  75.   int animations[][]= {
  76.  
  77.     {2, 1, 0,},
  78.     {2, 2, 0},
  79.     {4, 1, 3, 2, 0},
  80.     {4, 2, 4, 1, 0},
  81.     {6, 2, 4, 5, 3, 1, 0},
  82.     {6, 2, 3, 6, 4, 2, 0},
  83.     {8, 5, 8, 6, 3, 2, 4, 1, 0},
  84.     {8, 6, 7, 5, 4, 1, 3, 2, 0},
  85.     {11, 7, 9, 8, 6, 7, 5, 4, 3, 1, 2, 0},
  86.     {11, 8, 9, 7, 5, 8, 6, 3, 4, 2, 1, 0}
  87.   };
  88.  
  89.   Flicker1 flicker1;
  90.  
  91.  
  92.  
  93.   /********************/
  94.   /*** APPLET STUFF ***/
  95.   /********************/
  96.  
  97.  
  98.  
  99.  
  100.   public void setup_regions() {
  101.  
  102.     /* This is where we set up the "things to click on" for the rhythm
  103.        tree */
  104.  
  105.     int w = this.size().width;
  106.     int h = this.size().height;
  107.     int b = 0;
  108.  
  109.     Polygon p1 = new Polygon();
  110.     Polygon p2 = new Polygon();
  111.     Polygon p3 = new Polygon();
  112.     Polygon p4 = new Polygon();
  113.     Polygon p5 = new Polygon();
  114.     Polygon p6 = new Polygon();
  115.     Polygon p7 = new Polygon();
  116.     Polygon p8 = new Polygon();
  117.     Polygon p9 = new Polygon();
  118.  
  119.     p1.addPoint(0+b,0+b);
  120.     p1.addPoint(0+b,h/3-b);
  121.     p1.addPoint(w/3-b,h/3-b);
  122.     p1.addPoint(w/3-b,0+b);
  123.  
  124.     p2.addPoint(w/3+b,0+b);
  125.     p2.addPoint(w/3+b,h/3-b);
  126.     p2.addPoint(2*w/3-b,h/3-b);
  127.     p2.addPoint(2*w/3-b,0+b);
  128.  
  129.     p3.addPoint(2*w/3+b,0+b);
  130.     p3.addPoint(2*w/3+b,h/3-b);
  131.     p3.addPoint(w-b,h/3-b);
  132.     p3.addPoint(w-b,0+b);
  133.  
  134.     p4.addPoint(0+b,h/3+b);
  135.     p4.addPoint(0+b,2*h/3-b);
  136.     p4.addPoint(w/3-b,2*h/3-b);
  137.     p4.addPoint(w/3-b,h/3+b);
  138.  
  139.     p5.addPoint(w/3+b,h/3+b);
  140.     p5.addPoint(w/3+b,2*h/3-b);
  141.     p5.addPoint(2*w/3-b,2*h/3-b);
  142.     p5.addPoint(2*w/3-b,h/3+b);
  143.  
  144.     p6.addPoint(2*w/3+b,h/3+b);
  145.     p6.addPoint(2*w/3+b,2*h/3-b);
  146.     p6.addPoint(w-b,2*h/3-b);
  147.     p6.addPoint(w-b,h/3+b);
  148.  
  149.     p7.addPoint(0+b,2*h/3+b);
  150.     p7.addPoint(0+b,h-b);
  151.     p7.addPoint(w/3-b,h-b);
  152.     p7.addPoint(w/3-b,2*h/3+b);
  153.  
  154.     p8.addPoint(w/3+b,2*h/3+b);
  155.     p8.addPoint(w/3+b,h-b);
  156.     p8.addPoint(2*w/3-b,h-b);
  157.     p8.addPoint(2*w/3-b,2*h/3+b);
  158.  
  159.     p9.addPoint(2*w/3+b,2*h/3+b);
  160.     p9.addPoint(2*w/3+b,h-b);
  161.     p9.addPoint(w-b,h-b);
  162.     p9.addPoint(w-b,2*h/3+b);
  163.  
  164.     regions.addElement(p1);
  165.     regions.addElement(p2);
  166.     regions.addElement(p3);
  167.     regions.addElement(p4);
  168.     regions.addElement(p5);
  169.     regions.addElement(p6);
  170.     regions.addElement(p7);
  171.     regions.addElement(p8);
  172.     regions.addElement(p9);
  173.  
  174.   }
  175.  
  176.   public void init()
  177.     {
  178.       setup_regions();
  179.       /*
  180.     animations.addElement(spot1a);
  181.     animations.addElement(spot1b);
  182.     animations.addElement(spot2a);
  183.     animations.addElement(spot2b);
  184.     animations.addElement(spot3a);
  185.     animations.addElement(spot3b);
  186.     animations.addElement(spot4a);
  187.     animations.addElement(spot4b);
  188.     animations.addElement(spot5a);
  189.     animations.addElement(spot5b);
  190.     */
  191.       sounds = new AudioClip[9];
  192.       sounds[0]=getAudioClip(getCodeBase(),"1.au");
  193.       sounds[1]=getAudioClip(getCodeBase(),"7.au");
  194.       sounds[2]=getAudioClip(getCodeBase(),"10.au");
  195.       sounds[3]=getAudioClip(getCodeBase(),"13.au");
  196.       sounds[4]=getAudioClip(getCodeBase(),"15.au");
  197.       sounds[5]=getAudioClip(getCodeBase(),"16.au");
  198.       sounds[6]=getAudioClip(getCodeBase(),"17.au");
  199.       sounds[7]=getAudioClip(getCodeBase(),"19.au");
  200.       sounds[8]=getAudioClip(getCodeBase(),"5.au");
  201.       images = new Image[10];
  202.       images[0]=getImage(getCodeBase(), "RT1.gif");
  203.       images[1]=getImage(getCodeBase(), "RT2.gif");
  204.       images[2]=getImage(getCodeBase(), "RT3.gif");
  205.       images[3]=getImage(getCodeBase(), "RT4.gif");
  206.       images[4]=getImage(getCodeBase(), "RT5.gif");
  207.       images[5]=getImage(getCodeBase(), "RT6.gif");
  208.       images[6]=getImage(getCodeBase(), "RT7.gif");
  209.       images[7]=getImage(getCodeBase(), "RT8.gif");
  210.       images[8]=getImage(getCodeBase(), "RT9.gif");
  211.       images[9]=getImage(getCodeBase(), "RT10.gif");
  212.       threads = new Thread[9];
  213.       threads[0]=box1;
  214.       threads[1]=box2;
  215.       threads[2]=box3;
  216.       threads[3]=box4;
  217.       threads[4]=box5;
  218.       threads[5]=box6;
  219.       threads[6]=box7;
  220.       threads[7]=box8;
  221.       threads[8]=box9;
  222.  
  223.       tree = getImage(getCodeBase(),"testimage.gif");
  224.  
  225.       String power;
  226.       offscreenImage = createImage(this.size().width,this.size().height);
  227.       offscreenGraphics = offscreenImage.getGraphics();
  228.       myFont = new java.awt.Font("Courier", Font.BOLD, 12);
  229.       offscreenGraphics.setFont(myFont);
  230.  
  231.       /* load the picture */
  232.       try {
  233.     MyImage = getImage(new URL(getDocumentBase(), getParameter("IMAGE")));
  234.       }
  235.       catch (MalformedURLException e) {
  236.     offscreenGraphics.setColor(Color.black);
  237.     offscreenGraphics.drawString("Can't load the image", 50, 70);
  238.       }
  239.       repaint();
  240.  
  241.       /* find out if we are impotent */
  242.       power = getParameter("POWER");
  243.       if (power == null) power = "yes";
  244.       if (power.equals("no")) empowered = false;
  245.       else empowered = true;
  246.  
  247.       colorname = getParameter("COLOR");
  248.       if (colorname == null) colorname = "red";  /* default color */
  249.       MyName = getParameter("MONIKER");
  250.       if (MyName == null) MyName = "";
  251.  
  252.       sc = new StarClient(this);
  253.       dudes      = new Vector();
  254.       MyX = MyY = -100;             /* Start the marker off-screen */
  255.       MyColor = Color.red;
  256.     }
  257.  
  258.   public void PaintBackground(){
  259.     offscreenGraphics.drawImage(tree, 0, 0, this);
  260.     offscreenGraphics.drawImage(tree, 100, 0, this);
  261.     offscreenGraphics.drawImage(tree, 200, 0, this);
  262.     offscreenGraphics.drawImage(tree, 0, 100, this);
  263.     offscreenGraphics.drawImage(tree, 100, 100, this);
  264.     offscreenGraphics.drawImage(tree, 200, 100, this);
  265.     offscreenGraphics.drawImage(tree, 0, 200, this);
  266.     offscreenGraphics.drawImage(tree, 100, 200, this);
  267.     offscreenGraphics.drawImage(tree, 200, 200, this);
  268.     repaint();
  269.   }
  270.   public void start() {
  271.     if (kicker == null) {
  272.       kicker = new Thread(this);
  273.       kicker.start();
  274.     }
  275.   }
  276.  
  277.   public void stop() {             /* stop main thread */
  278.     if ( kicker !=null ) {
  279.       kicker.stop() ;
  280.       kicker = null ;
  281.     }
  282.     if ( sc !=null ) sc.stop(); /* stop and disconnect StarClient thread */
  283.   }
  284.  
  285.   public boolean mouseDown(java.awt.Event evt, int x, int y)
  286.     {
  287.       feedback();
  288.       if (empowered) {
  289.     /* transmit click coordinates */
  290.     String msg = " " + x + " " + y + " " + colorname + " " + MyName;
  291.     sc.send(msg);
  292.  
  293.     /* draw pointer immediately */
  294.     MyX = x; MyY = y;
  295.  
  296.     repaint();
  297.     regionalist();
  298.  
  299.  
  300.       }
  301.       return true;
  302.     }
  303.  
  304.   public boolean mouseUp(java.awt.Event evt, int x, int y)
  305.     {
  306.       return true;
  307.     }
  308.  
  309.   /* Tell user how many are connected */
  310.   public void feedback() {
  311.     String m;
  312.     if (MyName.equals("")) m = "";
  313.     else m = "(" + MyName + ")";
  314.     if (dudes.size() <= 1)
  315.       showStatus("You're the only one here. " + m);
  316.     else
  317.       showStatus("There are " + dudes.size() + " people connected. " + m);
  318.   }
  319.  
  320.   /* Hello, network. I'm alive. */
  321.   public void beacon() {
  322.     String msg = " -100 -100 " + colorname + " " + MyName;
  323.     sc.send(msg);
  324.   }
  325.  
  326.   public void run()
  327.     {
  328.  
  329.       int announcement = 0;
  330.       Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  331.       sc.start(getDocumentBase().getHost(), 8121);
  332.  
  333.       while (kicker != null)
  334.     {
  335.       if (announcement < 5) {
  336.         beacon(); PaintBackground();
  337.         announcement++;
  338.       }
  339.       try {Thread.sleep(5000);} catch (InterruptedException e) {}
  340.       repaint();
  341.     }
  342.     }
  343.  
  344.   public String[][] getParameterInfo() {
  345.     String[][] info = {
  346.       // Parameter Name Kind of Value   Description
  347.       {"image  ", "URL   ",  "a GIF or JPEG file"},
  348.       {"power  ", "yes/no",  "participant or observer?"},
  349.       {"color  ", "red,orange,yellow,green,blue" +
  350.        "black,white,gray,darkGray,lightGray," +
  351.        "magenta,pink,cyan",  "pointer color"},
  352.       {"moniker", "string",  "name of participant"},
  353.     };
  354.     return info;
  355.   }
  356.  
  357.   public String getAppletInfo() {
  358.     return "MultiMouse by Jeff Breidenbach v1.0beta1";
  359.   }
  360.  
  361.   /*************************/
  362.   /*** STAR CLIENT STUFF ***/
  363.   /*************************/
  364.  
  365.   /* called when the connection's all set  */
  366.   public void WS_up(String msg, Object who)
  367.     {
  368.       myStarID = sc.getStarID();
  369.       showStatus(msg);
  370.     }
  371.  
  372.   /* called when the connection's down */
  373.   public void WS_down(String msg, Object who)
  374.     {
  375.       showStatus("The connection is broken!");
  376.     }
  377.  
  378.   /* general messages and errors */
  379.   public void WS_debugLog(String msg, Object who)
  380.     {
  381.       showStatus(msg);
  382.     }
  383.  
  384.   /* gets called after reception of a new msg  */
  385.   public void WS_newMsg(String msg, Object who)
  386.     {
  387.       int FromID, x, y, deadID;
  388.       boolean Understood;
  389.       String tok;
  390.       String name="";
  391.       String col="red";
  392.  
  393.       FromID = -1; Understood = false; x = y = -100;
  394.       StringTokenizer st = new StringTokenizer(msg);
  395.  
  396.       /* who is it from?.. */
  397.       if(st.hasMoreTokens())
  398.     FromID = (Integer.valueOf(st.nextToken())).intValue();
  399.  
  400.       /* Is it a special annoucement (i.e. from the server itself) */
  401.       if(FromID == 0)
  402.     {
  403.       if(st.hasMoreTokens())
  404.         {
  405.           tok = st.nextToken();
  406.           if( (tok.equals("OBITUARY"))&&(st.hasMoreTokens()) )
  407.         {
  408.           deadID = (Integer.valueOf(st.nextToken())).intValue();
  409.           dudes.removeElement(new Integer(deadID));
  410.           feedback();
  411.         }
  412.         }
  413.       return;
  414.     }
  415.  
  416.       /* ..or from a web client? -> interpreted as an (x,y) position */
  417.       if(FromID > 0)
  418.     {
  419.       if(st.hasMoreTokens())
  420.         {
  421.           x = (Integer.valueOf(st.nextToken())).intValue();
  422.           if(st.hasMoreTokens())
  423.         {
  424.           y = (Integer.valueOf(st.nextToken())).intValue();
  425.           Understood = true;
  426.               if(st.hasMoreTokens())
  427.             {
  428.               col = st.nextToken();
  429.             }
  430.               while (st.hasMoreTokens())
  431.             {
  432.               name = name + st.nextToken() + " ";
  433.             }
  434.         }
  435.         }
  436.     }
  437.  
  438.       /* Let's do something about it */
  439.       if(Understood)
  440.     {
  441.       tracker(FromID, col, name);
  442.       if(FromID == myStarID) {        /* Was it from Myself?      */
  443.       }
  444.       else {                          /* ..or from somebody else? */
  445.         if (onscreen(x,y))
  446.           {
  447.         MyX = x; MyY = y;          /* draw pointer */
  448.         showStatus(name);
  449.         colorcode(col);
  450.         repaint();
  451.         regionalist();
  452.           }
  453.       }
  454.     }
  455.     }
  456.  
  457.   public void colorcode(String col) {
  458.     MyColor = Color.red;
  459.     if (col.equals("red")) MyColor = Color.red;
  460.     if (col.equals("orange")) MyColor = Color.orange;
  461.     if (col.equals("yellow")) MyColor = Color.yellow;
  462.     if (col.equals("green")) MyColor = Color.green;
  463.     if (col.equals("blue")) MyColor = Color.blue;
  464.     if (col.equals("black")) MyColor = Color.black;
  465.     if (col.equals("white")) MyColor = Color.white;
  466.     if (col.equals("gray")) MyColor = Color.gray;
  467.     if (col.equals("darkGray")) MyColor = Color.darkGray;
  468.     if (col.equals("lightGray")) MyColor = Color.lightGray;
  469.     if (col.equals("magenta")) MyColor = Color.magenta;
  470.     if (col.equals("pink")) MyColor = Color.pink;
  471.     if (col.equals("cyan")) MyColor = Color.cyan;
  472.   }
  473.  
  474.   /* keep track of who's connected  */
  475.   public void tracker(int ID, String thiscolor, String name) {
  476.     Object thisdude  = new Integer(ID);
  477.     if (dudes.contains(thisdude)) {}        /* I know this guy... */
  478.     else {                                  /* found a new dude!  */
  479.       dudes.addElement(thisdude);           /* let's remember him */
  480.       beacon();                             /* Hello, new dude */
  481.       feedback();                           /* Hey, look! */
  482.     }
  483.   }
  484.  
  485.   /* is this mouseclick onscreen?  */
  486.   public boolean onscreen(int x, int y) {
  487.     boolean answer;
  488.     if ((x != -100) || (y != -100)) answer = true;
  489.     else answer = false;
  490.     return answer;
  491.   }
  492.  
  493.   /**********************/
  494.   /*** GRAPHICS STUFF ***/
  495.   /**********************/
  496.  
  497.  
  498.   /********* Look here !!!! *****************************/
  499.   /*                                                    */
  500.   /*  This is what happens when you click on a "region" */
  501.   /*                                                    */
  502.   /******************************************************/
  503.   public void animate_adjacent(int box) {
  504.     if (box==0)
  505.     {animate_help(1);
  506.     animate_help(4);
  507.     animate_help(3);}
  508.     else if (box==1)
  509.     {animate_help(0);
  510.     animate_help(2);
  511.     animate_help(3);
  512.     animate_help(4);
  513.     animate_help(5);}
  514.     else if (box==2){
  515.     animate_help(1);
  516.     animate_help(4);
  517.     animate_help(5);}
  518.     else if(box==3){
  519.     animate_help(0);
  520.     animate_help(1);
  521.     animate_help(4);
  522.     animate_help(7);
  523.     animate_help(6);}
  524.     else if(box==4){
  525.     animate_help(0);
  526.     animate_help(1);
  527.     animate_help(2);
  528.     animate_help(3);
  529.     animate_help(5);
  530.     animate_help(6);
  531.     animate_help(7);
  532.     animate_help(8);}
  533.     else if(box==5){
  534.     animate_help(1);
  535.     animate_help(2);
  536.     animate_help(4);
  537.     animate_help(7);
  538.     animate_help(8);}
  539.     else if(box==6){
  540.     animate_help(3);
  541.     animate_help(4);
  542.     animate_help(7);}
  543.     else if(box==7){
  544.     animate_help(6);
  545.     animate_help(3);
  546.     animate_help(4);
  547.     animate_help(5);
  548.     animate_help(8);}
  549.     else if(box==8){
  550.     animate_help(5);
  551.     animate_help(4);
  552.     animate_help(7);}
  553.  
  554.     }
  555.  
  556.  
  557.  
  558.   public void animate_help(int box) {
  559.    int r = (int)(((float)Math.random())*9);
  560.     if (threads[box]==null)
  561.       {
  562.         threads[box] = new Flicker1(this, box,r);
  563.         if (numclicks[box]==5) {
  564.         animate_adjacent(box);
  565.         numclicks[box]=0;}
  566.         else numclicks[box]=(numclicks[box] + 1);
  567.       }
  568.   }
  569.  
  570.  
  571.   public void regionalist() {
  572.  
  573.     Polygon p = new Polygon();
  574.     int i,r;
  575.  
  576.     for (i=0; i<regions.size(); i++) {
  577.  
  578.       p = (Polygon) regions.elementAt(i);
  579.       if (p.inside(MyX,MyY)) {
  580.     r = (int)(((float)Math.random())*9);
  581.  
  582.     if (threads[i]==null)
  583.       {
  584.         threads[i] = new Flicker1(this, i,r);
  585.         if (numclicks[i]==5) {
  586.         animate_adjacent(i);
  587.         numclicks[i]=0;}
  588.         else numclicks[i]=(numclicks[i] + 1);
  589.       }
  590.       }
  591.     }
  592.   }
  593.  
  594.  
  595.   public void update(Graphics g) {
  596.     offscreenGraphics.drawImage(MyImage, 0, 0, this);
  597.     paint(g);
  598.   }
  599.  
  600.   public void paint(Graphics g)
  601.     {
  602.       g.drawImage(offscreenImage, 0, 0, this);
  603.     }
  604.  
  605.   public void animator(int box, int animation){
  606.     int frames=animations[animation][0];
  607.     int currimage=1;
  608.     Image TempImage;
  609.  
  610.     sounds[box].loop();
  611.  
  612.     while (currimage <= frames) {
  613.       TempImage = images[animations[animation][currimage]];
  614.       getGraphics().drawImage(TempImage, xcoordinates[box], ycoordinates[box],this);
  615.       System.out.println("Frame =" + animations[animation][currimage]);
  616.       System.out.println("coordinates = " + xcoordinates[box] + " " +  ycoordinates[box]);
  617.  
  618.       try{threads[box].sleep(200);}catch (InterruptedException e);
  619.       ++currimage;
  620.     }
  621.     sounds[box].stop();
  622.   }
  623.  
  624. }
  625.  
  626. class Flicker1 extends Thread {
  627.   RhythmTree rt;
  628.   int box, animation;
  629.  
  630.   public Flicker1(RhythmTree foo, int b, int a) {
  631.     box = b;
  632.     animation = a;
  633.     rt = foo;
  634.     this.start();
  635.   }
  636.  
  637.   public void run() {
  638.     rt.animator(box,animation);
  639.     rt.threads[box] = null; /* suicide */
  640.   }
  641. }
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.