import javax.swing.*;
import java.awt.*;

public class ColorTransition extends javax.swing.JApplet {
	
	private static final int WIDTH = 400;
	private static final int HEIGHT = 100;
	private static final int INC = 2;
	
	
	Color c1, c2;
	
	public void start() {
		try {
		Runnable showModalDialog = new Runnable() {
			public void run() {
				getColors();
			}
		};
		SwingUtilities.invokeAndWait(showModalDialog);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void paint(Graphics page) {
		int c1r = c1.getRed(), c2r = c2.getRed();
		int c1g = c1.getGreen(), c2g = c2.getGreen();
		int c1b = c1.getBlue(), c2b = c2.getBlue();
		
		for(int x= 0; x <=WIDTH-INC; x+= INC) {
			int red = between(c1r, c2r, (double)x/WIDTH);
			int green = between(c1g, c2g, (double)x/WIDTH);
			int blue = between(c1b, c2b, (double)x/WIDTH);
			page.setColor(new Color(red, green, blue));
			page.fillRect(x, 0, INC, ColorTransition.HEIGHT);
		}
	}
	
	private void getColors() {
		c1 = JColorChooser.showDialog(null, "Choose the first color", null);
		c2 = JColorChooser.showDialog(null, "Choose the second color", null);
	}

	private static int between(int val1, int val2, double ratio) {
		return (int) (val1 + (val2 - val1) * ratio);
	}
}

