import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class test1 extends Applet { AllCanvas P; public void init() { setBackground(Color.black); P=new AllCanvas(); P.resize(1200,1000); P.setBackground(Color.black); this.add(P); } } 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); offscreen.flush(); } } class AllCanvas extends DBCanvas implements MouseListener, MouseMotionListener { int w1,w2,w3,w4; int SIZE; int near; ViewInteger v1,v2,v3; ListenSquare L[]=new ListenSquare[10]; PolyVector P; PolyVector Q; AllCanvas() { addMouseListener(this); addMouseMotionListener(this); w1=7; w2=w1; w3=1; w4=0; SIZE=260; P=new PolyVector(SIZE); Q=new PolyVector(SIZE); P=P.regular(w1); L[1]=new ListenSquare(10,35,15,15,Color.blue,"-",1); L[2]=new ListenSquare(50,35,15,15,Color.blue,"+",2); L[3]=new ListenSquare(10,55,15,15,Color.blue,"-",3); L[4]=new ListenSquare(50,55,15,15,Color.blue,"+",4); L[5]=new ListenSquare(10,75,15,15,Color.blue,"",5); L[6]=new ListenSquare(30,75,15,15,Color.blue,"",6); L[7]=new ListenSquare(10,95,15,15,Color.blue,"-",7); L[8]=new ListenSquare(30,95,15,15,Color.blue,"+",8); v1=new ViewInteger(w1); v2=new ViewInteger(w2); } public void paint(Graphics g) { g.setColor(Color.red); g.setFont(new Font("TimesRoman",Font.PLAIN,28)); g.drawString("Rich: Applet 31",5,25); g.setFont(new Font("TimesRoman",Font.PLAIN,15)); g.setColor(Color.red); g.drawString("click on the blue circle",8,130); g.drawString("to change the polygon",8,146); g.setColor(Color.white); g.drawString("# vertices",70,50); g.drawString("# steps",70,70); g.drawString("resize",52,110); g.drawString("# steps",70,70); g.drawString("filling",52,90); v1.x=w1; v2.x=w2; v1.render(37,48,Color.white,g); v2.render(37,68,Color.white,g); if(w3==1) { g.setColor(Color.white); g.fillRect(15,80,5,5); } if(w3==2) { g.setColor(Color.white); g.fillRect(35,80,5,5); } for(int i=1;i<=8;++i) L[i].render(g); g.setColor(Color.blue); g.drawOval(210,30,2*SIZE-60,2*SIZE-60); g.translate(SIZE+180,SIZE); if(w3==1) { P.render(g,Color.white,3); Q=P.penta(); for(int i=1;i<=w2;++i) { Q.render(g,Color.white,3); Q=Q.penta(); } P.render(g,Color.white,1); } if(w3==2) { P.render(g,Color.red,4); Q=P.penta(); for(int i=1;i<=w2;++i) { int test=i-2*(i/2); Color H=Color.black; if(test==0) H=Color.red; if(test==1) H=Color.black; Q.render(g,H,4); Q=Q.penta(); } P.render(g,Color.black,1); } } public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseDragged(MouseEvent e) { e.consume(); Point p=new Point(); p.x=e.getX(); p.y=e.getY(); int test=0; for(int i=1;i<=8;++i) { if(L[i].inside(p)==1) test=i; } if((test==1)&&(w1>5)) {--w1;w2=w1;P=P.regular(w1);} if((test==2)&&(w1<20)) { ++w1;w2=w1;P=P.regular(w1);} if((test==3)&&(w2>1)) --w2; if((test==4)&&(w2<4*w1)) ++w2; if(test==5) w3=1; if(test==6) w3=2; if((test==7)&&(SIZE>180)) SIZE=SIZE-20; if((test==8)&&(SIZE<500)) SIZE=SIZE+20; P.SIZE=SIZE; if(test>0) repaint(); Point q=new Point(); q.x=p.x-SIZE-180; q.y=p.y-SIZE; double rad=Math.sqrt(q.x*q.x+q.y*q.y); if((rad>SIZE-50)&&(rad5)) {--w1;w2=w1;P=P.regular(w1);} if((test==2)&&(w1<20)) { ++w1;w2=w1;P=P.regular(w1);} if((test==3)&&(w2>1)) --w2; if((test==4)&&(w2<4*w1)) ++w2; if(test==5) w3=1; if(test==6) w3=2; if((test==7)&&(SIZE>180)) SIZE=SIZE-20; if((test==8)&&(SIZE<500)) SIZE=SIZE+20; P.SIZE=SIZE; if(test>0) repaint(); Point q=new Point(); q.x=p.x-SIZE-180; q.y=p.y-SIZE; double rad=Math.sqrt(q.x*q.x+q.y*q.y); if((rad>SIZE-50)&&(radn) b=b-n; if(c>n) c=c-n; if(d>n) d=d-n; V=V.cross(v[a],v[c]); W=W.cross(v[b],v[d]); X=X.cross(V,W); X.x=X.x/X.z; X.y=X.y/X.z; X.z=1.0; return(X); } PolyVector penta() { PolyVector R=new PolyVector(SIZE); R.n=n; for(int i=1;i<=n;++i) R.v[i]=localPenta(i); R.v[n+1]=R.v[1]; R.v[n+2]=R.v[2]; return(R); } void render(Graphics g,Color C,int k) { g.setColor(C); if(k==1) { for(int i=1;i<=n;++i) { int x1=(int)((SIZE-30)*this.v[i].x/this.v[i].z); int y1=(int)((SIZE-30)*this.v[i].y/this.v[i].z); int x2=(int)((SIZE-30)*this.v[i+1].x/this.v[i+1].z); int y2=(int)((SIZE-30)*this.v[i+1].y/this.v[i+1].z); g.setColor(C); g.drawLine(x1,y1,x2,y2); g.setColor(Color.red.brighter()); int f=(int)(SIZE/60); g.fillOval((SIZE-30+2*f)*x1/(SIZE-30)-f,(SIZE-30+2*f)*y1/(SIZE-30)-f,2*f, 2*f); } } if(k==2) { for(int i=1;i<=n;++i) { int x1=(int)((SIZE-30)*this.v[i].x/this.v[i].z); int y1=(int)((SIZE-30)*this.v[i].y/this.v[i].z); int x2=(int)((SIZE-30)*this.v[i+1].x/this.v[i+1].z); int y2=(int)((SIZE-30)*this.v[i+1].y/this.v[i+1].z); int x3=(int)((SIZE-30)*this.v[i+2].x/this.v[i+2].z); int y3=(int)((SIZE-30)*this.v[i+2].y/this.v[i+2].z); g.setColor(C); g.drawLine(x1,y1,x2,y2); } } if(k==3) { for(int i=1;i<=n;++i) { int x1=(int)((SIZE-30)*this.v[i].x/this.v[i].z); int y1=(int)((SIZE-30)*this.v[i].y/this.v[i].z); int x3=(int)((SIZE-30)*this.v[i+2].x/this.v[i+2].z); int y3=(int)((SIZE-30)*this.v[i+2].y/this.v[i+2].z); g.setColor(C); g.drawLine(x1,y1,x3,y3); } } if(k==4) { for(int i=1;i<=n;++i) { int x1=(int)((SIZE-30)*this.v[i].x/this.v[i].z); int y1=(int)((SIZE-30)*this.v[i].y/this.v[i].z); int x2=(int)((SIZE-30)*this.v[i+1].x/this.v[i+1].z); int y2=(int)((SIZE-30)*this.v[i+1].y/this.v[i+1].z); int x3=(int)((SIZE-30)*this.v[1].x/this.v[1].z); int y3=(int)((SIZE-30)*this.v[1].y/this.v[1].z); g.setColor(C); int[] xp={x1,x2,x3}; int[] yp={y1,y2,y3}; g.fillPolygon(xp,yp,3); } } } int near(Point p) { double min=10000000; int val=0; double test=0; double x,y; for(int i=1;i<=n;++i) { x=v[i].x/v[i].z; y=v[i].y/v[i].z; test=(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y); if(testx)&&(p.xy)&&(p.y0) { int z=y-10*(y/10); g.setColor(C); char ch=(char)(z+48); Character CH1=new Character(ch); g.drawString(CH1.toString(),u-place,v); place=place+8; y=(y-z)/10; } if(x==0) g.drawString("0",u,v); } }