/* 
	Simple.java

	java basics, 15-19.11.1999, juhuu@katastro.fi
*/

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.lang.Math;

public class Maeda extends Applet implements Runnable {
  Image		imageDoubleBuffer;
  Graphics	gDoubleBuffer;
  Thread	engine = null;

  int		x, y;
  int		mousex, mousey;
  int 		factor;
  boolean 	mouse_down, b;
  int		n_red;
  int		n_green;
  int		n_blue;
  int		n_color;
  
  /*
  Image 	imgPic;
  Image 	imgBackground;
	*/

  public void init() {
      // initialize double buffering
      imageDoubleBuffer = createImage(size().width, size().height);
      gDoubleBuffer = imageDoubleBuffer.getGraphics();
      gDoubleBuffer.setColor(Color.black);
      gDoubleBuffer.fillRect(0, 0, size().width, size().height);

      mousex 	= 0;
      mousey 	= 0;

      x 	= 0;
      y 	= 0;

      mouse_down = false;

      // let's load an image

/*
      imgPic = null;

     	String sPic = "punk.gif";
        String sBackground = "back.gif";

      MediaTracker med = new MediaTracker(this);

      imgPic = getImage(getDocumentBase(), sPic);
      med.addImage(imgPic, 0);

      imgBackground = getImage(getDocumentBase(), sBackground);
      med.addImage(imgBackground, 1);

      try
      {
  	  med.waitForAll();
      }	catch (InterruptedException e) {
   
     
      }
 
 */
 
  }

//----------------------------------------------------

  public void paint(Graphics g) {

	// clear the screen
//    	gDoubleBuffer.setColor(Color.white);
//    	gDoubleBuffer.fillRect(0, 0, size().width, size().height);

	
			
	
			int scr_width = size().width;
			int scr_height = size().height;

			double dx = x;
			double dy = y;

			dx = dx / 8;
			dy = dy / 8;


if (mouse_down == false) {
	/*gDoubleBuffer.setColor(Color.black);
			
			gDoubleBuffer.fillOval(mousex, mousey , 100,100);
				dx = dx + 5 + factor;
				dy = dy + 10 + factor;
			

			x = x + 1+ factor;
			y = y + 1 + factor;*/
			
			for (double i = 0 ; i <= 50 ; i++)
			
			{
			
			n_red = (int)(Math.random()*256);
			n_green = (int)(Math.random()*256);
			n_blue = (int)(Math.random()*256);
			


Color n_color = new Color (n_red, factor, n_blue);
		
		
				gDoubleBuffer.setColor(n_color);
			

				gDoubleBuffer.drawRect((int) (100 * Math.sin(mousex / (2 * Math.PI))) + scr_width / 2, (int) (100 * Math.sin(mousey / (2 * Math.PI)))   + scr_height / 2, 
				mousex / 2, mousey / 2);
				
								gDoubleBuffer.drawRect((int) (100 * Math.sin(mousex / (4 * Math.PI))) + scr_width / 2, (int) (100 * Math.sin(mousey / (4 * Math.PI)))   + scr_height / 2, 
				mousex+10 /2, mousey+10 / 2);


				dx = dx + 5 + factor;
				dy = dy + 10 + factor;
			

			x = x + 2;
			y = y + 4;
			
			}
			
}



if (mouse_down == true){


			for (double i = 0 ; i <= 50 ; i++)
			
			{
				n_red = (int)(Math.random()*256);
			n_green = (int)(Math.random()*256);
			n_blue = (int)(Math.random()*256);
			
			
		
			
			if (b==true){
			
			
			Color n_color = new Color (n_red, factor, n_blue);
		
		
				gDoubleBuffer.setColor(n_color);
			
				gDoubleBuffer.drawLine(mousex, mousey, (int) (100 * Math.sin(dx / (2 * Math.PI)))  + scr_width / 2, (int) (100 * Math.sin(dy / (2 * Math.PI)))   + scr_height / 2);
				dx = dx + 5 + factor;
				dy = dy + 10 + factor;
			

			x = x + 1;
			y = y + 1;
	

}

else{
		Color n_color = new Color (factor, n_green, n_blue);
		
		gDoubleBuffer.setColor(n_color);
			
				gDoubleBuffer.drawLine((int) (100 * Math.sin(dx / (2 * Math.PI)))  + scr_width / 2, (int) (100 * Math.sin(dy / (2 * Math.PI)))  + scr_height / 2, mousex, mousey);
				dx = dx + 5 + factor;
				dy = dy + 10 + factor;
			

			x = x + 10;
			y = y + 10;
		
		
		}

}

}
	// draw all the stuff from the double buffer to the screen
	g.drawImage(imageDoubleBuffer, 0, 0, null);				
  }


//----------------------------------------------------


  // start the applet
  public void start() {
      if (engine == null) {
          engine = new Thread(this);
          engine.start();
      }
      showStatus(getAppletInfo());
  } 

  // stop the applet
  public void stop() {
     if (engine != null && engine.isAlive())
     {
         engine.stop();
     } 
     engine = null; 
  }

  public void run()
  {                  
     while (true)
     {
         try
         {
             repaint();
             Thread.sleep(10);
         } catch (InterruptedException e) {
             stop();
         }
     }                                  
  }

  public void update(Graphics g) {    
	paint(g);
  }

  // print to the console & browserstatus
  private void print(String s) {
    System.out.println(s); 		// console
    showStatus(s);         		// browser status  
  }

//---

  public boolean mouseDown(Event e, int x, int y) {
	
	return true;  
  }

  public boolean mouseUp(Event e, int x, int y) {
	mouse_down = false;
	
	if (factor<255){
	factor = factor+10;
	}
	
	else
	
	{
	
	factor = 0;
	}
	if (b == true) {
	b = false;
	}
	else
	{
	b=true;
	}
	
	;
	return true;  
  }

  public boolean mouseMove(Event e, int x, int y)
  {
	mousex = x;
	mousey = y;
	return true;  
  }

  public boolean mouseDrag(Event e, int x, int y)
  {
  mouse_down = true;
	mousex = x;
	mousey = y;
	return true;  
  }


//---

}
