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(300,300); C.resize(120,300); X.setBackground(Color.black); C.setBackground(new Color(50,50,150)); add(C); add(X); try {A.start();} catch(Exception e) {} } } class DBCanvas extends Canvas { public void update(Graphics g) { Graphics g2; Image offscreen = null; offscreen = createImage(size().width, size().height); g2 = offscreen.getGraphics(); paint(g2); g.drawImage(offscreen, 0, 0, this); g2.dispose(); offscreen.flush(); } } class MotionCanvas extends DBCanvas implements MouseListener, MouseMotionListener { Animator A; Tadpole[] W=new Tadpole[5]; Barrier B; Gear G; 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; r[4][1]=75.0; //radius r[4][2]=30.0; //gear number r[4][3]=5.0; //speed r[4][4]=.85; //gear size r[4][5]=0.0; //phase r[4][6]=9.0; //eye size; r[4][7]=150.0; //xpos r[4][8]=150.0; //ypos C[4][1]=Color.cyan; //teeth color G=new Gear(r[4],C[4],this); x=150.0; //xpos y=150.0; //ypos r[1][1]=75.0; //radius c=Color.black; B=new Barrier(x,y,r[1][1],c,G); X[1][1]=50.0; //xpos Y[1][1]=50.0; //ypos X[1][2]=50.0; //goal xpos Y[1][2]=50.0; //goal ypos r[1][1]=9.0; //radius r[1][2]=30.0; //tail length r[1][3]=10.0; //tail vibration r[1][4]=1.0; //eye size r[1][5]=10.0; //speed C[1][1]=Color.blue; //body color C[1][2]=Color.yellow; //tail color C[1][3]=Color.green; //eye color W[1]=new Tadpole(X[1],Y[1],r[1],C[1],B); X[2][1]=50.0; //xpos Y[2][1]=100.0; //ypos X[2][2]=50.0; //goal xpos Y[2][2]=100.0; //goal ypos r[2][1]=9.0; //radius r[2][2]=30.0; //tail length r[2][3]=10.0; //tail vibration r[2][4]=1.0; //eye size r[2][5]=10.0; //speed C[2][1]=Color.blue; //body color C[2][2]=Color.red.brighter(); //tail color C[2][3]=Color.red; //eye color W[2]=new Tadpole(X[2],Y[2],r[2],C[2],B); X[3][1]=50.0; //xpos Y[3][1]=150.0; //ypos X[3][2]=50.0; //goal xpos Y[3][2]=150.0; //goal ypos r[3][1]=9.0; //radius r[3][2]=30.0; //tail length r[3][3]=10.0; //tail vibration r[3][4]=1.0; //eye size r[3][5]=10.0; //speed C[3][1]=Color.blue; //body color C[3][2]=Color.green; //tail color C[3][3]=Color.orange; //eye color W[3]=new Tadpole(X[3],Y[3],r[3],C[3],B); } 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); B.render(g); G.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) { e.consume(); Point p=new Point(); int inside1,inside2; double[] dist=new double[5]; p.x=e.getX(); p.y=e.getY(); for(int i=1;i<=3;++i) { dist[i]=W[i].distance(p); } inside1=B.inside(p,-.55*B.r); inside2=B.inside(p,W[1].r[1]); if((inside1==1)||(inside2==0)) { if((dist[1]0) V1=V1.scale(1.0-G.r[3]/10.0); V2=new Vector(x2,y2); V2=V2.unit(V2); V3=V1; if(block(T)<0) V3=V2; return(V3); } int inside(Point p,double rr) { double d; int test; test=0; d=(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y); d=Math.sqrt(d); if(dr[1]-r[4]) { ex=.7*ex/er; ey=.7*ey/er; x1=(int)(x+r[1]*ex); y1=(int)(y+r[1]*ey); } if(er<=r[1]-r[4]) { opt=1; x1=(int)(eye.x); y1=(int)(eye.y); } g.fillOval((int)(x1-r[4]),(int)(y1-r[4]),(int)(2*r[4]),(int)(2*r[4])); g.drawOval((int)(x1-r[4]),(int)(y1-r[4]),(int)(2*r[4]),(int)(2*r[4])); /*************/ /*******goal*****/ g.setColor(C[1]); g.setColor(Color.red); if(state==2) g.fillOval((int)(X[2]-4),(int)(Y[2]-4),8,8); /***************/ /********tail********/ g.setColor(C[2]); if(state==0) { gx1=-ex/.7; gy1=-ey/.7; gr1=1.0; gx2=gx1; gy2=gy1; } if(state!=0) { gx1=X[2]-X[1]; gy1=Y[2]-Y[1]; gr1=Math.sqrt(gx1*gx1+gy1*gy1); gx1=-gx1/gr1; gy1=-gy1/gr1; gx2=gx1-.4*gy1*Math.cos(2*Math.PI*mod); gy2=gy1+.4*gx1*Math.cos(2*Math.PI*mod); } x3=(int)(x+r[1]*gx1); y3=(int)(y+r[1]*gy1); x4=(int)(x+r[1]*gx1+r[2]*gx2); y4=(int)(y+r[1]*gy1+r[2]*gy2); if(opt==0) g.drawLine(x3,y3,x4,y4); } double distance(Point p) { double dist; dist=(X[1]-p.x)*(X[1]-p.x)+(Y[1]-p.y)*(Y[1]-p.y); dist=Math.sqrt(dist); return(dist); } Tadpole update() { double dist; double x,y; int touch; Tadpole T=this; dist=(X[1]-X[2])*(X[1]-X[2])+(Y[1]-Y[2])*(Y[1]-Y[2]); dist=Math.sqrt(dist); if(dist>r[1]) state=2; if((dist<=r[1])&&(dist>1.5*r[5])) state=1; if(dist<1.5*r[5]) state=0; touch=B.touch(this); if(state==0) { T.X[1]=X[2]; T.Y[1]=Y[2]; } if((state!=0)&&(touch==0)) { x=(X[2]-X[1])/dist; y=(Y[2]-Y[1])/dist; T.X[1]=X[1]+r[5]*x; T.Y[1]=Y[1]+r[5]*y; } if((state!=0)&&(touch!=0)) { int esc; Vector V; V=B.move(this); T.X[1]=X[1]+r[5]*V.x; T.Y[1]=Y[1]+r[5]*V.y; } T.mod=mod+.01*r[3]; return(T); } } class VSlider { int x,y,w,h; int pos; Color[] C; String S; VSlider(int x,int y,int w,int h,int pos,Color[] C,String S) { 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) { g.setColor(C[1]); g.fillRect(x,y,w,h); g.setColor(C[2]); g.drawRect(x,y,w,h); g.setColor(C[3]); g.drawRect(x+1,pos-1,w-2,2); g.setColor(C[2]); g.setFont(new Font("TimesRoman",Font.PLAIN,13)); g.drawString(S,x,y+h+12); } int inside(Point p) { int test=0; if((p.x>x)&&(p.xy)&&(p.y