// Random Graphics generator
// by J. W. Rider, 991026

import java.applet.Applet;
import java.awt.*;

public class RandomGraphic
   extends Applet
   implements Runnable
{	Thread thread;
	Graphics gr;

	float colorPersist = 0;

	// override Applet methods init(), start(), stop(), destroy()

	public void init()
	{
	   if (thread == null)
	   {
		thread = new Thread(this);
		thread.start();
		thread.suspend();
	   }
	}

	public void start()
	{
	   if(thread!=null) thread.resume();
	}

	public void stop()
	{
	  if(thread!=null) thread.suspend();
	}

	public void destroy()
	{
	  if (thread!=null)
	  { thread.stop(); thread=null; }
	}

	// override Component method paint()

	public void paint(Graphics g)
	{
	   if (gr!=null) gr.dispose();
	   gr = /*g.create()*/ getGraphics();
	}

	// create Runnable method run()

	public void run()
	{  double r=Math.random();
	   for (;;){
  	      if(gr!=null)
              {
		if (Math.random()<0.4) setRandomColor();
                r=Math.random();
		if (r<0.05) fillRandomRect();
                else if (r<0.1) drawRandomRect();
		else if (r<0.15) fillRandom3DRect();
    else if (r<0.2) drawRandom3DRect();
    else if (r<0.25) clearRandomRect();
    else if (r<0.275) fillRandomPolygon();
		else if (r<0.3) ;
		else if (r<0.35) fillRandomRoundRect();
		else if (r<0.375) fillRandomOval();
		else if (r<0.4) ;
		else if (r<0.425) drawRandomArc();
		else if (r<0.45) ;
		else if (r<0.475) fillRandomArc();
		else if (r<0.5) ;
		else if (r<0.525) copyRandomArea();
		else if (r<0.55) ;
		else if (r<0.575) drawRandomOval();
		else if (r<0.6) ;
		else if (r<0.65) drawRandomRoundRect();
		else if (r<0.675) drawRandomPolygon();
		else if (r<0.725) drawGradientBlock();
		else if (r<0.775)  drawGradientBlock2();
		else if (r<0.825) drawBoxCircle();
    else if (r<0.875) drawCircleBox();
    else if (r<0.9) drawBoxLine();
		else drawStickBlock();
	      }
	      try {thread.sleep(100);} catch(Exception e) {}
           }
	}

	void setRandomColor()
	{
	   if (Math.random()<0.02) gr.setXORMode(gr.getColor());
           else if (Math.random()<0.1) gr.setPaintMode();
	   if (Math.random()<0.2) gr.setColor(randomColor(gr.getColor()));
	}

	void drawBoxCircle()
	{
	   Rectangle r=new Rectangle(size());
	   int n=(int)(Math.round(Math.random()*20)+2);
           long r2 = ((n*n+1)/2+n)/2;
	   long c2 = ((n*n+1)/2-n)/2;
	   long m2 = (Math.random()<0.5) ? (Math.random()<0.5) ? (Math.round(Math.random()*c2)) : c2 : 0;
	   r=randomRect(r);
           for (int i=0; i < n; i++)
               for (int j=0; j < n; j++)
               {
                 long i2=i*i-i*(n-1)+c2;
	 	 long j2=j*j-j*(n-1)+c2;
                 if (i2+j2<=r2 && i2+j2>=m2)
                 {
		    if(m2 !=0 || Math.random()<0.5)
                       gr.fill3DRect(r.x+i*r.width/n,
 				r.y+j*r.height/n,
                             r.width/n, r.height/n,Math.random()<0.5);
                    try{thread.sleep(10);} catch(Exception e){}
 		 }
               }
	}

	void drawBoxLine()
	{
	   Rectangle r=new Rectangle(size());
	   int n=(int)(Math.round(Math.random()*20)+2);
	   r=randomRect(r);
           if (Math.random()<0.5)
              for (int i=0; i < n; i++)
              {
                 gr.fill3DRect(r.x+i*r.width/n, r.y+i*r.height/n,
                             r.width/n, r.height/n, Math.random()<0.5);
                 try{thread.sleep(10);} catch(Exception e){}
              }
	   else
              for (int i=0; i < n; i++)
              {
                 gr.fill3DRect(r.x+(n-i-1)*r.width/n, r.y+i*r.height/n,
                             r.width/n, r.height/n, Math.random()<0.5);
                 try{thread.sleep(10);} catch(Exception e){}
              }
	}

	void drawCircleBox()
	{
	   Rectangle r=new Rectangle(size());
	   Color oldc = gr.getColor();
	   Color newc = randomColor(oldc);
	   int n=(int)(Math.round(Math.random()*20)+2);
	   r=randomRect(r);
           for (int i=0; i < n; i++)
               for (int j=0; j < n; j++)
               {  if (Math.random()<0.5)
		  {
                    gr.fillOval(r.x+i*r.width/n, r.y+j*r.height/n,
                             r.width/n, r.height/n);
		    if (Math.random()<0.5)
  		    {
		      gr.setColor(newc);
		      gr.drawOval(r.x+i*r.width/n, r.y+j*r.height/n,
                             r.width/n, r.height/n);
		      gr.setColor(oldc);
		    }
		  }
		  else if (Math.random()<0.5)
		  {
		    gr.setColor(newc);
		    gr.drawOval(r.x+i*r.width/n, r.y+j*r.height/n,
                             r.width/n, r.height/n);
		    gr.setColor(oldc);
		  }
                  try {thread.sleep(10);} catch (Exception e) {}
               }
	}

	void drawStickBlock()
	{
	   Rectangle r=new Rectangle(size());
	   int n=(int)(Math.round(Math.random()*20)+2);
	   r=randomRect(r);
           if (Math.random()<0.5)
              for (int i=0; i < n; i++)
              {
                 gr.drawLine(r.x+i*r.width/n, r.y,
                             r.x+i*r.width/n, r.y+r.height);
                 try{thread.sleep(10);} catch(Exception e){}
              }
	   else
              for (int i=0; i < n; i++)
              {
                 gr.drawLine(r.x, r.y+i*r.height/n,
                             r.x+r.width, r.y+i*r.height/n);
                 try{thread.sleep(10);} catch(Exception e){}
              }
	}

	void drawGradientBlock()
	{
	   Rectangle r=new Rectangle(size());
	   int n=(int)(Math.round(Math.random()*20)+2);
		 Color oldc = gr.getColor();
		 Color newc = randomColor(oldc);
	   r=randomRect(r);
           if (Math.random()<0.5)
              for (int i=0; i < r.width; i++)
              {
		 if ( Math.random()<(float)i/r.width )
		    gr.setColor(newc);
 		 else gr.setColor(oldc);
                 if (Math.random()<0.5) gr.drawLine(r.x+i, r.y,
                                                    r.x+i, r.y+r.height);
                 try{thread.sleep(4);} catch(Exception e){}
              }
	   else
              for (int i=0; i < r.height; i++)
              {
		 if ( Math.random()<(float)i/r.height )
		    gr.setColor(newc);
 		 else gr.setColor(oldc);
                 if (Math.random()<0.5) gr.drawLine(r.x, r.y+i,
                                                    r.x+r.width, r.y+i);
                 try{thread.sleep(4);} catch(Exception e){}
              }
		 gr.setColor(oldc);
	}

	void drawGradientBlock2()
	{
	   Rectangle r=new Rectangle(size());
	   int n=(int)(Math.round(Math.random()*20)+2);
		 Color oldc = gr.getColor();
		 Color newc = randomColor(oldc);
		 Color curc;

	   r=randomRect(r);
     if (Math.random()<0.5)
        for (int i=0; i < r.width; i++)
        {
          gr.setColor( new Color(
			 	((r.width-i)*oldc.getRed()+i*newc.getRed())/r.width,
				((r.width-i)*oldc.getGreen()+i*newc.getGreen())/r.width,
				((r.width-i)*oldc.getBlue()+i*newc.getBlue())/r.width));
          gr.drawLine(r.x+i, r.y,
                      r.x+i, r.y+r.height);
          try{thread.sleep(4);} catch(Exception e){}
        }
     else
        for (int i=0; i < r.height; i++)
        {
            gr.setColor( new Color(
			 	((r.height-i)*oldc.getRed()+i*newc.getRed())/r.height,
				((r.height-i)*oldc.getGreen()+i*newc.getGreen())/r.height,
				((r.height-i)*oldc.getBlue()+i*newc.getBlue())/r.height));
            gr.drawLine(r.x, r.y+i,
                        r.x+r.width, r.y+i);
            try{thread.sleep(4);} catch(Exception e){}
        }
	gr.setColor(oldc);
	}

	// Random Graphics routines

	void clearRandomRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.clearRect(r.x,r.y,r.width,r.height);
	}

	void copyRandomArea()
	{
	   Rectangle r=new Rectangle(size());
	   int dx = (int)(Math.round(r.width*Math.random())+r.x);
	   int dy = (int)(Math.round(r.height*Math.random())+r.y);
	   r=randomRect(r);
           int n = (int)(Math.round(Math.random()*20)+2);
           for (int i=0; i < n; i++)
           {
	     gr.copyArea(r.x+i*dx/n, r.y+i*dy/n,
                         r.width, r.height, dx-(n-i-1)*r.x/n, dy-(n-i-1)*r.y/n);
           }
	}

	void drawRandomArc()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.drawArc( r.x, r.y, r.width, r.height,
		(int)(Math.random()*360), (int)(Math.random()*360));
	}

	void fillRandomArc()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.fillArc( r.x, r.y, r.width, r.height,
			(int)(Math.random()*360), (int)(Math.random()*360));
	}

	void drawRandomOval()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.drawOval(r.x,r.y,r.width,r.height);
	}

	void fillRandomOval()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.fillOval(r.x,r.y,r.width,r.height);
	   if (Math.random()<0.5)
	   {
		Color oldc = gr.getColor();
		Color newc = randomColor(oldc);
		gr.setColor(newc);
		gr.drawOval(r.x,r.y,r.width,r.height);
	        gr.setColor(oldc);
	   }
	}

	void drawRandomPolygon()
	{
	   Rectangle r=new Rectangle(size());
           int nPoints = (int)(Math.round(20*Math.random())+3);
	   int x[]=new int[nPoints];
	   int y[]=new int[nPoints];
	   randomPolygon(r,x,y);
	   gr.drawPolygon(x,y,nPoints);
	}

	void fillRandomPolygon()
	{
	   Rectangle r=new Rectangle(size());
           int nPoints = (int)(Math.round(20*Math.random())+3);
	   int x[]=new int[nPoints];
	   int y[]=new int[nPoints];
	   randomPolygon(r,x,y);
	   gr.fillPolygon(x,y,nPoints);
	   if (Math.random()<0.5)
	   {
		Color oldc = gr.getColor();
		Color newc = randomColor(oldc);
		gr.setColor(newc);
		gr.drawPolygon(x,y,nPoints);
	        gr.setColor(oldc);
	   }
	}

	void drawRandomRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.drawRect(r.x,r.y,r.width,r.height);
	}

	void fillRandomRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.fillRect(r.x,r.y,r.width,r.height);
	   if (Math.random()<0.5)
	   {
		Color oldc = gr.getColor();
		Color newc = randomColor(oldc);
		gr.setColor(newc);
		gr.drawRect(r.x,r.y,r.width,r.height);
	        gr.setColor(oldc);
	   }
	}

	void drawRandom3DRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.draw3DRect(r.x,r.y,r.width,r.height,Math.random()<0.5);
	}

	void fillRandom3DRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.fill3DRect(r.x,r.y,r.width,r.height,Math.random()<0.5);
	}

	void drawRandomRoundRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.drawRoundRect(r.x,r.y,r.width,r.height,10,10);
	}

	void fillRandomRoundRect()
	{
	   Rectangle r=new Rectangle(size());
	   r=randomRect(r);
	   gr.fillRoundRect(r.x,r.y,r.width,r.height,10,10);
	   if (Math.random()<0.5)
	   {
		Color oldc = gr.getColor();
		Color newc = randomColor(oldc);
		gr.setColor(newc);
		gr.drawRoundRect(r.x,r.y,r.width,r.height,10,10);
	        gr.setColor(oldc);
	   }
	}


	// Random objects

	Color randomColor(Color c)
	{
	   return new Color(
		(float)(Math.random()+colorPersist*c.getRed()/255.0)/(1+colorPersist),
		(float)(Math.random()+colorPersist*c.getGreen()/255.0)/(1+colorPersist),
		(float)(Math.random()+colorPersist*c.getBlue()/255.0)/(1+colorPersist)
                           );
	}

	void randomPolygon(Rectangle r,int x[],int y[])
	{
	   for (int i=0; i < x.length; i++)
		x[i]=(int)(Math.round(r.width*(1.6*Math.random()-0.3)+r.x));
	   for (int i=0; i < y.length; i++)
		y[i]=(int)(Math.round(r.height*(1.6*Math.random()-0.3)+r.y));
	}

	Rectangle randomRect(Rectangle r)
	{
	  long lx,ux,ly,uy,t;
          do{
	  lx = Math.round(r.width*(1.5*Math.random()-0.5)+r.x);
	  ux = Math.round(r.width*(1.5*Math.random())+r.x);
	  if (lx > ux) {t=lx; lx=ux; ux=t;}
	  ly=Math.round(r.height*(1.5*Math.random()-0.5)+r.y);
	  uy=Math.round(r.height*1.5*Math.random()+r.y);
	  if (ly > uy) {t=ly; ly=uy; uy=t;}
	  }while ((lx < r.x || ly < r.y) && (lx > r.x+r.width || ly > r.y+r.height));
	  return new Rectangle((int)(lx),(int)(ly),(int)(ux-lx),(int)(uy-ly));
	}


}


1