

import java.util.*;
import java.awt.*;
import java.applet.*;


public class MathArt09 extends Applet{
	
	int w, h;
	private Image offImage;
   	private Dimension offDimension;
	private Graphics offGraphics;
	Color c = new Color(0,0,0);
	int posx=100;
	int posy=200; 
	
	public void init() {
		
		Dimension d = this.size(); 
		w = d.width;
		h = d.height;
		
		if ((offImage == null) || (w != offDimension.width) || (h != offDimension.height)) {
			offImage = createImage(w, h);
			offDimension = d;
			offGraphics = offImage.getGraphics();
		}
		paintSquares();	
		repaint();
		
		//		color();
	}
	
	public void update(Graphics g) {
		paint(g);
	}
	
	public void paint(Graphics g) {	
		g.drawImage(offImage, 0, 0, this);
	}
	
	
	public void color() {
		
		paintBackground();	
		
		int q = posx+posy;
		int sH = (int) (Math.random() * 20);
		int sW = (int) (Math.random() * 20);
		
		for (int j=0; j<h; j=j+sH) {
			
			double sin = Math.sin((double)j*j/(double)384);
			double cos = Math.cos((double)j/(double)24);
			
			double hue = 2*( (sin + cos)/(double)(20) + (double)(q)*(double)(0.001125));
			
			if (hue > (0.2)) hue = (double)(0.4) - hue;
			if (hue <0) hue = hue * (double)(-1);
			if (hue >1) hue = 1 - (hue -1);
			
			c = new Color(Color.HSBtoRGB ((float)(hue) , (float)(0.4), (float)(0.6))); 
			offGraphics.setColor(c);
			offGraphics.fillRect(0,j,w,sH-2);
			
			sH = (int) (Math.random() * 20);	
		}
		
		repaint();
	}
	
	
	public void paintBackground() {
		
		int xy = (posx+posy);
		if (xy < 1) xy =1;
		int yx = (posy-posx);
		if (yx < 1) yx = (-1)*yx+1;
		
		for (int j=0; j<h/2; j=j+10) {
			
			double value = (double)j*(0.5)/(double)(h/2)+(0.3);
			value = value + (double)((int)j%yx)*(0.1)/(double)(xy);
			
			double hue = (double)j*(0.2)/(double)(h/2)+(0.3);
			hue = hue + (double)((int)j%yx) * (0.2) / (double)(xy);
			
			c = new Color(Color.HSBtoRGB ((float)(hue) , (float)(0.1), (float)(value))); 
			offGraphics.setColor(c);
			offGraphics.fillRect(0,j,w,10);
		}
		
		
		for (int j=h/2; j<h; j=j+10) {
			
			double value = (double)(h-j)*(0.5)/(double)(h/2)+(0.3);
			value = value - (double)((int)j%yx)*(0.1)/(double)(xy);
			
			double hue = (double)j*(0.3)/(double)(h/2)+(0.3);
			hue = hue - (double)((int)j%yx)*(0.2)/(double)(xy);
			
			c = new Color(Color.HSBtoRGB ((float)(hue) , (float)(0.1), (float)(value))); 
			offGraphics.setColor(c);
			offGraphics.fillRect(0,j,w,10);
		}
		
	}
	
	
	public void paintSquares() {
		
		for (int j=0; j<h; j=j+7) {
			for (int i=0; i<w; i=i+10) {
				
				
				double value = (double)j*(0.5)/(double)(h/2)+(0.3);
				value = value + (double)((int)j%posy)*(0.1)/(double)(13);
				
				double hue = (double)i*(0.2)/(double)(h/2)+(0.3);
				hue = hue + (double)((int)i%posx) * (0.2) / (double)(33);
				
				
				double sat = (double)i*(0.2)/(double)(h/2)+(0.3);
				sat = sat + (double)(i*j * 0.2) / (double)(w*h);
				
				
				c = new Color(Color.HSBtoRGB ((float)(hue) , (float)(sat), (float)(value))); 
				offGraphics.setColor(c);
				offGraphics.fillRect(i,j,10,7);
			}
		}
	}
	
	public boolean mouseDown(Event e, int x, int y) {	
		
		if (x>0 & x<w)
			posx=x;
		if (y>0 & y<h)
			posy=y;
		
		paintSquares();	
		
		repaint();
		
		return true;
	}
	
}









