import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class test1 extends Applet { MotionCanvas X; Animator A; ControlCanvas C; public void init() { setBackground(Color.blue); A=new Animator(); X=new MotionCanvas(); C=new ControlCanvas(); A=A.addMotionCanvas(X); X=X.addAnimator(A); C=C.addAnimator(A); C=C.addMotionCanvas(X); X.resize(600,600); C.resize(180,600); X.setBackground(Color.black); C.setBackground(Color.blue); add(C); add(X); try {A.start();} catch(Exception e) {} } } class MotionCanvas extends Canvas implements MouseListener, MouseMotionListener { Animator A; Fish[] W=new Fish[5]; MotionCanvas() { addMouseListener(this); addMouseMotionListener(this); Color C[][]=new Color[5][5]; double X[][]=new double[5][5]; double Y[][]=new double[5][5]; double r[][]=new double[5][15]; double rr,x,y; Color c; X[1][1]=50.0; //xpos Y[1][1]=50.0; //ypos r[1][0]=0.2; //orientation r[1][1]=20.0; //radius r[1][2]=40.0; //tail length r[1][3]=.1; //tail vibration r[1][4]=.0223607; //periodicity r[1][5]=10.0; //speed C[1][1]=Color.yellow; //body color C[1][2]=Color.yellow; //tail color C[1][3]=Color.yellow; //eye color W[1]=new Fish(1,X[1],Y[1],r[1],C[1],this); X[2][1]=450.0; //xpos Y[2][1]=450.0; //ypos r[2][0]=0.5; //orientation r[2][1]=30.0; //radius r[2][2]=40.0; //tail length r[2][3]=.1; //tail vibration r[2][4]=.0141421; //periodicity r[2][5]=10.0; //speed C[2][1]=Color.white; //body color C[2][2]=Color.white; //tail color C[2][3]=Color.white; //eye color W[2]=new Fish(2,X[2],Y[2],r[2],C[2],this); X[3][1]=450.0; //xpos Y[3][1]=50.0; //ypos r[3][0]=.7; //orientation r[3][1]=25.0; //radius r[3][2]=40.0; //tail length r[3][3]=.1; //tail vibration r[3][4]=.0173205; //periodicity r[3][5]=10.0; //speed C[3][1]=Color.green; //body color C[3][2]=Color.orange; //tail color C[3][3]=Color.orange; //eye color W[3]=new Fish(3,X[3],Y[3],r[3],C[3],this); X[4][1]=50.0; //xpos Y[4][1]=450.0; //ypos r[4][0]=.7; //orientation r[4][1]=20.0; //radius r[4][2]=50.0; //tail length r[4][3]=.1; //tail vibration r[4][4]=.0273205; //periodicity r[4][5]=10.0; //speed C[4][1]=Color.pink; //body color C[4][2]=Color.pink; //tail color C[4][3]=Color.pink; //eye color W[4]=new Fish(4,X[4],Y[4],r[4],C[4],this); } MotionCanvas addAnimator(Animator A) { MotionCanvas Y=this; Y.A=A; return(Y); } public void paint(Graphics g) { W[1].render(g); W[2].render(g); W[3].render(g); W[4].render(g); } public void mouseDragged (MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} } class Vector { double x,y; Vector(double x,double y) { this.x=x; this.y=y; } Vector unit(Vector V) { double r; Vector W=V; r=V.x*V.x+V.y*V.y; r=Math.sqrt(r); if(r!=0.0) { W.x=V.x/r; W.y=V.y/r; } return(W); } Vector scale(double d) { Vector W=this; W.x=d*this.x; W.y=d*this.y; return(W); } Vector plus(Vector v) { Vector W=this; W.x=W.x+v.x; W.y=W.y+v.y; return(W); } double orient() { double d; d=Math.atan(this.y/this.x); d=d/Math.PI; d=d/2.0; if(this.x<0) d=d+.5; return(d); } } class Fish { MotionCanvas V; double[] X,Y; double r[]; Color C[]; int state; //state of Fish double mod,modd; //periodic motion int n; //fish number Fish(int n,double[] X,double[] Y,double r[],Color C[],MotionCanvas V) { this.X=X; this.Y=Y; this.r=r; this.C=C; state=0; mod=0.0; this.V=V; this.n=n; } void render(Graphics g) { int[] x=new int[10]; int[] y=new int[10]; double mod2; mod2=Math.sin(2.0*Math.PI*mod); x[1]=(int)(X[1]); y[1]=(int)(Y[1]); x[2]=(int)(x[1]+r[1]*Math.cos(2.0*Math.PI*r[0])); y[2]=(int)(y[1]+r[1]*Math.sin(2.0*Math.PI*r[0])); x[3]=(int)(x[1]+.5*r[1]*Math.cos(.5*Math.PI+2.0*Math.PI*r[0])); y[3]=(int)(y[1]+.5*r[1]*Math.sin(.5*Math.PI+2.0*Math.PI*r[0])); x[4]=(int)(x[1]-r[1]*Math.cos(2.0*Math.PI*r[0])); y[4]=(int)(y[1]-r[1]*Math.sin(2.0*Math.PI*r[0])); x[5]=(int)(x[1]+.5*r[1]*Math.cos(1.5*Math.PI+2.0*Math.PI*r[0])); y[5]=(int)(y[1]+.5*r[1]*Math.sin(1.5*Math.PI+2.0*Math.PI*r[0])); x[6]=(int)(.45*x[1]+.55*x[2]); y[6]=(int)(.45*y[1]+.55*y[2]); x[7]=(int)(2.0*x[4]-x[1]+mod2*(x[1]-x[3])); y[7]=(int)(2.0*y[4]-y[1]+mod2*(y[1]-y[3])); x[8]=(int)(r[1]/6.0); /*body*/ g.setColor(C[1]); g.drawLine(x[2],y[2],x[3],y[3]); g.drawLine(x[3],y[3],x[4],y[4]); g.drawLine(x[4],y[4],x[5],y[5]); g.drawLine(x[5],y[5],x[2],y[2]); g.fillOval(x[6]-x[8],y[6]-x[8],2*x[8],2*x[8]); g.drawLine(x[4],y[4],x[7],y[7]); } double distance(int j) { double d; double x1,x2,y1,y2; x1=V.W[j].X[1]; y1=V.W[j].Y[1]; x2=X[1]; y2=Y[1]; d=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return(d); } Vector look(int j) { double x1,x2,y1,y2; x1=V.W[j].X[1]; y1=V.W[j].Y[1]; x2=X[1]; y2=Y[1]; Vector v=new Vector(x1-x2,y1-y2); v=v.unit(v); return(v); } Fish update() { Fish T=this; Vector[] v=new Vector[10]; double[] d=new double[10]; for(int j=1;j<=4;++j) { d[j]=T.distance(j); v[j]=T.look(j); } if(n==1) { if(modd<.5) { T.X[1]=T.X[1]+5*v[4].x; T.Y[1]=T.Y[1]+5*v[4].y; T.r[0]=v[4].orient(); } if(modd>=.5) { T.X[1]=T.X[1]-5*v[2].x; T.Y[1]=T.Y[1]-5*v[2].y; T.r[0]=.5+v[2].orient(); } } if(n==2) { if(modd<.5) { T.X[1]=T.X[1]+5*v[1].x; T.Y[1]=T.Y[1]+5*v[1].y; T.r[0]=v[1].orient(); } if(modd>=.5) { T.X[1]=T.X[1]-5*v[3].x; T.Y[1]=T.Y[1]-5*v[3].y; T.r[0]=.5+v[3].orient(); } } if(n==3) { if(modd<.5) { T.X[1]=T.X[1]+5*v[2].x; T.Y[1]=T.Y[1]+5*v[2].y; T.r[0]=v[2].orient(); } if(modd>=.5) { T.X[1]=T.X[1]-5*v[4].x; T.Y[1]=T.Y[1]-5*v[4].y; T.r[0]=.5+v[4].orient(); } } if(n==4) { if(modd<.5) { T.X[1]=T.X[1]+5*v[3].x; T.Y[1]=T.Y[1]+5*v[3].y; T.r[0]=v[3].orient(); } if(modd>=.5) { T.X[1]=T.X[1]-5*v[1].x; T.Y[1]=T.Y[1]-5*v[1].y; T.r[0]=.5+v[1].orient(); } } if(T.X[1]>600-r[1]) T.X[1]=600-r[1]; if(T.Y[1]>600-r[1]) T.Y[1]=600-r[1]; if(T.X[1]