import java.applet.Applet; import java.awt.event.*; import java.awt.*; /*Applet 10 is similar to Applets 8 and 9, except that one moves a pentagon around rather than a square. Accordingly the class PolyFlip replaces the class Checker. PolyFlip is considerably more general than what we use here. This applet is a warmup for the more involved Applets 12-14. Applet 14 gives more methods for PolyFlip and uses its full generality. The ListenTriangle class is slightly different than it was in the earlier applets.*/ public class test1 extends Applet { PlayingBoard board1; TextCanvas board2; int SIZE=10; public void init() { setBackground(Color.blue); board1=new PlayingBoard(SIZE); board1.resize(40*SIZE,40*SIZE); board2=new TextCanvas(); board1.setBackground(Color.black); board2.setBackground(Color.blue); board2.resize(40*SIZE,5*SIZE); add(board1); add(board2); } } class TextCanvas extends Canvas { Canvas C; TextCanvas() { this.C=C; } public void paint(Graphics g) { g.setFont(new Font("Helvetica",Font.PLAIN,25)); g.setColor(Color.green); g.drawString("Rich: Applet 10",5,25); g.setFont(new Font("Helvetica",Font.PLAIN,15)); g.setColor(Color.white); g.drawString("Click on the pentagon to move it.",190,20); } } 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 PlayingBoard extends DBCanvas implements MouseListener { int SIZE; Complex cen; PolyFlip P; PlayingBoard(int SIZE) { this.SIZE=SIZE; Color[] C=new Color[10]; C[0]=Color.cyan; C[1]=Color.red; C[2]=Color.yellow; C[3]=Color.magenta; C[4]=Color.orange; cen=new Complex(); cen.x=20*SIZE; cen.y=20*SIZE; P=new PolyFlip(0.0,40.0,1,5,cen,0,1,0,C); addMouseListener(this); } public void paint(Graphics g) { P.render(g,Color.white); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) { Point y=new Point(); e.consume(); y.x=e.getX(); y.y=e.getY(); if(P.inside(y)>=0) P=P.flip(P.inside(y)); repaint(); } } class Complex { double x,y; Complex() { this.x=x; this.y=y; } } class ListenTriangle { Polygon P; ListenTriangle(Polygon P) { this.P=P; } int inside(Point p) { int x1,x2,x3,y1,y2,y3; int v1x,v1y,v2x,v2y; int A1,A2,A3; int value; Polygon Q; Q=this.P; x1=Q.xpoints[0]; x2=Q.xpoints[1]; x3=Q.xpoints[2]; y1=Q.ypoints[0]; y2=Q.ypoints[1]; y3=Q.ypoints[2]; v1x=p.x-x1; v1y=p.y-y1; v2x=p.x-x2; v2y=p.y-y2; A3=v1x*v2y-v1y*v2x; v1x=p.x-x2; v1y=p.y-y2; v2x=p.x-x3; v2y=p.y-y3; A1=v1x*v2y-v1y*v2x; v1x=p.x-x3; v1y=p.y-y3; v2x=p.x-x1; v2y=p.y-y1; A2=v1x*v2y-v1y*v2x; value=0; if((A1<0)&&(A2<0)&&(A3<0)) value=1; if((A1>0)&&(A2>0)&&(A3>0)) value=1; return(value); } } class PolyFlip { Color[] C; Complex c; double r1,r2; int mode; int hide; int angle; int k,n; PolyFlip(double r1,double r2,int k,int n,Complex c,int hide,int mode,int angle,Color[] C) { this.k=k; this.n=n; this.r1=r1; this.r2=r2; this.c=c; this.mode=mode; this.hide=hide; this.angle=angle; this.C=C; hide=0; } void render(Graphics g,Color C) { double x1,x2,x3,y1,y2,y3,x4,y4,x5,y5; int i,m; double ii,nn; Polygon P=new Polygon(); for(i=0;i-1) { if(this.mode==0) { ii=i*this.k; nn=this.n; x=this.r2*Math.cos(2.0*Math.PI*(this.angle+ii)/nn); y=this.r2*Math.sin(2.0*Math.PI*(this.angle+ii)/nn); len=2*Math.cos(Math.PI/nn); Q.c.x=Q.c.x+len*x; Q.c.y=Q.c.y-len*y; Q.angle=this.angle+2*this.k*i; } if(this.mode==1) { ii=-i*this.k; nn=this.n; x=this.r2*Math.cos(2.0*Math.PI*(this.angle+ii)/nn); y=this.r2*Math.sin(2.0*Math.PI*(this.angle+ii)/nn); len=2*Math.cos(Math.PI/nn); Q.c.x=Q.c.x-len*x; Q.c.y=Q.c.y+len*y; Q.angle=-2*this.k*i+this.angle; } Q.mode=1-this.mode; } return(Q); } }