import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class test1 extends Applet { MotionCanvas M; ControlCanvas C; Animator A; public void init() { setBackground(Color.blue); M=new MotionCanvas(); C=new ControlCanvas(); A=new Animator(); M.setBackground(Color.black); C.setBackground(new Color(50,50,150)); C=C.addMotionCanvas(M); C=C.addAnimator(A); M=M.addAnimator(A); A=A.addMotionCanvas(M); M.setSize(300,300); C.setSize(155,300); add(C); add(M); 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 MotionCanvas extends DBCanvas implements MouseListener,MouseMotionListener { ListenSquare[][] Q=new ListenSquare[30][30]; Particle[] P=new Particle[6]; Animator A; int count; MotionCanvas() { addMouseListener(this); addMouseMotionListener(this); P[1]=new Particle(265.0,265.0,4.0,Color.red); P[1].I=26; P[1].J=26; P[2]=new Particle(45.0,45.0,4.0,Color.blue); P[2].I=4; P[2].J=4; P[3]=new Particle(45.0,265.0,4.0,Color.green.darker()); P[3].I=4; P[3].J=26; P[4]=new Particle(265.0,45.0,4.0,Color.yellow ); P[4].I=26; P[4].J=4; count=4; double ii,jj; for(int i=0;i<30;++i) { for(int j=0;j<30;++j) { ii=i; jj=j; ii=10*ii+5.0; jj=10*jj+5.0; Q[i][j]=new ListenSquare(ii,jj,5.0,0,Color.white); } } } MotionCanvas addAnimator(Animator A) { MotionCanvas Y=this; Y.A=A; return(Y); } public void paint(Graphics g) { for(int i=1;i<=28;++i) { for(int j=1;j<=28;++j) { Q[i][j].render(g); } } for(int i=1;i<=count;++i) P[i].render(g); } public void mouseMoved(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) { e.consume(); Point p=new Point(); ListenSquare QQ; p.x=e.getX(); p.y=e.getY(); p.x=(p.x/10); p.y=(p.y/10); if((p.x>0)&&(p.x<29)&&(p.y>0)&&(p.y<29)) { QQ=Q[p.x][p.y]; if((QQ.history==0)&&(QQ.on!=30)) { QQ.on=1-QQ.on; QQ.history=1; QQ.n=Q[p.x][p.y-1].on; QQ.e=Q[p.x+1][p.y].on; QQ.w=Q[p.x-1][p.y].on; QQ.s=Q[p.x][p.y+1].on; QQ.s=Q[p.x][p.y+1].on; Q[p.x-1][p.y].e=1-Q[p.x-1][p.y].e; Q[p.x+1][p.y].w=1-Q[p.x+1][p.y].w; Q[p.x][p.y-1].s=1-Q[p.x][p.y-1].s; Q[p.x][p.y+1].n=1-Q[p.x][p.y+1].n; Q[p.x-1][p.y].history=0; Q[p.x+1][p.y].history=0; Q[p.x][p.y-1].history=0; Q[p.x][p.y+1].history=0; repaint(); Q[p.x][p.y]=QQ; } } } public void mouseClicked(MouseEvent e) { e.consume(); Point p=new Point(); ListenSquare QQ; p.x=e.getX(); p.y=e.getY(); p.x=(p.x/10); p.y=(p.y/10); if((p.x>0)&&(p.x<29)&&(p.y>0)&&(p.y<29)) { QQ=Q[p.x][p.y]; QQ.on=1-QQ.on; QQ.n=Q[p.x][p.y-1].on; QQ.e=Q[p.x+1][p.y].on; QQ.w=Q[p.x-1][p.y].on; QQ.s=Q[p.x][p.y+1].on; Q[p.x-1][p.y].e=1-Q[p.x-1][p.y].e; Q[p.x+1][p.y].w=1-Q[p.x+1][p.y].w; Q[p.x][p.y-1].s=1-Q[p.x][p.y-1].s; Q[p.x][p.y+1].n=1-Q[p.x][p.y+1].n; Q[p.x][p.y]=QQ; repaint(); } } void reset () { for(int i=0;i<=29;++i) { for(int j=0;j<=29;++j) { Q[i][j].on=0; Q[i][j].n=0; Q[i][j].s=0; Q[i][j].e=0; Q[i][j].w=0; Q[i][j].C=Color.white; } P[1]=new Particle(265.0,265.0,4.0,Color.red); P[1].I=26; P[1].J=26; P[2]=new Particle(45.0,45.0,4.0,Color.blue); P[2].I=4; P[2].J=4; P[3]=new Particle(45.0,265.0,4.0,Color.green.darker()); P[3].I=4; P[3].J=26; P[4]=new Particle(265.0,45.0,4.0,Color.yellow ); P[4].I=26; P[4].J=4; } } void recolor (Color C) { for(int i=1;i<=28;++i) { for(int j=1;j<=28;++j) { Q[i][j].C=C; } } } } class VSlider { int x,y,w,h; int pos; Color[] C; String S; VSlider(int x,int y,int w,int h,int pos,Color[] C,String S) { this.x=x; this.y=y; this.h=h; this.w=w; this.pos=pos; this.C=C; this.S=S; } void render(Graphics g) { g.setColor(C[1]); g.fillRect(x,y,w,h); g.setColor(C[2]); g.drawRect(x,y,w,h); g.setColor(C[3]); g.drawRect(x+1,pos-1,w-2,2); g.setColor(C[2]); g.setFont(new Font("Helvetica",Font.PLAIN,11)); g.drawString(S,x,y+h+12); } int inside(Point p) { int test=0; if((p.x>x)&&(p.xy)&&(p.yx-r)&&(p.xy-r)&&(p.y=2) { if(dir==1) Q[P.I-2][P.J].C=P.C; if(dir==2) Q[P.I][P.J-2].C=P.C; if(dir==3) Q[P.I+2][P.J].C=P.C; if(dir==4) Q[P.I][P.J+2].C=P.C; } if(count==3) { if(dir==1) Q[P.I-1][P.J+1].C=P.C; if(dir==2) Q[P.I-1][P.J-1].C=P.C; if(dir==3) Q[P.I+1][P.J-1].C=P.C; if(dir==4) Q[P.I+1][P.J+1].C=P.C; } } P.d=dir; if(dirr>0) P.d=dirr; return(P); } } class Animator extends Thread { MotionCanvas M; long speed; public void run() { speed=50; while(true) { for(int j=1;j<=M.count;++j) { M.P[j]=M.P[j].update(M.Q,this); } M.repaint(); try {sleep(2*speed);} catch(Exception e) {} } } Animator addMotionCanvas(MotionCanvas M) { Animator B=this; B.M=M; return(B); } }