home *** CD-ROM | disk | FTP | other *** search
Java Source | 2017-09-21 | 16.9 KB | 650 lines |
- /***********
- Rhythm Tree
- ***********/
-
- /* basically, this is MultiMouse with a bunch of "regions" defined,
- that you can click on and something will happen
-
- */
-
-
- import java.util.StringTokenizer;
- import java.util.Vector;
- import java.lang.Integer;
- import java.util.Random;
-
-
- import java.net.*;
- import java.io.*;
- import java.applet.*;
- import java.awt.*;
- import java.applet.Applet;
-
- public class RhythmTree extends Applet implements Runnable, WebStarHandler
- {
- Graphics offscreenGraphics; /* for double buffered graphics */
- Image offscreenImage;
- Font myFont;
-
- int MyX, MyY; /* for drawing the pointer */
- Color MyColor;
-
- String comment = ""; /* network, messaging stuff */
- Thread kicker = null;
- StarClient sc;
- int myStarID;
-
- boolean empowered; /* passed in through parameters */
- String colorname;
- String MyName;
-
- Image MyImage; /* which picture shall we display? */
- Image tree;
- Vector dudes; /* tracking number of users */
-
- Vector regions = new Vector();
- int numclicks[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
-
- AudioClip sounds[];
- Image images[];
- /*
- int spot1a[] = {2, 2, 1,};
- int spot1b[] = {2, 3, 1};
- int spot2a[] = {4, 2, 4, 3, 1};
- int spot2b[] = {4, 3, 5, 2, 1};
- int spot3a[] = {6, 3, 5, 6, 4, 2, 1};
- int spot3b[] = {6, 2, 4, 7, 5, 3, 1};
- int spot4a[] = {8, 6, 9, 7, 4, 3, 5, 2, 1};
- int spot4b[] = {8, 7, 8, 6, 5, 2, 4, 3, 1};
- int spot5a[] = {11, 8, 10, 9, 7, 8, 6, 5, 4, 2, 3, 1};
- int spot5b[] = {11, 9, 10, 8, 6, 9, 7, 4, 5, 3, 2, 1};
- */
- Thread threads[];
- Thread box1=null;
- Thread box2=null;
- Thread box3=null;
- Thread box4=null;
- Thread box5=null;
- Thread box6=null;
- Thread box7=null;
- Thread box8=null;
- Thread box9=null;
-
- int xcoordinates[]= {0, 100, 200, 0, 100, 200, 0, 100, 200};
- int ycoordinates[]= {0, 0, 0, 100, 100, 100, 200, 200, 200};
- int animations[][]= {
-
- {2, 1, 0,},
- {2, 2, 0},
- {4, 1, 3, 2, 0},
- {4, 2, 4, 1, 0},
- {6, 2, 4, 5, 3, 1, 0},
- {6, 2, 3, 6, 4, 2, 0},
- {8, 5, 8, 6, 3, 2, 4, 1, 0},
- {8, 6, 7, 5, 4, 1, 3, 2, 0},
- {11, 7, 9, 8, 6, 7, 5, 4, 3, 1, 2, 0},
- {11, 8, 9, 7, 5, 8, 6, 3, 4, 2, 1, 0}
- };
-
- Flicker1 flicker1;
-
-
-
- /********************/
- /*** APPLET STUFF ***/
- /********************/
-
-
-
-
- public void setup_regions() {
-
- /* This is where we set up the "things to click on" for the rhythm
- tree */
-
- int w = this.size().width;
- int h = this.size().height;
- int b = 0;
-
- Polygon p1 = new Polygon();
- Polygon p2 = new Polygon();
- Polygon p3 = new Polygon();
- Polygon p4 = new Polygon();
- Polygon p5 = new Polygon();
- Polygon p6 = new Polygon();
- Polygon p7 = new Polygon();
- Polygon p8 = new Polygon();
- Polygon p9 = new Polygon();
-
- p1.addPoint(0+b,0+b);
- p1.addPoint(0+b,h/3-b);
- p1.addPoint(w/3-b,h/3-b);
- p1.addPoint(w/3-b,0+b);
-
- p2.addPoint(w/3+b,0+b);
- p2.addPoint(w/3+b,h/3-b);
- p2.addPoint(2*w/3-b,h/3-b);
- p2.addPoint(2*w/3-b,0+b);
-
- p3.addPoint(2*w/3+b,0+b);
- p3.addPoint(2*w/3+b,h/3-b);
- p3.addPoint(w-b,h/3-b);
- p3.addPoint(w-b,0+b);
-
- p4.addPoint(0+b,h/3+b);
- p4.addPoint(0+b,2*h/3-b);
- p4.addPoint(w/3-b,2*h/3-b);
- p4.addPoint(w/3-b,h/3+b);
-
- p5.addPoint(w/3+b,h/3+b);
- p5.addPoint(w/3+b,2*h/3-b);
- p5.addPoint(2*w/3-b,2*h/3-b);
- p5.addPoint(2*w/3-b,h/3+b);
-
- p6.addPoint(2*w/3+b,h/3+b);
- p6.addPoint(2*w/3+b,2*h/3-b);
- p6.addPoint(w-b,2*h/3-b);
- p6.addPoint(w-b,h/3+b);
-
- p7.addPoint(0+b,2*h/3+b);
- p7.addPoint(0+b,h-b);
- p7.addPoint(w/3-b,h-b);
- p7.addPoint(w/3-b,2*h/3+b);
-
- p8.addPoint(w/3+b,2*h/3+b);
- p8.addPoint(w/3+b,h-b);
- p8.addPoint(2*w/3-b,h-b);
- p8.addPoint(2*w/3-b,2*h/3+b);
-
- p9.addPoint(2*w/3+b,2*h/3+b);
- p9.addPoint(2*w/3+b,h-b);
- p9.addPoint(w-b,h-b);
- p9.addPoint(w-b,2*h/3+b);
-
- regions.addElement(p1);
- regions.addElement(p2);
- regions.addElement(p3);
- regions.addElement(p4);
- regions.addElement(p5);
- regions.addElement(p6);
- regions.addElement(p7);
- regions.addElement(p8);
- regions.addElement(p9);
-
- }
-
- public void init()
- {
- setup_regions();
- /*
- animations.addElement(spot1a);
- animations.addElement(spot1b);
- animations.addElement(spot2a);
- animations.addElement(spot2b);
- animations.addElement(spot3a);
- animations.addElement(spot3b);
- animations.addElement(spot4a);
- animations.addElement(spot4b);
- animations.addElement(spot5a);
- animations.addElement(spot5b);
- */
- sounds = new AudioClip[9];
- sounds[0]=getAudioClip(getCodeBase(),"1.au");
- sounds[1]=getAudioClip(getCodeBase(),"7.au");
- sounds[2]=getAudioClip(getCodeBase(),"10.au");
- sounds[3]=getAudioClip(getCodeBase(),"13.au");
- sounds[4]=getAudioClip(getCodeBase(),"15.au");
- sounds[5]=getAudioClip(getCodeBase(),"16.au");
- sounds[6]=getAudioClip(getCodeBase(),"17.au");
- sounds[7]=getAudioClip(getCodeBase(),"19.au");
- sounds[8]=getAudioClip(getCodeBase(),"5.au");
- images = new Image[10];
- images[0]=getImage(getCodeBase(), "RT1.gif");
- images[1]=getImage(getCodeBase(), "RT2.gif");
- images[2]=getImage(getCodeBase(), "RT3.gif");
- images[3]=getImage(getCodeBase(), "RT4.gif");
- images[4]=getImage(getCodeBase(), "RT5.gif");
- images[5]=getImage(getCodeBase(), "RT6.gif");
- images[6]=getImage(getCodeBase(), "RT7.gif");
- images[7]=getImage(getCodeBase(), "RT8.gif");
- images[8]=getImage(getCodeBase(), "RT9.gif");
- images[9]=getImage(getCodeBase(), "RT10.gif");
- threads = new Thread[9];
- threads[0]=box1;
- threads[1]=box2;
- threads[2]=box3;
- threads[3]=box4;
- threads[4]=box5;
- threads[5]=box6;
- threads[6]=box7;
- threads[7]=box8;
- threads[8]=box9;
-
- tree = getImage(getCodeBase(),"testimage.gif");
-
- String power;
- offscreenImage = createImage(this.size().width,this.size().height);
- offscreenGraphics = offscreenImage.getGraphics();
- myFont = new java.awt.Font("Courier", Font.BOLD, 12);
- offscreenGraphics.setFont(myFont);
-
- /* load the picture */
- try {
- MyImage = getImage(new URL(getDocumentBase(), getParameter("IMAGE")));
- }
- catch (MalformedURLException e) {
- offscreenGraphics.setColor(Color.black);
- offscreenGraphics.drawString("Can't load the image", 50, 70);
- }
- repaint();
-
- /* find out if we are impotent */
- power = getParameter("POWER");
- if (power == null) power = "yes";
- if (power.equals("no")) empowered = false;
- else empowered = true;
-
- colorname = getParameter("COLOR");
- if (colorname == null) colorname = "red"; /* default color */
- MyName = getParameter("MONIKER");
- if (MyName == null) MyName = "";
-
- sc = new StarClient(this);
- dudes = new Vector();
- MyX = MyY = -100; /* Start the marker off-screen */
- MyColor = Color.red;
- }
-
- public void PaintBackground(){
- offscreenGraphics.drawImage(tree, 0, 0, this);
- offscreenGraphics.drawImage(tree, 100, 0, this);
- offscreenGraphics.drawImage(tree, 200, 0, this);
- offscreenGraphics.drawImage(tree, 0, 100, this);
- offscreenGraphics.drawImage(tree, 100, 100, this);
- offscreenGraphics.drawImage(tree, 200, 100, this);
- offscreenGraphics.drawImage(tree, 0, 200, this);
- offscreenGraphics.drawImage(tree, 100, 200, this);
- offscreenGraphics.drawImage(tree, 200, 200, this);
- repaint();
- }
- public void start() {
- if (kicker == null) {
- kicker = new Thread(this);
- kicker.start();
- }
- }
-
- public void stop() { /* stop main thread */
- if ( kicker !=null ) {
- kicker.stop() ;
- kicker = null ;
- }
- if ( sc !=null ) sc.stop(); /* stop and disconnect StarClient thread */
- }
-
- public boolean mouseDown(java.awt.Event evt, int x, int y)
- {
- feedback();
- if (empowered) {
- /* transmit click coordinates */
- String msg = " " + x + " " + y + " " + colorname + " " + MyName;
- sc.send(msg);
-
- /* draw pointer immediately */
- MyX = x; MyY = y;
-
- repaint();
- regionalist();
-
-
- }
- return true;
- }
-
- public boolean mouseUp(java.awt.Event evt, int x, int y)
- {
- return true;
- }
-
- /* Tell user how many are connected */
- public void feedback() {
- String m;
- if (MyName.equals("")) m = "";
- else m = "(" + MyName + ")";
- if (dudes.size() <= 1)
- showStatus("You're the only one here. " + m);
- else
- showStatus("There are " + dudes.size() + " people connected. " + m);
- }
-
- /* Hello, network. I'm alive. */
- public void beacon() {
- String msg = " -100 -100 " + colorname + " " + MyName;
- sc.send(msg);
- }
-
- public void run()
- {
-
- int announcement = 0;
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- sc.start(getDocumentBase().getHost(), 8121);
-
- while (kicker != null)
- {
- if (announcement < 5) {
- beacon(); PaintBackground();
- announcement++;
- }
- try {Thread.sleep(5000);} catch (InterruptedException e) {}
- repaint();
- }
- }
-
- public String[][] getParameterInfo() {
- String[][] info = {
- // Parameter Name Kind of Value Description
- {"image ", "URL ", "a GIF or JPEG file"},
- {"power ", "yes/no", "participant or observer?"},
- {"color ", "red,orange,yellow,green,blue" +
- "black,white,gray,darkGray,lightGray," +
- "magenta,pink,cyan", "pointer color"},
- {"moniker", "string", "name of participant"},
- };
- return info;
- }
-
- public String getAppletInfo() {
- return "MultiMouse by Jeff Breidenbach v1.0beta1";
- }
-
- /*************************/
- /*** STAR CLIENT STUFF ***/
- /*************************/
-
- /* called when the connection's all set */
- public void WS_up(String msg, Object who)
- {
- myStarID = sc.getStarID();
- showStatus(msg);
- }
-
- /* called when the connection's down */
- public void WS_down(String msg, Object who)
- {
- showStatus("The connection is broken!");
- }
-
- /* general messages and errors */
- public void WS_debugLog(String msg, Object who)
- {
- showStatus(msg);
- }
-
- /* gets called after reception of a new msg */
- public void WS_newMsg(String msg, Object who)
- {
- int FromID, x, y, deadID;
- boolean Understood;
- String tok;
- String name="";
- String col="red";
-
- FromID = -1; Understood = false; x = y = -100;
- StringTokenizer st = new StringTokenizer(msg);
-
- /* who is it from?.. */
- if(st.hasMoreTokens())
- FromID = (Integer.valueOf(st.nextToken())).intValue();
-
- /* Is it a special annoucement (i.e. from the server itself) */
- if(FromID == 0)
- {
- if(st.hasMoreTokens())
- {
- tok = st.nextToken();
- if( (tok.equals("OBITUARY"))&&(st.hasMoreTokens()) )
- {
- deadID = (Integer.valueOf(st.nextToken())).intValue();
- dudes.removeElement(new Integer(deadID));
- feedback();
- }
- }
- return;
- }
-
- /* ..or from a web client? -> interpreted as an (x,y) position */
- if(FromID > 0)
- {
- if(st.hasMoreTokens())
- {
- x = (Integer.valueOf(st.nextToken())).intValue();
- if(st.hasMoreTokens())
- {
- y = (Integer.valueOf(st.nextToken())).intValue();
- Understood = true;
- if(st.hasMoreTokens())
- {
- col = st.nextToken();
- }
- while (st.hasMoreTokens())
- {
- name = name + st.nextToken() + " ";
- }
- }
- }
- }
-
- /* Let's do something about it */
- if(Understood)
- {
- tracker(FromID, col, name);
- if(FromID == myStarID) { /* Was it from Myself? */
- }
- else { /* ..or from somebody else? */
- if (onscreen(x,y))
- {
- MyX = x; MyY = y; /* draw pointer */
- showStatus(name);
- colorcode(col);
- repaint();
- regionalist();
- }
- }
- }
- }
-
- public void colorcode(String col) {
- MyColor = Color.red;
- if (col.equals("red")) MyColor = Color.red;
- if (col.equals("orange")) MyColor = Color.orange;
- if (col.equals("yellow")) MyColor = Color.yellow;
- if (col.equals("green")) MyColor = Color.green;
- if (col.equals("blue")) MyColor = Color.blue;
- if (col.equals("black")) MyColor = Color.black;
- if (col.equals("white")) MyColor = Color.white;
- if (col.equals("gray")) MyColor = Color.gray;
- if (col.equals("darkGray")) MyColor = Color.darkGray;
- if (col.equals("lightGray")) MyColor = Color.lightGray;
- if (col.equals("magenta")) MyColor = Color.magenta;
- if (col.equals("pink")) MyColor = Color.pink;
- if (col.equals("cyan")) MyColor = Color.cyan;
- }
-
- /* keep track of who's connected */
- public void tracker(int ID, String thiscolor, String name) {
- Object thisdude = new Integer(ID);
- if (dudes.contains(thisdude)) {} /* I know this guy... */
- else { /* found a new dude! */
- dudes.addElement(thisdude); /* let's remember him */
- beacon(); /* Hello, new dude */
- feedback(); /* Hey, look! */
- }
- }
-
- /* is this mouseclick onscreen? */
- public boolean onscreen(int x, int y) {
- boolean answer;
- if ((x != -100) || (y != -100)) answer = true;
- else answer = false;
- return answer;
- }
-
- /**********************/
- /*** GRAPHICS STUFF ***/
- /**********************/
-
-
- /********* Look here !!!! *****************************/
- /* */
- /* This is what happens when you click on a "region" */
- /* */
- /******************************************************/
- public void animate_adjacent(int box) {
- if (box==0)
- {animate_help(1);
- animate_help(4);
- animate_help(3);}
- else if (box==1)
- {animate_help(0);
- animate_help(2);
- animate_help(3);
- animate_help(4);
- animate_help(5);}
- else if (box==2){
- animate_help(1);
- animate_help(4);
- animate_help(5);}
- else if(box==3){
- animate_help(0);
- animate_help(1);
- animate_help(4);
- animate_help(7);
- animate_help(6);}
- else if(box==4){
- animate_help(0);
- animate_help(1);
- animate_help(2);
- animate_help(3);
- animate_help(5);
- animate_help(6);
- animate_help(7);
- animate_help(8);}
- else if(box==5){
- animate_help(1);
- animate_help(2);
- animate_help(4);
- animate_help(7);
- animate_help(8);}
- else if(box==6){
- animate_help(3);
- animate_help(4);
- animate_help(7);}
- else if(box==7){
- animate_help(6);
- animate_help(3);
- animate_help(4);
- animate_help(5);
- animate_help(8);}
- else if(box==8){
- animate_help(5);
- animate_help(4);
- animate_help(7);}
-
- }
-
-
-
- public void animate_help(int box) {
- int r = (int)(((float)Math.random())*9);
- if (threads[box]==null)
- {
- threads[box] = new Flicker1(this, box,r);
- if (numclicks[box]==5) {
- animate_adjacent(box);
- numclicks[box]=0;}
- else numclicks[box]=(numclicks[box] + 1);
- }
- }
-
-
- public void regionalist() {
-
- Polygon p = new Polygon();
- int i,r;
-
- for (i=0; i<regions.size(); i++) {
-
- p = (Polygon) regions.elementAt(i);
- if (p.inside(MyX,MyY)) {
- r = (int)(((float)Math.random())*9);
-
- if (threads[i]==null)
- {
- threads[i] = new Flicker1(this, i,r);
- if (numclicks[i]==5) {
- animate_adjacent(i);
- numclicks[i]=0;}
- else numclicks[i]=(numclicks[i] + 1);
- }
- }
- }
- }
-
-
- public void update(Graphics g) {
- offscreenGraphics.drawImage(MyImage, 0, 0, this);
- paint(g);
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(offscreenImage, 0, 0, this);
- }
-
- public void animator(int box, int animation){
- int frames=animations[animation][0];
- int currimage=1;
- Image TempImage;
-
- sounds[box].loop();
-
- while (currimage <= frames) {
- TempImage = images[animations[animation][currimage]];
- getGraphics().drawImage(TempImage, xcoordinates[box], ycoordinates[box],this);
- System.out.println("Frame =" + animations[animation][currimage]);
- System.out.println("coordinates = " + xcoordinates[box] + " " + ycoordinates[box]);
-
- try{threads[box].sleep(200);}catch (InterruptedException e);
- ++currimage;
- }
- sounds[box].stop();
- }
-
- }
-
- class Flicker1 extends Thread {
- RhythmTree rt;
- int box, animation;
-
- public Flicker1(RhythmTree foo, int b, int a) {
- box = b;
- animation = a;
- rt = foo;
- this.start();
- }
-
- public void run() {
- rt.animator(box,animation);
- rt.threads[box] = null; /* suicide */
- }
- }
-
-
-
-
-
-
-
-
-