import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; public class test1 extends Applet { PictureCanvas P; Manager M; Animator A; TextCanvas T; public void init() { setBackground(Color.blue); P=new PictureCanvas(); P.setBackground(Color.black); A=new Animator(); P.setSize(300,300); T=new TextCanvas(); T.setSize(300,100); T.setBackground(Color.black); add(P); add(T); M=new Manager(); M.P=P; M.A=A; M.T=T; A.M=M; P.M=M; T.M=M; try {A.start();} catch(Exception e) {} A.suspend(); } } class Manager { PictureCanvas P; TextCanvas T; Animator A; Manager() {} } class DBCanvas extends Canvas { public void update(Graphics g) { Graphics g2; Image offscreen = null; offscreen = createImage(getSize().width, getSize().height); g2 = offscreen.getGraphics(); paint(g2); g.drawImage(offscreen, 0, 0, this); g2.dispose(); offscreen.flush(); } } class TextCanvas extends DBCanvas implements MouseListener,MouseMotionListener { ListenSquare L,L2; Manager M; TextCanvas() { addMouseListener(this); addMouseMotionListener(this); L=new ListenSquare(109,6,85,17); L2=new ListenSquare(255,67,23,23); L.type=1; L.state=150; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.blue); g.setFont(new Font("Helvetica",Font.PLAIN,30)); g.drawString("Rich: Applet 42",5,60); g.setColor(Color.yellow); g.setFont(new Font("Helvetica",Font.PLAIN,15)); g.setColor(Color.red); g.drawString("Click red box to work demo",38,90); g.setColor(Color.yellow); g.drawString("drag slider to",4,20); g.drawString("modify demo",202,20); g.translate(0,5); PolyVector P1=new PolyVector(); P1=P1.regular(11,2,4,260,46,20); P1.render(g,Color.blue,Color.blue); PolyVector P3=new PolyVector(); P3=P3.regular(2,1,4,287,59,9); P3.render(g,Color.yellow,Color.yellow); L2.render(g,Color.red,Color.red); g.translate(0,-5); L.render(g,Color.yellow,Color.yellow,Color.blue); } public void mousePressed(MouseEvent e) { e.consume(); M.P.choice=0; Point p=new Point(); p.x=e.getX(); p.y=e.getY(); if(L.inside(p)==1) M.P.choice=1; if((M.P.state>0)&&(M.P.state<25)) M.P.choice=0; } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { e.consume(); int x=e.getX(); if((x>110)&&(x<194)&&(M.P.choice==1)) { M.P.val=(230.0-x)/160.0; L.state=x; if(M.P.state==0) M.P.reset(1,M.P.val); if(M.P.state==25) M.P.reset(2,M.P.val); M.A.suspend(); repaint(); M.P.repaint(); } } public void mouseClicked(MouseEvent e) { e.consume(); int x=e.getX(); int y=e.getY(); Point p=new Point(); p.x=x; p.y=y-5; if(L2.inside(p)==1) { if(M.P.choice==0) { ++M.P.go; if(M.P.go==3) M.P.go=1; M.A.resume(); } } if((x>110)&&(x<194)&&(M.P.choice==1)) { M.P.val=(230.0-x)/160.0; L.state=x; if(M.P.state==0) M.P.reset(1,M.P.val); if(M.P.state==25) M.P.reset(2,M.P.val); M.A.suspend(); repaint(); M.P.repaint(); } } } class PictureCanvas extends DBCanvas { Manager M; PolyVector[] P=new PolyVector[20]; double val; int choice; int state; int go; PictureCanvas() { for(int j=1;j<=9;++j) P[j]=new PolyVector(); val=.5; choice=0; state=0; reset(1,val); go=0; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); P[6].render(g,Color.blue,Color.blue); P[4].render(g,Color.yellow,Color.yellow); P[5].render(g,new Color(210,0,0),new Color(210,0,0)); P[3].render(g,Color.red,Color.red); P[2].render(g,Color.orange,Color.orange); if(state==0) { P[4].render(g,Color.yellow,Color.yellow); P[5].render(g,new Color(210,0,0),new Color(210,0,0)); } if(state==25) { P[7].render(g,Color.orange,Color.orange); P[8].render(g,new Color(210,0,0),new Color(210,0,0)); P[5].render(g,new Color(210,0,0),new Color(210,0,0)); } } void reset(int jj,double v) { P[1]=P[1].regular(2,3,4,103,104,140); for(int j=2;j<=5;++j) { P[j]=P[1].triangle(j-1,v); } for(int j=1;j<=4;++j) P[6].v[j]=P[1+j].v[3]; P[6].n=4; P[7].n=3; P[7].v[1]=P[4].v[1]; P[7].v[2]=P[7].v[2].add(P[4].v[1].scale(1-v),P[4].v[2].scale(v)); P[7].v[3]=P[7].v[3].add(P[4].v[1].scale(1-v),P[4].v[3].scale(v)); P[8].n=4; P[8].v[1]=P[3].v[2]; P[8].v[2]=P[8].v[2].add(P[3].v[1].scale(v),P[3].v[2].scale(1-v)); P[8].v[3]=P[8].v[3].add(P[3].v[1].scale(v),P[3].v[3].scale(1-v)); P[8].v[4]=P[3].v[3]; P[9].n=2; P[9].v[1]=P[7].v[2]; P[9].v[2]=P[8].v[3]; state=0; go=0; if(jj>1) { P[2]=P[2].rotate(1,-Math.PI/2.0); P[3]=P[3].rotate(2,Math.PI/2.0); P[8]=P[8].rotate(2,P[3],Math.PI/2.0); state=25; go=1; } } void evolve() { if(go==1) { if(state<25) { P[2]=P[2].rotate(1,-Math.PI/50.0); P[3]=P[3].rotate(2,Math.PI/50.0); P[8]=P[8].rotate(2,P[3],Math.PI/50.0);state=state+1; repaint(); } } if(go==2) { if(state>0) { P[2]=P[2].rotate(1,Math.PI/50.0); P[3]=P[3].rotate(2,-Math.PI/50.0); P[8]=P[8].rotate(2,P[3],-Math.PI/50.0);state=state-1; repaint(); } } } } class Vector { double x,y,z; Vector() { this.x=0; this.y=0; this.z=0; } Vector(double x,double y,double z) { this.x=x; this.y=y; this.z=z; } Vector cross(Vector V,Vector W) { Vector X=new Vector(); X.x=V.y*W.z-V.z*W.y; X.y=V.z*W.x-V.x*W.z; X.z=V.x*W.y-V.y*W.x; return(X); } Vector normalize() { Vector X=this; X.x=x/z; X.y=y/z; X.z=1.0; return(X); } double norm() { double d=0; d=x*x+y*y+z*z; d=Math.sqrt(d); return(d); } Vector unit() { Vector V=new Vector(); double d=this.norm(); V.x=x/d; V.y=y/d; V.z=z/d; return(V); } Vector rotate(double d) { Vector W=new Vector(); W.x=x*Math.cos(d)+y*Math.sin(d); W.y=-x*Math.sin(d)+y*Math.cos(d); W.z=1.0; return(W); } double dot(Vector V,Vector W) { double d=0; d=V.x*W.x+V.y*W.y+V.z*W.z; return(d); } Vector scale(double d) { Vector W=new Vector(); W.x=d*x; W.y=d*y; W.z=d*z; return(W); } Vector add(Vector V,Vector W) { Vector X=new Vector(); X.x=V.x+W.x; X.y=V.y+W.y; X.z=V.z+W.z; return(X); } Vector sub(Vector V,Vector W) { Vector X=new Vector(); X.x=V.x-W.x; X.y=V.y-W.y; X.z=V.z-W.z; return(X); } Vector reflect(Vector W) { Vector X=new Vector(); double d=X.dot(this,W); X=X.sub(this.scale(2*d),W); return(X); } Vector reflect(Vector A,Vector B) { Vector[] V=new Vector[10]; V[1]=A; V[2]=B; V[1].z=1; V[2].z=1; V[3]=sub(V[1],V[2]); V[3]=V[3].unit(); V[4]=sub(this,V[2]); V[5]=V[3].reflect(V[4]); V[6]=add(V[5],V[2]); V[6].z=1; return(V[6]); } void print() { System.out.println(x+" "+y+ " " +z); } } class PolyVector { Vector[] v=new Vector[11]; int n; PolyVector() { for(int i=1;i<=10;++i) v[i]=new Vector(); } void render(Graphics2D g,Color C1,Color C2) { float[] X=new float[11]; float[] Y=new float[11]; for(int i=0;i<=8;++i) { X[i]=(float)(this.v[i+1].x/this.v[i+1].z); Y[i]=(float)(this.v[i+1].y/this.v[i+1].z); } GeneralPath path=new GeneralPath(); path.moveTo(X[0],Y[0]); for(int i=1;in) k=k-n; if(k<1) k=k+n; return(k); } } class ListenSquare { double x1,x2; double y1,y2; int state; int type; Color C; ListenSquare(double x1,double y1,double x2,double y2) { this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; this.state=0; this.type=0; } int inside(Point p) { int val; val=1; if(p.x<=this.x1) val=0; if(p.x>this.x1+this.x2) val=0; if(p.y<=this.y1) val=0; if(p.y>this.y1+this.y2) val=0; return(val); } void render(Graphics g,Color C1,Color C2,Color C3) { int xx1,yy1,xx2,yy2; xx1=(int)(this.x1); yy1=(int)(this.y1); xx2=(int)(int)(this.x2); yy2=(int)(int)(this.y2); g.setColor(C1); g.fillRect(xx1,yy1,xx2,yy2); g.setColor(C2); g.drawRect(xx1,yy1,xx2,yy2); g.setColor(C3); if(type==1) g.fillRect(state-3,yy1,6,yy2); if(type==1) g.drawRect(state-3,yy1,6,yy2); if(type==2) g.fillRect(xx1,state-3,xx2,6); } void render(Graphics g,Color C1,Color C2) { int xx1,yy1,xx2,yy2; xx1=(int)(this.x1); yy1=(int)(this.y1); xx2=(int)(int)(this.x2); yy2=(int)(int)(this.y2); g.setColor(C1); g.fillRect(xx1,yy1,xx2,yy2); g.setColor(C2); g.drawRect(xx1,yy1,xx2,yy2); } } class Animator extends Thread { Manager M; public void run() { while(true) { M.P.evolve(); try {sleep(50);} catch(Exception e) {} } } }