
// package express.color;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class ColorSelector extends Applet {

	int xPos;
	int yPos;
	Color selectedColor;

	private Point grayPoint;
	private Point colorPoint;
	
	private Image offImage;
	private Graphics offGraphics;
	 
	static int w=400, h=206;
	
	class ChoiceListener implements  MouseMotionListener, MouseListener{

		public void mouseDragged(MouseEvent e) {	
	//		repaint();
		}
		public void mouseMoved(MouseEvent e) {
		}
		public void mousePressed(MouseEvent e) {
	//		repaint();
		}
		public void mouseReleased(MouseEvent e) {
		}
		public void mouseEntered(MouseEvent e) {
		}
		public void mouseExited(MouseEvent e) {
		}
		public void mouseClicked(MouseEvent e) {
		}
	}


	public ColorSelector() {
		
		ChoiceListener chooseL = new ChoiceListener();
		this.addMouseListener(chooseL);
		this.addMouseMotionListener(chooseL);
		
//		repaint();
	}

	public void paintGray(int xx, int yy, int ww, int hh) {
		
		for (int i=xx; i<ww; i=i+4) {
	
				offGraphics.setColor(Color.getHSBColor(0, 0, ((float)(w-i)/(float)w))  );
				offGraphics.fillRect(i,0,4,5);
			
		}
	}
	
	public void paintColor(int xx, int yy, int ww, int hh) {
		
		for (int i=xx; i<ww; i=i+4) {
			for (int j=yy; j<hh; j++) {
				
				//offGraphics.setColor(Color.getHSBColor(((float)i/(float)ww), ((float)j-6/(float)hh), 0.85f)  );
				
				offGraphics.setColor(Color.getHSBColor(((float)i/(float)ww), ((float)(206-j)/(float)(hh-6)), 0.85f)  );
				offGraphics.fillRect(i,j,4,1);	
			}
		}
	}


		
	public void update(Graphics g) {
		
		if (offImage == null ) {		
			offImage = createImage(w, h);
			offGraphics = offImage.getGraphics();
		}
		paintGray(0,0,w,6);
		paintColor(0,6,w,h);
		
		paint(g);
	}

	public void paint(Graphics g) {
		g.drawImage(offImage, 0, 0, this);
	}
	
	
	public boolean inside(int d, int x, int y, Point p) {
		boolean b;
		
		if ( (Math.sqrt( (p.x-x)*(p.x-x) + (p.y-y)*(p.y-y) ) ) < d )  b = true;
		else b = false;

		return b;	
	}
	
	
	
	public static void main(String[] args) {
		WindowListener l = new WindowAdapter()	{
			public void windowClosing(WindowEvent e) {System.exit(0);}
			};	

		Frame f = new Frame("ColorSelector");
		f.addWindowListener(l);
		f.add(new ColorSelector());
		f.setSize(w, h);
		f.show();
		
	}
		
	
	public String[][] getParameterInfo() {
		return null;
	}

}
