import java.applet.Applet; import java.awt.*; /*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; public void init() { A=new Animator(); X=new MotionCanvas(); A=A.addMotionCanvas(X); setBackground(Color.black); X.setBackground(Color.black); X.resize(100,100); add(X); try {A.start();} catch(Exception e) {} } 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: ",10,140); g.drawString("Applet 35 ",10,160); } } 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 MotionCanvas extends DBCanvas { Penny[] P=new Penny[30]; int count; MotionCanvas() { count=5; P[4]=new Penny(2,50,50,40,40,.12,0,Color.red,Color.green); P[2]=new Penny(1,80,15,30,30,.18,0,Color.green,Color.yellow); P[3]=new Penny(1,20,70,40,40,.16,0,Color.blue,Color.yellow); P[1]=new Penny(2,80,70,45,45,.14,0,Color.orange,Color.cyan); P[5]=new Penny(1,15,10,40,40,.10,0,Color.pink,Color.magenta); } public void paint(Graphics g) { for(int j=1;j<=count;++j) P[j].render(g); } } 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); g.setColor(C1); if(temp<0) { temp=-temp; g.setColor(C2); } if(type==1) { 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) { 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); } } void evolve() { CLOCK=CLOCK+SPEED; } } 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(20);} catch(Exception e) {} } } Animator addMotionCanvas(MotionCanvas X) { Animator B=this; B.X=X; return(B); } }