import java.awt.Color;
import java.awt.event.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

class planet implements ActionListener{

        String name;
        //period

        double period;//ms
        //orbit
        public double a,b,c,p,e;
        //planet
        double planetSize;//radius of the planet
        //position
        public double position_theta, position_x, position_y;
        public double age=0; //age
        double rotateSpeed;
        public Color orbitColor=Color.pink, planetColor=Color.blue;
//Constructor
        public RectangularShape orbit,mover;

        public planet(double a, double b, double planetSize,double period, String name){
                this.a=a;
                this.b=b;
                this.c=Math.sqrt(a*a-b*b);
                this.planetSize=planetSize;
                this.period=period;
                this.e=c/a;
                this.p=a*a/c-c;
                position_theta=0;
                position_x=a+c;
                position_y=0;
                age =0;
                this.name=name;
                rotateSpeed=2*Math.PI*a*b/(period*position_x*position_x);
                orbit = new Ellipse2D.Double(c-a,-b,2*a,2*b);
                mover = new Ellipse2D.Double(a+c-planetSize,-planetSize,2*planetSize,2*planetSize);
        }
//setColor
        public void setColor(Color orbitColor, Color planetColor){
                this.orbitColor=orbitColor;
                this.planetColor=planetColor;
        }

//response to the timer action
        public void actionPerformed(ActionEvent event){
                double delay=((Timer)(event.getSource())).getDelay();
                age = age + delay;

                position_theta=position_theta+rotateSpeed*delay;
                double ro= e*p/(1-e*Math.cos(position_theta));

                position_x=ro*Math.cos(position_theta);

                position_y=ro*Math.sin(position_theta);
                rotateSpeed=2*Math.PI*a*b/(period*ro*ro);
                if(age%period == period/2){
                        rotateSpeed=2*Math.PI*a*b/(period*(a-c)*(a-c));
                        position_theta=Math.PI;
                        position_x=c-a;
                        position_y=0;
                        }
                if(age%period == 0){
                        rotateSpeed=2*Math.PI*a*b/(period*(a+c)*(a+c));
                        position_theta=0;
                        position_x=c+a;
                        position_y=0;
                        }
                mover = new Ellipse2D.Double(position_x-planetSize,position_y-planetSize,2*planetSize,2*planetSize);
        }
        public String toString(){
                return name;
        }
        public void display(Graphics2D g){
                g.setPaint(planetColor);
                g.fill(mover);
                g.setPaint(orbitColor);
                g.draw(orbit);
                g.drawString(toString(),((int)(position_x)),((int)(position_y)));
        }
}

class Solar extends JPanel implements ActionListener {
    planet earth,venus,jupiter,mercury,mar;
    Timer god;
    RectangularShape sun;

    Solar(){
        earth = new planet(149,109,6,36400,"Earth");//
        venus = new planet(108,78,6,20400,"Venus"); //
        mercury = new planet(58,38,2.4,8400,"Mercury");//
        mar = new planet(227,187,3,68400,"Mar");//
//        jupiter = new planet(778,778,10,433200);//
        jupiter = new planet(300,220,10,433200,"Jupiter");//

        god = new Timer(100,this);
//        god.setRepeats(false);
        god.start();
        god.addActionListener(mercury);
        god.addActionListener(jupiter);
        god.addActionListener(earth);
        god.addActionListener(mar);
        god.addActionListener(venus);

        sun = new Ellipse2D.Double(0,0,20,20);
        
    }

    public Color getBackground(){
        return Color.black;
    }

    public void renderShape(){
        repaint();
    }

    public void actionPerformed(ActionEvent e){
        renderShape();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setBackground(Color.black);
        Dimension s = getSize();

        double a00=0,a10=0,a01=0,a11=0,a02=0,a12=0;

//sets the shape to the center of the canvas.
        a02=s.width/2;
        a12=s.height/2;
//draw sun
        double solarCycle = 200000;
        AffineTransform transformation = new AffineTransform(1,0,0,1,a02,a12);
//        transformation.scale(0.5,0.5);
        double solarAngle=earth.age/solarCycle*360;
        a00=Math.cos(Math.PI*solarAngle/180);
        a10=-Math.sin(Math.PI*solarAngle/180);
        a01=Math.sin(Math.PI*solarAngle/180);
        a11=Math.cos(Math.PI*solarAngle/180);

        AffineTransform rotateSolar = new AffineTransform(a00,a10,a01,a11,0,0);
        transformation.concatenate(rotateSolar);

        g2.transform(transformation);
        g2.setPaint(Color.red);
        g2.fill(sun);
//draw mercury
        a00=Math.cos(Math.PI*0/180);
        a10=-Math.sin(Math.PI*0/180);
        a01=Math.sin(Math.PI*0/180);
        a11=Math.cos(Math.PI*0/180);
        AffineTransform rotate = new AffineTransform(a00,a10,a01,a11,0,0);
        g2.transform(rotate);
        mercury.display(g2);
//draw jupiter
        a00=Math.cos(Math.PI*3/180);
        a10=-Math.sin(Math.PI*3/180);
        a01=Math.sin(Math.PI*3/180);
        a11=Math.cos(Math.PI*3/180);
        rotate = new AffineTransform(a00,a10,a01,a11,0,0);
        g2.transform(rotate);
        jupiter.display(g2);
//draw earth
        a00=Math.cos(Math.PI*20/180);
        a10=-Math.sin(Math.PI*20/180);
        a01=Math.sin(Math.PI*20/180);
        a11=Math.cos(Math.PI*20/180);
        rotate = new AffineTransform(a00,a10,a01,a11,0,0);
        g2.transform(rotate);
        earth.display(g2);
//draw mar
        a00=Math.cos(Math.PI*2/180);
        a10=-Math.sin(Math.PI*2/180);
        a01=Math.sin(Math.PI*2/180);
        a11=Math.cos(Math.PI*2/180);
        rotate = new AffineTransform(a00,a10,a01,a11,0,0);
        g2.transform(rotate);
        mar.display(g2);
//draw venus
        a00=Math.cos(Math.PI*52/180);
        a10=-Math.sin(Math.PI*52/180);
        a01=Math.sin(Math.PI*52/180);
        a11=Math.cos(Math.PI*52/180);
        rotate = new AffineTransform(a00,a10,a01,a11,0,0);
        g2.transform(rotate);
        venus.display(g2);
    }

/*    public static void main(String s[]) {
        JFrame f = new JFrame("Solar");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JPanel Solar = new Solar();
        f.getContentPane().add("Center", Solar);
        f.pack();
        f.setSize( 1000, 800 );
        f.show();
    }
*/
}

public class Jiejie extends JApplet{
        public void init(){
                getContentPane().add(new Solar());
        }
}




