import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class planetMain{
	public static void main(String[] args) {
		GameWindow gw = new GameWindow("テストウィンドウ",400,400);
		gw.setVisible(true);
		gw.change(new DrawCanvas());
		gw.startGameLoop();
	}
}
class GameWindow extends JFrame implements Runnable{
	private Thread th = null;
	private double sleepAddTime;
	private int fps=60;



	public GameWindow(String title, int width, int height) {
		super(title);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(width,height);
		setLocationRelativeTo(null);
		setLayout(null);
		setResizable(false);
		setFps(fps);
	}
	public synchronized void change(JPanel p) {
		getContentPane().removeAll();
		Insets inset = getInsets();
		p.setBounds(-inset.left,-inset.top,getWidth(),getHeight());
		add(p);
		validate();
		repaint();
	}
	public synchronized void startGameLoop(){
		if ( th == null ) {
			th = new Thread(this);
			th.start();
 
		}
	}
	public synchronized void stopGameLoop(){
		if ( th != null ) {
			th = null;
		}
	}
	public void run(){
		double nextTime = System.currentTimeMillis() + sleepAddTime;

		while(th != null){
			try{
				long res = (long)nextTime - System.currentTimeMillis();
				if ( res < 0 ) res = 0;
				Thread.sleep(res);


				repaint();
				nextTime += sleepAddTime;
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
	public void setFps(int fps){
		if ( fps < 10 || fps > 60 ) {
			throw new IllegalArgumentException("fpsの設定は10～60の間で指定してください。");
		}
		this.fps = fps;
		sleepAddTime = 1000.0 / fps;
	}
}
class DrawCanvas extends JPanel{
//ここにアニメーション処理を記述
    double fpsInv=1./60;
    double ecli=0.0934,a=1.524;
    double aV = 0.723;
    double theta=0.;
    double thetaE=0.,radE=1.;  //earth moves in circle.
    double thetaV=0.,radV=aV;  //venus moves in circle.
    double ecli2= ecli* ecli;
    double scale=100.;
    int circleE=(int)scale;
    int circleV=(int)(scale*aV);
    double bMars=a*Math.sqrt(1-ecli2);
    int leftX=200-(int)(scale*a*(1+ecli));
    int leftY=200-(int)(scale*bMars);
    int widthMars=(int)(2*scale*a);
    int heightMars=(int)(2*scale*bMars);

    double speedfactor=Math.sqrt(a);
    double speedfactorV=Math.sqrt(aV);

    double rad=a*(1-ecli2)/(1.+ecli*Math.cos(theta)); 
    //radE = 1.;
    //radV = aV;
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);
            
            int x,y;
            int xE,yE;
            int xV,yV;
    
            // anglular velocity gets faster near the sun. Kepler's 2nd law.
            theta=theta+fpsInv*speedfactor/(rad*rad);
	    thetaE = thetaE + fpsInv;
	    thetaV = thetaV + fpsInv*speedfactorV/(radV*radV);
            //g.setColor(Color.white);
            //g.fillRect(0, 0, d.width, d.height);
            g.setColor(Color.black);
            // this is eclipse. Kepler's 1st law.
            //ケプラーの第３法則はセメスター物理Iのp50.
            rad=a*(1-ecli2)/(1.+ecli*Math.cos(theta));

            x=(int)(scale*rad*Math.cos(theta));
            y=(int)(scale*rad*Math.sin(theta));
            xE = (int)(scale*Math.cos(thetaE));
            yE = (int)(scale*Math.sin(thetaE));
            xV = (int)(scale*radV*Math.cos(thetaV));
            yV = (int)(scale*radV*Math.sin(thetaV));
            g.drawLine(190,200,210,200);
            g.drawLine(200,190,200,210);
            g.drawOval(200-circleE,200-circleE,2*circleE,2*circleE);
            g.drawOval(200-circleV,200-circleV,2*circleV,2*circleV);
            g.drawOval(leftX,leftY,widthMars,heightMars);
            g.fillOval(x+197, -y+197, 6, 6);     //left corner, width
            g.fillOval(xE+197, -yE+197, 6, 6);
            g.fillOval(xV+197, -yV+197, 6, 6);


        }
}


