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=X.addControlCanvas(C); X.resize(400,400); C.resize(300,400); X.setBackground(Color.black); C.setBackground(new Color(0,0,150)); add(C); add(X); try {A.start();} catch(Exception e) {} } } class MotionCanvas extends DBCanvas implements MouseListener, MouseMotionListener { Animator A; ControlCanvas C; Fish[] W=new Fish[7]; Color COL[][]=new Color[7][7]; MotionCanvas() { addMouseListener(this); addMouseMotionListener(this); double X[][]=new double[7][7]; double Y[][]=new double[7][7]; double r[][]=new double[7][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]=.02; //periodicity r[1][5]=10.0; //speed COL[1][1]=Color.yellow; //body color COL[1][2]=Color.yellow; //tail color COL[1][3]=Color.blue; //eye color W[1]=new Fish(1,X[1],Y[1],r[1],COL[1],this); X[2][1]=350.0; //xpos Y[2][1]=550.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]=.02; //periodicity r[2][5]=10.0; //speed COL[2][1]=Color.white; //body color COL[2][2]=Color.white; //tail color COL[2][3]=Color.blue; //eye color W[2]=new Fish(2,X[2],Y[2],r[2],COL[2],this); X[3][1]=350.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]=.02; //periodicity r[3][5]=10.0; //speed COL[3][1]=Color.green; //body color COL[3][2]=Color.green; //tail color COL[3][3]=Color.blue; //eye color W[3]=new Fish(3,X[3],Y[3],r[3],COL[3],this); X[4][1]=50.0; //xpos Y[4][1]=350.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]=.02; //periodicity r[4][5]=10.0; //speed COL[4][1]=Color.pink; //body color COL[4][2]=Color.pink; //tail color COL[4][3]=Color.blue; //eye color W[4]=new Fish(4,X[4],Y[4],r[4],COL[4],this); X[5][1]=200.0; //xpos Y[5][1]=200.0; //ypos r[5][0]=.7; //orientation r[5][1]=30.0; //radius r[5][2]=50.0; //tail length r[5][3]=.1; //tail vibration r[5][4]=.02; //periodicity r[5][5]=10.0; //speed COL[5][1]=Color.red; //body color COL[5][2]=Color.red; //tail color COL[5][3]=Color.blue; //eye color W[5]=new Fish(5,X[5],Y[5],r[5],COL[5],this); X[6][1]=250.0; //xpos Y[6][1]=150.0; //ypos r[6][0]=.7; //orientation r[6][1]=35.0; //radius r[6][2]=50.0; //tail length r[6][3]=.1; //tail vibration r[6][4]=.02; //periodicity r[6][5]=10.0; //speed COL[6][1]=Color.orange; //body color COL[6][2]=Color.orange; //tail color COL[6][3]=Color.blue; //eye color W[6]=new Fish(6,X[6],Y[6],r[6],COL[6],this); } MotionCanvas addAnimator(Animator A) { MotionCanvas Y=this; Y.A=A; return(Y); } MotionCanvas addControlCanvas(ControlCanvas A) { MotionCanvas Y=this; Y.C=A; return(Y); } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for(int i=1;i<=C.fish;++i) W[i].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 COL[]; int state; //state of Fish double mod,modd; //periodic motion int n; //fish number Fish(int n,double[] X,double[] Y,double r[],Color COL[],MotionCanvas V) { this.X=X; this.Y=Y; this.r=r; this.COL=COL; 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); x[9]=(int)(y[4]-y[7]); y[9]=(int)(x[7]-x[4]); /*body*/ g.setColor(COL[1]); int[] X={x[2],x[3],x[4],x[5]}; int[] Y={y[2],y[3],y[4],y[5]}; g.fillPolygon(X,Y,4); g.setColor(COL[3]); g.fillOval(x[6]-x[8],y[6]-x[8],2*x[8],2*x[8]); g.drawOval(x[6]-x[8],y[6]-x[8],2*x[8],2*x[8]); g.setColor(COL[2]); int[] X2={x[4],(int)(x[7]-.2*x[9]), x[7],(int)(x[7]+.2*x[9])}; int[] Y2={y[4],(int)(y[7]-.2*y[9]), y[7],(int)(y[7]+.2*y[9])}; g.fillPolygon(X2,Y2,4); } 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<=V.C.fish;++j) { d[j]=T.distance(j); v[j]=T.look(j); } if(modd<.5) { T.X[1]=T.X[1]+5*v[V.C.repel[n]].x; T.Y[1]=T.Y[1]+5*v[V.C.repel[n]].y; T.r[0]=v[V.C.repel[n]].orient(); } if(modd>=.5) { T.X[1]=T.X[1]-5*v[V.C.attract[n]].x; T.Y[1]=T.Y[1]-5*v[V.C.attract[n]].y; T.r[0]=.5+v[V.C.attract[n]].orient(); } if(T.X[1]>400-r[1]) T.X[1]=400-r[1]; if(T.Y[1]>400-r[1]) T.Y[1]=400-r[1]; if(T.X[1]100)&&(dist<400)) {attractTest=0;repelTest=i;} } dist=(P.x-x[0])*(P.x-x[0])+(P.y-y[0])*(P.y-y[0]); if(dist<225) {reconfigure(); repaint();} if(attractTest>0) { int temp=0; temp=attract[attractTest]+1; if(temp==fish+1) temp=1; if(temp==attractTest) ++temp; if(temp==fish+1) temp=1; attract[attractTest]=temp; repaint(); } if(repelTest>0) { int temp=0; temp=repel[repelTest]+1; if(temp==fish+1) temp=1; if(temp==repelTest) ++temp; if(temp==fish+1) temp=1; repel[repelTest]=temp; repaint(); } for(int i=1;i<=fish;++i) { if(V[i].inside(P)==1) { double sp=(P.x-V[i].x)/10000.0+.0075; System.out.println(sp); X.W[i].r[4]=sp; V[i].pos=P.x; V[i].redo(this); } } } } class Animator extends Thread { MotionCanvas X; public void run() { while(true) { for(int i=1;i<=X.C.fish;++i) X.W[i]=X.W[i].update(); X.repaint(); try {sleep(40);} catch(Exception e) {} } } Animator addMotionCanvas(MotionCanvas X) { Animator B=this; B.X=X; return(B); } } class HSlider { int x,y,w,h; int pos; Color[] C; String S; HSlider(int x,int y,int w,int h,int pos) { this.x=x; this.y=y; this.h=h; this.w=w; this.pos=pos; this.C=C; this.S=S; } void render(Graphics g,Color C) { g.setColor(C); g.fillRect(x,y,w,h); g.setColor(Color.white); g.drawRect(x,y,w,h); g.setColor(Color.black); g.drawRect(pos-1,y+1,2,h-2); } int inside(Point p) { int test=0; if((p.x>x)&&(p.xy)&&(p.y