import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class earthRot{
	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 times=0.;
        double PI=3.1416;
        double theta=0.,thetaP=0.,Y=200.;
        double fps=60.;
        double Tpendurum=1.;//振り子の周期が1秒
        double Tearth=10.;//地球の自転が10秒

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
                times=times+1;
                theta=theta+2.*PI/(Tearth*fps);//地球の回転は反時計回り、そこでは振り子は(x,y)
                thetaP=thetaP+2.*PI/(Tpendurum*fps);//振り子の位相
	        Y=150.*Math.cos(thetaP);//北極の上の振り子は(X,Y)
 
                int x,y;
                
	        x=-(int)(Y*Math.sin(theta));//X=0なので普通の回転変換でこうなるが
	        y=(int)(Y*Math.cos(theta));//グラフィック座標系はX軸が反転してる
                
                g.fillOval(x+200, y+200, 5, 5);
        }
}
