

import java.util.*;
import java.awt.*;
import java.applet.*;


public class MathArt07 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 = 100;
	
	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();
		}
		
		color(w/2,h/2,w,h,6,8);
		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 rW, int rH, int sW, int sH) {
		
		for (int i=x-rW/2; i<x+rW/2; i=i+sW) {
			for (int j=y-rH/2; j<y+rH/2; j=j+sH) {		
				
				double iq  = (double)(   (double)i            /(double)30);
				double jq  = (double)(   (double)j            /(double)(80+posx));
				double ijq = (double)(  (2*(double)i+(double)j) /(double)(25+posy/3));
				
				int red   = (int) ( ( iq) / (double)2*(double)255)  ;				
				int green = (int) ( ( jq) / (double)2*(double)255) ;
				int blue  = (int) ( (ijq) / (double)2*(double)255) ;
				
				if ((red < 0)   || (red > 255))   red=255-(red%255);
				if ((green < 0) || (green > 255)) green = 255-(green%255);
				if ((blue < 0)  || (blue > 255))  blue = 255-(blue%255);
				
				c = new Color(red,green,blue);
				
				offGraphics.setColor(c);
				offGraphics.fillRect(i,j,sW-2,sH-2);	
			}
		}
	
	}
	
	public boolean mouseDown(Event e, int x, int y) {	
		posx=x;
		posy=y;
		color(w/2,h/2,w,h,6,8);
		repaint();

		return true;
	}
}









