// This applet was manufactured by © 2000 Juergen Giesen


import java.awt.*;
import java.applet.Applet;

public class CMYColorApplet10 extends Applet
{
	Image bild;
	int x0=10, y0=10;
	int w=10;
	int h=25;
	int xL=10, xR=267;
	int left=38;
	// circle center coordinates
	int xCyanStart=105, yCyanStart=165;
	int xCyan=xCyanStart, yCyan=yCyanStart;	
	int xMagentaStart=225, yMagentaStart=165;
	int xMagenta=xMagentaStart, yMagenta=yMagentaStart;	
	int xYellowStart=165, yYellowStart=270;
	int xYellow=xYellowStart, yYellow=yYellowStart;
	int radius=50;
	int r2=radius*radius;
	boolean cyan=true, magenta=true, yellow=true;	
	boolean both=false;
	int valCyan=255, valMagenta=255, valYellow=255;
	int valRed=255, valGreen=255, valBlue=255;
	Button myButton;
	boolean control=false;
		
	public void init() {
		
		setBackground(Color.white);
		bild = getImage(getDocumentBase(),"applet/schieber.gif");				
		myButton = new Button("reset");
		BorderLayout bl = new BorderLayout();
		setLayout(bl);
		add("South",myButton);
	}
//--------------------------------------------------------	
	public boolean mouseDown(Event event, int x, int y) {
	
		control=false;
		
		// select circle
		if (distance(x,xCyan,y,yCyan)<r2) {cyan=true; control=false;} else cyan=false;
		if (distance(x,xMagenta,y,yMagenta)<r2) {magenta=true;control=false;} else magenta=false;
		if (distance(x,xYellow,y,yYellow)<r2) {yellow=true; control=false;} else yellow=false;
		
		
		
		both=false;

		if (distance(x,xCyan,y,yCyan)<r2 && distance(x,xMagenta,y,yMagenta)<r2) both=true;
		if (distance(x,xCyan,y,yCyan)<r2 && distance(x,xYellow,y,yYellow)<r2) both=true;
		if (distance(x,xMagenta,y,yMagenta)<r2 && distance(x,xYellow,y,yYellow)<r2) both=true;
						
		// cyan controll bar
		if ((x<xL)  && (y>y0) && (y<y0+h)) {
			valCyan=0;					
			repaint();
		}
		if ((x>xR)  && (y>y0) && (y<y0+h)) {
			valCyan=255;			
			repaint();
		}
		if ((x>xL) && (x<xR) && (y>y0) && (y<y0+h)) {
			x0=x-w/2;
			valCyan=x-xL-1;
			valGreen=x-xL-1;
			valBlue=x-xL-1;
			control=true;			
			repaint();
		}
		// magenta controll bar
		if ((x<xL)  && (y>y0+30) && (y<y0+h+30)) {
			valMagenta=0;
			repaint();
		}							
		if ((x>xR)  && (y>y0+30) && (y<y0+h+30)) {
			valMagenta=255;			
			repaint();
		}
		if ((x>xL) && (x<xR) && (y>y0+30) && (y<y0+h+30)) {
			x0=x-w/2;
			valMagenta=x-xL-1;
			valRed=x-xL-1;
			valBlue=x-xL-1;
			control=true;						
			repaint();
		}
		// yellow controll bar							
		if ((x<xL)  && (y>y0+60) && (y<y0+h+60)) {
			valYellow=0;			
			repaint();
		}
		if ((x>xR)  && (y>y0+60) && (y<y0+h+60)) {
			valYellow=255;			
			repaint();
		}		
		if ((x>xL) && (x<xR) && (y>y0+60) && (y<y0+h+60)) {
			x0=x-w/2;
			valYellow=x-xL-1;
			valRed=x-xL-1;
			valGreen=x-xL-1;
			control=true;
			repaint();
		}					
		return true;		
	}
//--------------------------------------------------------	
	public boolean mouseDrag(Event event, int x, int y) {
	
		// cyan controll bar
		if ((x>xL) && (x<xR) && (y>y0) && (y<y0+h) && (control)) {
			x0=x-w/2;
			valCyan=x-xL-1;
			repaint();
		}
		// magenta controll bar
		if ((x>xL) && (x<xR) && (y>y0+30) && (y<y0+h+30) && (control)) {
			x0=x-w/2;
			valMagenta=x-xL-1;			
			control=true;																		
			repaint();
		}
		// yellow controll bar		
		if ((x>xL) && (x<xR) && (y>y0+60) && (y<y0+h+60) && (control)) {
			x0=x-w/2;
			valYellow=x-xL-1;			
			repaint();
		}		

		if (both) return true;
		
		// drag circles
		if (distance(x,xCyan,y,yCyan)<r2 && x>left && x<left+255 && y>100 && y<335 && (cyan)) {					
			xCyan=x;
			yCyan=y;
			repaint();			
			return true;
		}				
		if (distance(x,xMagenta,y,yMagenta)<r2 && x>left && x<left+255 && y>100 && y<335 && (magenta)) {						
			xMagenta=x;
			yMagenta=y;
			repaint();			
			return true;			
		}		
		if (distance(x,xYellow,y,yYellow)<r2 && x>left && x<left+255 && y>100 && y<335 && (yellow)) {					
			xYellow=x;
			yYellow=y;
			control=false;
			repaint();			
			return true;			
		}				
		repaint();				
		return true;	
	}
//--------------------------------------------------------		
	public boolean action(Event event, Object eventobject) {
											
		if (event.target instanceof Button) {		
			xCyan=xCyanStart; yCyan=yCyanStart;
			xMagenta=xMagentaStart; yMagenta=yMagentaStart;
			xYellow=xYellowStart; yYellow=yYellowStart;
			valCyan=255; valMagenta=255; valYellow=255;
			valRed=255; valGreen=255; valBlue=255;			
			repaint();		
		}
		return true;
	}	
//--------------------------------------------------------	
	public int distance(int x1,int x2, int y1, int y2) {
		return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
		}
//--------------------------------------------------------	
	public String hex(int n) {
		String str="0123456789ABCDEF";
		int n1 = (int)(n/16);
		int n2 = n % 16;
		return "" + str.charAt(n1) + str.charAt(n2);
		}
//--------------------------------------------------------	
	public void paint( Graphics g ) {
		
		g.setColor(Color.red);
		g.drawString("Drag circles !",125,347);
		Font f = new Font("Helvetica", Font.PLAIN, 9);
		g.setColor(Color.black);		
		g.setFont(f);
		g.drawString("CMY 1.0  -  © 2000 Juergen Giesen  -  www.sweethome.de/giesen/",30,359);
		f = new Font("Helvetica", Font.PLAIN, 10);
		g.setFont(f);
		
		g.drawRect(left,100,255,235);
		g.drawRect(1,1,size().width-2,size().height-2);
					
		// draw control bars
		g.setColor(Color.cyan);
		g.fillRect(xL,17,255,10);
		g.drawImage(bild,valCyan+xL-5,y0,this);
		g.drawString(""+valCyan, 280, 25);
		g.drawString(hex(valCyan), 310, 25);
						
		g.setColor(Color.magenta);
		g.fillRect(xL,17+30,255,10);
		g.drawImage(bild,valMagenta+xL-5,y0+30,this);
		g.drawString(""+valMagenta, 280, 25+30);
		g.drawString(hex(valMagenta), 310, 25+30);				

		g.setColor(Color.yellow);
		g.fillRect(xL,17+60,255,10);
		g.drawImage(bild,valYellow+xL-5,y0+60,this);
		g.drawString(""+valYellow, 280, 25+60);
		g.drawString(hex(valYellow), 310, 25+60);				
						
		// draw scoll bar marks
		g.setColor(Color.white);				
		g.drawLine(xL+50,15,xL+50,28);
		g.drawLine(xL+100,15,xL+100,28);
		g.drawLine(xL+150,15,xL+150,28);
		g.drawLine(xL+200,15,xL+200,28);
		
		g.drawLine(xL+50,45,xL+50,58);
		g.drawLine(xL+100,45,xL+100,58);
		g.drawLine(xL+150,45,xL+150,58);
		g.drawLine(xL+200,45,xL+200,58);
		
		g.drawLine(xL+50,75,xL+50,88);
		g.drawLine(xL+100,75,xL+100,88);
		g.drawLine(xL+150,75,xL+150,88);
		g.drawLine(xL+200,75,xL+200,88);						
				
		// draw circles		
		g.setColor(new Color(0,valCyan,valCyan));						
		g.fillOval(xCyan-radius,yCyan-radius,2*radius,2*radius);
		
		g.setColor(new Color(valMagenta,0,valMagenta));						
		g.fillOval(xMagenta-radius,yMagenta-radius,2*radius,2*radius);
		
		g.setColor(new Color(valYellow,valYellow,0));						
		g.fillOval(xYellow-radius,yYellow-radius,2*radius,2*radius);				
		
		// draw intersections if necessary 
		if (distance(xCyan,xMagenta,yCyan,yMagenta)<4*r2 || distance(xCyan,xYellow,yCyan,yYellow)<4*r2 || distance(xMagenta,xYellow,yMagenta,yYellow)<4*r2)
		for (int i=left; i<left+255; i++ )
			for (int j=100; j<335; j++)
			 {
				if (distance(xCyan,i,yCyan,j)<=r2 && distance(xMagenta,i,yMagenta,j)<=r2) {g.setColor(new Color(0,0,valBlue));g.drawLine(i,j,i,j);}		
				if (distance(xCyan,i,yCyan,j)<=r2 && distance(xYellow,i,yYellow,j)<=r2) {g.setColor(new Color(0,valGreen,0));g.drawLine(i,j,i,j);}
				if (distance(xMagenta,i,yMagenta,j)<=r2 && distance(xYellow,i,yYellow,j)<=r2) {g.setColor(new Color(valRed,0,0));g.drawLine(i,j,i,j);}
				if (distance(xCyan,i,yCyan,j)<=r2 && distance(xMagenta,i,yMagenta,j)<=r2 && distance(xYellow,i,yYellow,j)<=r2) {g.setColor(new Color(0,0,0));g.drawLine(i,j,i,j);}
			}			
	}
}

