package graphisme;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;
public class PanneauDessin extends JPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean clefContact = true;
	private double monAngle = 0.0;
	private Moteur monMoteur = new Moteur();
	private Image monImageOff;
	private Graphics gOff;
	
	public PanneauDessin()
	{
		//repaint();
		//maRose = new Image();
		monMoteur.start();
	}
	
	public void paint(Graphics g)
	{
		int centreX = this.getWidth() / 2,
			centreY = this.getHeight() / 2,
			rayon = this.getHeight() / 3,
			rayonImage = rayon + 15,
			satellX1 = rayonImage + (int)(rayon * Math.cos(monAngle)),
			satellY1 = rayonImage - (int)(rayon * Math.sin(monAngle)),
			satellX2 = rayonImage - (int)(rayon * Math.cos(monAngle)),
			satellY2 = rayonImage + (int)(rayon * Math.sin(monAngle)),
			satellX3 = rayonImage - 80 - (int)(rayon * Math.cos(monAngle + 1.5)),
			satellY3 = rayonImage - (int)(rayon * Math.sin(monAngle + 1.5));
		
		if (monImageOff == null)
		{
			// Cr�ation de l'image OFF
			monImageOff = createImage(rayonImage * 2, rayonImage * 2);
			gOff = monImageOff.getGraphics();
		}
		// Rectangle d'effacement
		gOff.setColor(Color.black);
		//g.fillRect(centreX - rayon - 15, centreY - rayon - 15, rayon * 2 + 30, rayon * 2 + 30);
		gOff.fillRect(0, 0, rayonImage * 2, rayonImage * 2);
		// Satellite 1
		gOff.setColor(Color.red);
		gOff.fillOval(satellX1 - 15, satellY1 - 15, 30, 30);
		
		// Satellite 2
		gOff.setColor(Color.green);
		gOff.fillOval(satellX2 - 15, satellY2 - 15, 30, 30);
		
		// Satellite 3
		gOff.setColor(Color.blue);
		gOff.fillOval(satellX3 - 15, satellY3 - 15, 30, 30);
		
		// Ligne inter-satellites
		gOff.setColor(Color.cyan);
		gOff.drawLine(satellX2, satellY2, satellX1, satellY1);
		gOff.drawLine(satellX2, satellY2, satellX3, satellY3);
		
		// R�v�lation de l'image de double-buffering quand tout est fini. 
		g.drawImage(monImageOff, 0, 0, null);
		g.drawImage(monImageOff, centreX - rayonImage + 100, centreY - rayonImage - 25, null);
	}
	
	private class Moteur extends Thread
	{
		@Override
		public void run()
		{
			while (clefContact)
			{
				monAngle += Math.PI / 180;
				if (monAngle > 2 * Math.PI)
				{
					monAngle -= 2 * Math.PI;
				}
				repaint();
				try
				{
					Thread.sleep(10);
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
