import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class test1 extends Applet { PixCanvas B; public void init() { B=new PixCanvas(); B.resize(600,600); B.setBackground(Color.black); setBackground(Color.black); this.add(B); } } 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 PixCanvas extends DBCanvas implements MouseListener, MouseMotionListener { Color[] COL=new Color[100]; int HEIGHT, WIDTH, HEIGHT2,WIDTH2; ListenTriangle[] H=new ListenTriangle[6]; ListenTriangle[] W=new ListenTriangle[6]; PixCanvas() { addMouseListener(this); addMouseMotionListener(this); COL[0]=Color.cyan; COL[1]=Color.yellow; COL[2]=Color.magenta; COL[3]=Color.cyan; COL[4]=Color.yellow; COL[5]=Color.magenta; COL[6]=Color.cyan; COL[7]=Color.yellow; COL[8]=Color.magenta; COL[9]=Color.cyan; COL[10]=Color.yellow; COL[11]=Color.red; for(int i=1;i<=5;++i) { H[i]=new ListenTriangle(); W[i]=new ListenTriangle(); } H[1].x1=5; H[1].y1=310; H[1].x2=35; H[1].y2=310; H[1].x3=20; H[1].y3=330; H[2].x1=5; H[2].y1=270; H[2].x2=35; H[2].y2=270; H[2].x3=20; H[2].y3=250; W[1].x1=310; W[1].y1=10; W[1].x2=310; W[1].y2=40; W[1].x3=330; W[1].y3=25; W[2].x1=270; W[2].y1=10; W[2].x2=270; W[2].y2=40; W[2].x3=250; W[2].y3=25; W[3].x1=560; W[3].y1=560; W[3].x2=580; W[3].y2=580; W[3].x3=560; W[3].y3=580; HEIGHT=55; WIDTH=89; HEIGHT2=55; WIDTH2=89; } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int count=0; g.setColor(Color.white); g.drawRect(48,48,498,498); g.setFont(new Font("Helvetica",Font.PLAIN,25)); g.setColor(Color.yellow); g.drawString("Rich: Applet 27",410,30); g.setFont(new Font("Helvetica",Font.PLAIN,12)); g.setColor(Color.white); g.drawString("To change the dimensions of the box, click inside",10,570); g.drawString("the big white square, or use the magenta arrow keys",10,590); g.drawString("Click on the yellow triangle to do",340,570); g.drawString("one step of the Euclidean Algorithm.",340,590); HEIGHT=HEIGHT2; WIDTH=WIDTH2; /*numbers*/ g.setFont(new Font("Helvetica",Font.PLAIN,25)); int h1,h2,w1,w2; h1=HEIGHT/10; h2=HEIGHT-10*(HEIGHT/10); w1=WIDTH/10; w2=WIDTH-10*(WIDTH/10); char ch1=(char)(h1+48); Character CH1=new Character(ch1); g.drawString(CH1.toString(),5,300); char ch2=(char)(h2+48); Character CH2=new Character(ch2); g.drawString(CH2.toString(),23,300); char cw1=(char)(w1+48); Character CW1=new Character(cw1); g.drawString(CW1.toString(),275,35); char cw2=(char)(w2+48); Character CW2=new Character(cw2); g.drawString(CW2.toString(),293,35); W[3].render(g,Color.yellow); for(int i=1;i<=2;++i) { H[i].render(g,Color.magenta); W[i].render(g,Color.magenta); } /************/ g.translate(50,50); g.setColor(COL[0]); g.fillRect(0,0,5*WIDTH,5*HEIGHT); g.setColor(Color.white); g.drawRect(0,0,5*WIDTH,5*HEIGHT); while(WIDTH>0) { if(WIDTH>HEIGHT) ++count; g.setColor(COL[count]); while(HEIGHT1)) { this.HEIGHT2=HEIGHT2-1; repaint(); } if((w==1)&&(WIDTH2<99)) { this.WIDTH2=WIDTH2+1; repaint(); } if((w==2)&&(WIDTH2>1)) { this.WIDTH2=WIDTH2-1; repaint(); } if((p.x>55)&&(p.x<549)&&(p.y>55)&&(p.y<549)) { WIDTH2=(p.x-50)/5; HEIGHT2=(p.y-50)/5; repaint(); } int history=0; if((w==3)&&(WIDTH20) HEIGHT2=HEIGHT2-q*WIDTH2; history=1; repaint(); } if((w==3)&&(WIDTH2>HEIGHT2)&&(history==0)) { int q=WIDTH2/HEIGHT2; if(WIDTH2-q*HEIGHT2>0) WIDTH2=-q*HEIGHT2+WIDTH2; history=1; repaint(); } } } class ListenTriangle { double x1,x2,x3; double y1,y2,y3; Color C; ListenTriangle() { this.x1=x1; this.x2=x2; this.x3=x3; this.y1=y1; this.y2=y2; this.y3=y3; this.C=C; } int inside(Point p) { double v1x,v1y,v2x,v2y; double A1,A2,A3; int val; v1x=p.x-this.x1; v1y=p.y-this.y1; v2x=p.x-this.x2; v2y=p.y-this.y2; A3=v1x*v2y-v1y*v2x; v1x=p.x-this.x2; v1y=p.y-this.y2; v2x=p.x-this.x3; v2y=p.y-this.y3; A1=v1x*v2y-v1y*v2x; v1x=p.x-this.x3; v1y=p.y-this.y3; v2x=p.x-this.x1; v2y=p.y-this.y1; A2=v1x*v2y-v1y*v2x; val=0; if((A1<0)&&(A2<0)&&(A3<0)) val=1; if((A1>0)&&(A2>0)&&(A3>0)) val=1; return(val); } void render(Graphics g,Color C) { g.setColor(C); Polygon P=new Polygon(); int x[]={(int)(this.x1),(int)(this.x2),(int)(this.x3)}; int y[]={(int)(this.y1),(int)(this.y2),(int)(this.y3)}; P.xpoints=x; P.ypoints=y; P.npoints=3; g.fillPolygon(P); g.setColor(Color.white); g.drawPolygon(P); } }