import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JComponent;

public class Spirale extends JComponent {

	int max = 477;
	int numData = 1096;

	int[] data = { 1, 32, 3, 1, 133, 51, 54, 51, 25, 1, 1, 26, 92, 43, 22, 17, 0, 0, 81, 52, 51, 111, 35, 1, 2, 134, 98, 82, 68, 33, 6, 2, 223, 136, 97, 142, 74, 10, 2, 50, 142, 190, 129, 9, 7, 1, 252, 97, 99, 90, 67, 8, 3, 279, 140, 95, 105, 63, 8, 1, 221, 152, 112, 157, 65, 6, 2, 228, 108, 118, 89, 62, 2, 4, 223, 118, 95, 138, 4, 12, 3, 187, 87, 90, 95, 46, 2, 1, 193, 81, 63, 52, 41, 4, 3, 119, 53, 51, 20, 1, 0, 0, 3, 79, 28, 36, 13, 4, 0, 56, 37, 34, 23, 16, 0, 1, 72, 42, 19, 8, 1, 2, 1, 59,
			33, 63, 23, 15, 2, 1, 57, 23, 22, 15, 9, 2, 0, 34, 22, 12, 0, 9, 2, 0, 99, 39, 35, 17, 9, 0, 0, 0, 44, 20, 26, 11, 3, 0, 105, 35, 20, 22, 27, 1, 2, 52, 31, 22, 24, 0, 2, 1, 57, 23, 29, 19, 14, 2, 0, 71, 6, 34, 22, 19, 2, 1, 57, 21, 23, 18, 7, 2, 3, 62, 39, 21, 14, 13, 0, 0, 40, 21, 15, 10, 5, 0, 0, 35, 22, 11, 12, 6, 1, 1, 54, 17, 23, 16, 8, 0, 0, 35, 21, 0, 11, 12, 0, 1, 52, 21, 19, 15, 12, 3, 2, 52, 36, 23, 25, 21, 0, 1, 38, 36, 34, 34, 17, 0, 2, 67, 42, 35, 41, 29, 1, 2, 105, 57,
			61, 60, 33, 0, 0, 119, 72, 67, 35, 45, 8, 2, 161, 82, 143, 84, 48, 2, 1, 185, 162, 76, 70, 52, 2, 1, 156, 91, 79, 55, 30, 6, 2, 15, 70, 72, 60, 35, 4, 0, 144, 77, 61, 49, 35, 0, 1, 152, 76, 73, 42, 32, 4, 2, 154, 70, 54, 91, 26, 2, 1, 165, 17, 83, 65, 40, 5, 1, 246, 93, 93, 72, 51, 2, 0, 164, 127, 103, 115, 64, 3, 3, 259, 127, 101, 92, 70, 11, 1, 224, 127, 86, 67, 60, 7, 2, 109, 41, 16, 0, 1, 1, 2, 63, 29, 11, 11, 3, 5, 5, 210, 89, 55, 60, 37, 7, 2, 183, 92, 10, 72, 48, 2, 4, 212,
			111, 82, 58, 45, 7, 0, 208, 11, 94, 85, 49, 3, 2, 252, 122, 109, 95, 65, 14, 3, 264, 133, 124, 165, 79, 2, 4, 392, 12, 164, 174, 108, 8, 10, 477, 344, 170, 136, 87, 10, 6, 357, 112, 109, 107, 91, 8, 0, 360, 32, 194, 89, 104, 15, 2, 386, 170, 123, 130, 62, 5, 6, 283, 117, 7, 85, 53, 7, 1, 202, 62, 43, 23, 1, 2, 2, 2, 153, 49, 38, 28, 3, 2, 91, 45, 43, 55, 24, 4, 0, 78, 52, 36, 32, 19, 2, 4, 80, 33, 32, 31, 17, 0, 1, 61, 30, 25, 26, 17, 1, 0, 42, 14, 14, 1, 4, 3, 1, 79, 47, 21, 20,
			10, 2, 1, 6, 66, 21, 24, 6, 0, 0, 44, 28, 9, 12, 5, 0, 0, 49, 24, 22, 18, 12, 1, 0, 46, 28, 16, 23, 13, 0, 0, 57, 30, 18, 31, 13, 1, 1, 40, 23, 28, 21, 1, 0, 0, 49, 29, 14, 14, 9, 0, 1, 44, 15, 2, 24, 12, 1, 2, 32, 14, 15, 15, 5, 0, 2, 2, 15, 4, 7, 3, 1, 0, 29, 14, 19, 16, 8, 0, 0, 2, 19, 11, 6, 7, 1, 1, 42, 19, 15, 2, 25, 0, 0, 55, 22, 16, 9, 4, 1, 0, 16, 4, 10, 17, 8, 1, 1, 46, 52, 21, 12, 18, 1, 1, 60, 38, 26, 43, 18, 1, 0, 77, 47, 55, 37, 24, 3, 1, 107, 57, 54, 38, 30, 2, 0,
			126, 101, 94, 65, 55, 5, 2, 151, 78, 61, 45, 40, 4, 1, 148, 77, 90, 70, 54, 3, 1, 212, 72, 54, 59, 18, 2, 0, 149, 78, 47, 46, 32, 2, 2, 128, 72, 66, 51, 33, 3, 0, 36, 97, 78, 67, 45, 3, 2, 148, 57, 33, 14, 4, 0, 0, 6, 1, 5, 1, 3, 0, 0, 9, 4, 5, 2, 1, 1, 0, 11, 5, 8, 6, 4, 0, 0, 15, 9, 7, 3, 0, 1, 1, 17, 7, 9, 8, 1, 1, 7, 115, 58, 54, 31, 33, 4, 3, 80, 110, 39, 68, 52, 2, 2, 200, 80, 86, 71, 52, 5, 8, 407, 107, 81, 102, 67, 8, 0, 267, 125, 85, 102, 47, 3, 3, 222, 67, 79, 68, 34, 8,
			2, 187, 57, 60, 59, 35, 2, 4, 112, 51, 42, 39, 29, 2, 0, 74, 39, 37, 28, 17, 2, 1, 87, 46, 28, 10, 10, 1, 0, 66, 41, 35, 32, 17, 2, 0, 59, 35, 21, 17, 12, 0, 0, 43, 15, 32, 18, 12, 0, 0, 53, 22, 25, 19, 16, 1, 1, 51, 26, 15, 32, 10, 1, 0, 40, 20, 7, 8, 0, 1, 2, 0, 7, 15, 16, 6, 2, 0, 0, 30, 5, 7, 0, 0, 0, 16, 8, 0, 9, 4, 0, 0, 23, 12, 10, 11, 5, 0, 0, 18, 11, 6, 9, 4, 1, 0, 64, 11, 3, 0, 3, 1, 1, 32, 18, 12, 4, 3, 0, 0, 1, 21, 12, 4, 7, 2, 0, 25, 7, 51, 8, 0, 2, 0, 25, 7, 11, 9, 9,
			0, 0, 29, 10, 10, 10, 4, 0, 1, 21, 13, 15, 10, 3, 0, 0, 26, 2, 11, 4, 5, 1, 0, 16, 6, 6, 4, 2, 1, 1, 21, 6, 7, 11, 5, 2, 1, 16, 12, 9, 3, 6, 0, 2, 13, 8, 8, 2, 0, 0, 0, 12, 7, 11, 6, 3, 0, 1, 11, 4, 6, 4, 4, 2, 1, 14, 5, 13, 5, 5, 0, 0, 26, 11, 10, 7, 4, 0, 2, 40, 11, 4, 17, 11, 1, 0, 39, 21, 14, 14, 7, 0, 2, 14, 1, 50, 23, 9, 1, 5, 64, 20, 25, 19, 6, 0, 0, 42, 30, 18, 17, 10, 0, 1, 31, 18, 21, 15, 6, 1, 0, 27, 0, 33, 8, 9, 1, 1, 46, 20, 16, 23, 11, 1, 2, 43, 23, 16, 12, 9, 1, 1,
			45, 22, 23, 15, 8, 0, 1, 64, 21, 27, 19, 11, 0, 1, 46, 23, 23, 3, 10, 1, 0, 40, 27, 27, 18, 13, 2, 0, 40, 19, 15, 7, 2, 1, 0, 2, 0, 12, 17, 6, 1, 0 };

