import java.applet.Applet; import java.awt.*; import java.awt.event.*; /*This Applet just prints some words on a black background. It contains the two basic methods which are typically in a graphics applet. The init() method is mandatory. The paint() method is what draws the picture. */ public class test1 extends Applet { MotionCanvas X; Animator A; ControlCanvas C; public void init() { A=new Animator(); X=new MotionCanvas(); A=A.addMotionCanvas(X); C=new ControlCanvas(X,A); C.resize(200,100); setBackground(Color.black); X.setBackground(Color.black); X.resize(150,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 ControlCanvas extends Canvas implements MouseListener { MotionCanvas X; Animator A; ListenSquare[] L=new ListenSquare[10]; ControlCanvas(MotionCanvas X,Animator A) { this.X=X; this.A=A; addMouseListener(this); for(int i=1;i<=6;++i) { L[i]=new ListenSquare(30+18*i,60,10,10); } L[7]=new ListenSquare(48,80,100,10); } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setFont(new Font("Helvetica",Font.PLAIN,20)); g.setColor(Color.yellow); g.drawString("Rich: Applet 36 ",30,20); for(int i=1;i<=7;++i) { L[i].render(g,Color.blue,Color.yellow); } g.setColor(Color.red); int temp=2*(X.sleep+15); g.fillRect(temp,81,3,9); } 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(); p.x=e.getX(); p.y=e.getY(); int test=0; for(int i=1;i<=7;++i) { if(L[i].inside(p)==1) test=i; } X.reconstruct(test); if(test==7) { X.sleep=p.x/2-15; repaint(); } } public void mouseExited(MouseEvent e) {} } class MotionCanvas extends DBCanvas { Penny[] P=new Penny[50]; int count; int sleep; MotionCanvas() { sleep=30; count=17; for(int k=1;k<=17;++k) { double kk=k; P[k]=new Penny(1,110-7*k,30+7*k,130-4*k,130-4*k,kk/500+.1,0,Color.blue,Color.yellow); } } public void paint(Graphics gfx) { Graphics2D g=(Graphics2D) gfx; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for(int j=1;j<=count;++j) P[j].render(g); } void reconstruct (int j) { if(j==1) { count=17; for(int k=1;k<=17;++k) { double kk=k; P[k]=new Penny(1,110-7*k,30+7*k,130-4*k,130-4*k,kk/500+.1,0,Color.blue,Color.yellow); } } if(j==2) { count=17; for(int k=1;k<=17;++k) { double kk=k; P[k]=new Penny(1,75,75,110-6*k,110-6*k,kk/500+.1,0,Color.blue,Color.yellow); } } if(j==3) { count=20; for(int k=1;k<=count;++k) { double kk=k; P[k]=new Penny(2,50+10*k,75,100,100,.1+kk/500,0,Color.yellow,Color.blue); } } if(j==4) { count=40; for(int k=1;k<=count/2;++k) { double kk=k; P[2*k-1]=new Penny(2,25+10*k,75,75,75,.1+kk/500,0,Color.yellow,Color.blue); P[2*k]=new Penny(1,75,25+10*k,75,75,.1+kk/500,0,Color.red,Color.blue); } } if(j==5) { count=25; for(int k=1;k<=count;++k) { double kk=k; int test=k-3*(k/3); Color c=Color.red; if(test==1) c=Color.red; if(test==2) c=Color.yellow; if(test==0) c=Color.blue; P[k]=new Penny(3,75,75,130-5*k,130-5*k,.1+kk/500,0,c,c); } } if(j==6) { count=25; for(int k=1;k<=count;++k) { double kk=k; int test=k-3*(k/3); Color c=Color.red; if(test==1) c=Color.red; if(test==2) c=Color.yellow; if(test==0) c=Color.blue; P[k]=new Penny(3,15+5*k,15+5*k,130-5*k,130 -5*k,.1+kk/500,0,c,c); } } } } class Penny { double WIDTH,HEIGHT,SPEED,CLOCK; int x,y; int type; Color C1,C2; Penny(int t,int x,int y,double w,double h,double s,double c,Color c1,Color c2) { this.WIDTH=w; this.HEIGHT=h; this.SPEED=s; this.CLOCK=c; this.C1=c1; this.C2=c2; this.x=x; this.y=y; this.type=t; } void render(Graphics g) { double temp=Math.cos(CLOCK); double temp2=Math.sin(CLOCK); if(type==1) { g.setColor(C1); if(temp<0) { temp=-temp; g.setColor(C2); } int w=(int)(WIDTH*temp); int h=(int)(HEIGHT); g.fillOval(x-w,y-h,2*w,2*h); g.setColor(Color.black); g.drawOval(x-w,y-h,2*w,2*h); } if(type==2) { g.setColor(C1); if(temp<0) { temp=-temp; g.setColor(C2); } int w=(int)(WIDTH); int h=(int)(HEIGHT*temp); g.fillOval(x-w,y-h,2*w,2*h); g.setColor(Color.black); g.drawOval(x-w,y-h,2*w,2*h); } if(type==3) { g.setColor(C1); int w=(int)(WIDTH); int h=(int)(HEIGHT); int xx=(int)(x+WIDTH*temp); int yy=(int)(x+HEIGHT*temp2); g.fillOval(xx-w,yy-h,2*w,2*h); g.setColor(Color.black); g.drawOval(xx-w,yy-h,2*w,2*h); } } void evolve() { CLOCK=CLOCK+SPEED; if(CLOCK>2*Math.PI) CLOCK=CLOCK-2*Math.PI; } } class Animator extends Thread { MotionCanvas X; public void run() { while(true) { for(int j=1;j<=X.count;++j) X.P[j].evolve(); X.repaint(); try {sleep(X.sleep);} catch(Exception e) {} } } Animator addMotionCanvas(MotionCanvas X) { Animator B=this; B.X=X; return(B); } } class ListenSquare { double x1,x2; double y1,y2; ListenSquare(double x1,double y1,double x2,double y2) { this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; } 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) { 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); } }