

import java.util.*;
import java.awt.*;
import java.applet.*;


public class MathArt06 extends Applet{
	
	int w, h;
	private Image offImage;
   	private Dimension offDimension;
	private Graphics offGraphics;

	int posx=1;
	int posy=1;
	Color c = new Color(0,0,0);

	public void init() {
 
		Dimension d = this.size(); 

		w = d.width;
		h = d.height;

		posx = w/2;
		posy = h/2;
		
		if ((offImage == null) || (w != offDimension.width) || (h != offDimension.height)) {
			offImage = createImage(w, h);
			offDimension = d;
			offGraphics = offImage.getGraphics();
		}
		color(posx,posy,w,4);
		repaint();

	}
	
	
	public void update(Graphics g) {
		paint(g);
	}
	
	public void paint(Graphics g) {	
		g.drawImage(offImage, 0, 0, this);
	}

	public void color(int x, int y, int r, int s) {
	
		int q = (int)(((double)posx+(double)posy)/(double)2);


		for (int i=x-r/2; i<x+r/2; i=i+s) {
			for (int j=y-r/2; j<y+r/2; j=j+s) {		
				
				
		double iq = (double)(((double)i+(double)q)/(double)40);
		double jq = (double)(((double)j+(double)q)/(double)40);
		double ijq = (double)(((double)i+(double)j+(double)q)/(double)60);

				int red   = (int) ((Math.sin(iq)+Math.cos(ijq))*(Math.sin(iq)+Math.cos(ijq))/(double)4*(double)255)  ;				
				int green = (int) ((Math.sin(jq)+Math.cos(iq)) *(Math.sin(jq)+Math.cos(jq)) /(double)4*(double)255) ;
				int blue  = (int) ((Math.sin(ijq)+Math.cos(jq))*(Math.sin(ijq)+Math.cos(iq))/(double)4*(double)255) ;
				
				if (red < 0) red = (-1)*red;
				if (red > 255) red = 255-(red%255);

				if (green < 0) green = (-1)*green;
				if (green > 255) green = 255-(green%255);

				if (blue < 0) blue = (-1)*blue;
				if (blue > 255) blue = 255-(blue%255);

				
			//	if (red == 0) red = 255;
			//	if (green == 0) green = 255;
			//	if (blue == 0 ) blue = 255;

				c = new Color(red,green,blue);

				offGraphics.setColor(c);
				offGraphics.fillRect(i,j,s,s);	
			}
		}
				repaint();	

	}
	

	public boolean mouseDown(Event e, int x, int y) {	
		posx=x;
		posy=y;
		color(w/2,w/2,w,4);

		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y) {		
	
		return true;
	}
	
	public boolean mouseDrag(Event e, int x, int y) {
		posx=x;
		posy=y;
		color(w/2,w/2,w,4);

		return true;
	}
	
}