	Color[] tColor = new Color[numData];

	float[] lineWidth = new float[numData];

	double periode = 10;

	double offset = 0.1;

	public Spirale() {

		super();
		periode = 10;
		offset = 0.1;

		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				sizeChange();
			}
		});
		setPreferredSize(new Dimension(400, 300));
		setBackground(new Color(212, 208, 200));

		// System.out.println(data.length);

		ColorScale cs = new ColorScale(Color.WHITE, Color.BLUE, ColorScale.ColorSpace.HSB);
		
		float h = 240f / 360f, s = 0f, v = 1f;
		float maxLineWidth = 3; // plus mindestens 1
		for (int i = 0; i < numData; i++) {
			if (i < data.length) {
				// System.out.println(i+": "+s);
				s = (float) (data[i]) / (float) max;
				tColor[i] = cs.getInterpolatedColor(s);
				tColor[i] = Color.getHSBColor(h, s, v);
				lineWidth[i] = 1f + s * maxLineWidth;
			}
			else {
				tColor[i] = Color.RED;
			}
		}
	}

	public void setPeriode(double per) {
		periode = (per > 0) ? per : 1;
		repaint();
	}

	public void setOffset(double ofs) {
		offset = ofs;
		repaint();
	}

	public void sizeChange() {
		repaint();
	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);

		double w = getWidth();
		double h = getHeight();
		double rad = (w < h) ? (w * 0.9) / 2 : (h * 0.9) / 2;
		Point2D.Double c = new Point2D.Double(w / 2.0, h / 2.0);

		double oneData = 2 * Math.PI / periode;
		// System.out.println(rad);
		double offRad = offset * rad;
		rad -= offRad;
		// System.out.println(offRad+" "+rad);
		double oneRad = rad / (data.length + 1);

		// System.out.println("Repaint...");

		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(getBackground());
		g2.fillRect(0,0,getWidth(), getHeight());
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		double r = offRad;
		double wi = -Math.PI / 2;

		Point2D.Double last = new Point2D.Double(c.x, c.y);
		last.x += r * Math.cos(wi);
		last.y += r * Math.sin(wi);

		g2.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
		for (int i = 0; i < data.length; i++) {

			// System.out.println(i+" "+oneData+" "+numCircs+" "+oneRad+" "+wi+" "+r);

			wi += oneData;
			r += oneRad;

			Point2D.Double np = new Point2D.Double(c.x + r * Math.cos(wi), c.y + r * Math.sin(wi));

			g2.setColor(tColor[i]);
			g2.setStroke(new BasicStroke(lineWidth[i],BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));

			g2.draw(new Line2D.Double(last, np));
			last = np;
		}

	}
}
