import javax.swing.*;
import java.awt.*;

public class ShadedBall extends javax.swing.JApplet {
	
	private static final int CENTERX = 300;
	private static final int CENTERY = 300;
	private static final int RADIUS = 300;
	private static final int INC = 2;
	
	
	Color color;
	
	public void start() {
		try {
			Runnable showModalDialog = new Runnable() {
				public void run() {
					getColor();
				}
			};
			SwingUtilities.invokeAndWait(showModalDialog);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void paint(Graphics g) {
		
		int red = color.getRed();
		int blue = color.getBlue();
		int green = color.getGreen();
		
		for(int r = RADIUS; r > 0; r -= INC) {
			g.setColor(new Color(between(red, 0, (double)r/RADIUS),
					between(green,0,  (double)r/RADIUS),
					between(blue, 0,  (double)r/RADIUS)));
			
			g.fillOval(CENTERX-r, CENTERY-r, 2*r, 2*r);
		}

	}
	
	private static int between(int val1, int val2, double ratio) {
		// cone
		//return (int) (val1 + (val2 - val1) * ratio);
		
		// ball
		return (int) (val1 + (val2 - val1) * (1-Math.cos(Math.PI*0.5*ratio)));
	}
	
	private void getColor() {
		color = JColorChooser.showDialog(null, "Choose the ball color", null);
	}
	
}

