import java.awt.*;
import java.applet.*;
import java.lang.Math.*;
import javax.swing.*;
import javax.swing.event.*;

public class rockets extends AnimeApplet{
  JSlider VBar;
  JCheckBox centerCB;
  JLabel l2;
  double V=0;
  
  double futosa=0.05;
  double nagasa=0.6;
  double Lfactor=1.0;
  boolean isEarthCenter=true;
    
    Image earth;
  
  int w,h;

  public void init(){
    super.init();
    Container cont=getContentPane();
    cont.setLayout(new BorderLayout());


    earth = getImage(getDocumentBase(), "Earth.png");
    
    VBar=new JSlider();
    VBar.setMaximum(99);
    VBar.setMinimum(0);
    VBar.setValue(0);
    VBar.setMajorTickSpacing(10);
    VBar.setMinorTickSpacing(5);
    VBar.setPaintTicks(true);
    VBar.setPaintLabels(true);
    VBar.addChangeListener(new ChangeListener() {
	public void stateChanged(ChangeEvent evt) {
	  int vv=VBar.getValue();
	  String s ="ロケットの速度は光速の"+java.lang.Integer.toString(vv)+"％";
	  l2.setText(s);
	  Vchange(vv);
	}
      });

    l2=new JLabel("ロケットの速度は光速の 0％");

    centerCB=new JCheckBox("地球静止系",true);
    centerCB.addItemListener(new java.awt.event.ItemListener() {
	public void  itemStateChanged(java.awt.event.ItemEvent evt) {
	  changecenterFlg(centerCB.isSelected());
	}
      });


    
    JPanel p2=new JPanel();
    p2.setLayout( new GridLayout(2,1));
    
    JPanel p4=new JPanel();
    p4.add(l2);
    p4.add(VBar);

    JPanel p3=new JPanel();
    p3.setLayout( new BorderLayout());
    p3.add(centerCB,BorderLayout.EAST);
    p3.add(p4,BorderLayout.CENTER);
    
    p2.add(animePanel);
    p2.add(p3);
    

    cont.add(p2,BorderLayout.SOUTH);
    cont.add(canvas,BorderLayout.CENTER);
    
    tm.start();
    start();
  }
    
  private void line(Graphics g,Color c,double x,double y,double x1,double y1)
  {
    g.setColor(c);
    g.drawLine(w/2+(int)(x*w/2),h/4-(int)(y*w/2),
	       w/2+(int)(x1*w/2),h/4-(int)(y1*w/2));
    
  }
  
    private void circle(Graphics g,Color c,double x,double y,double r)
  {
    g.setColor(c);
    g.drawOval(w/2+(int)((x-r)*w/2),h/4-(int)((y+r)*w/2),
	       (int)(r*w),(int)(r*w));
  }
  
  private void fillcircle(Graphics g,Color c,double x,double y,double r)
  {
    g.setColor(c);
    g.fillOval(w/2+(int)((x-r)*w/2),h/4-(int)((y+r)*w/2),(int)(r*w),(int)(r*w));
  }
  
  private void fillellipse(Graphics g,Color c,double x,double y,double r,double rr)
  {
    g.setColor(c);
    g.fillOval(w/2+(int)((x-rr*r)*w/2),h/4-(int)((y+r)*w/2),(int)(rr*r*w),(int)(r*w));
  }
  
  public void Vchange(int a) { V=((double)a)/100; }
  public void changecenterFlg(boolean f){ isEarthCenter=f;  }
  
  
  public void writeCanvas(Graphics g){
      int i;
      double rocketX;
      double rocketL;
      double earthX;
      double earthL;

      int y,ny;
      

      w=getWidth();
      h=getHeight();
      
      g.setColor(Color.black);
      g.fillRect(0,0,w,h);



      if( isEarthCenter){
	earthX=0;
	earthL=1;
	rocketX=-0.85+V*t;
	rocketL=Math.sqrt(1-V*V);
      } else {
	earthX=-V*t;
	earthL=Math.sqrt(1-V*V);
	rocketX=-0.85;
	rocketL=1;
      }

      double rocketW=0.3;

      //      fillellipse(g,Color.green,earthX,-0.5,0.3,earthL);
      g.drawImage(earth, 
		  w/2+(int)((earthX-earthL*0.3)*w/2),
		  h/4-(int)((-0.5+0.3)*w/2),
		  (int)(earthL*0.3*w),
		  (int)(0.3*w),this);
      line(g,Color.white,rocketX,rocketW/2,rocketX,-rocketW/2);
      line(g,Color.white,rocketX,rocketW/2,rocketX+0.5*rocketL,rocketW/2);
      line(g,Color.white,rocketX,-rocketW/2,rocketX+0.5*rocketL,-rocketW/2);
      line(g,Color.white,rocketX+0.5*rocketL,rocketW/2,rocketX+0.7*rocketL,0);
      line(g,Color.white,rocketX+0.5*rocketL,-rocketW/2,rocketX+0.7*rocketL,0);

      double t0 =t*earthL;
      double tt = t0 - 2*rocketW*Math.floor(t0/rocketW/2);
      if( tt > rocketW ) {
	fillcircle(g,Color.red,earthX,-tt+3*rocketW/2-0.4,0.01);
      } else {
	fillcircle(g,Color.red,earthX,tt-rocketW/2-0.4,0.01);
      }
      line(g,Color.black,earthX-0.05*earthL,-rocketW/2-0.4,earthX+0.05*earthL,-rocketW/2-0.4);
      line(g,Color.black,earthX-0.05*earthL,rocketW/2-0.4,earthX+0.05*earthL,rocketW/2-0.4);


      fillellipse(g,Color.yellow,earthX+0.2*earthL,-0.3,0.03,earthL);
      double phi=2*Math.PI*t0/rocketW/2;
      line(g,Color.black,
	   earthX+0.2*earthL,
	   -0.3,
	   earthX+earthL*(0.2+0.03*Math.sin(phi)),
	   -0.3+0.03*Math.cos(phi));



      t0=t*rocketL;
      tt = t0 - 2*rocketW*Math.floor(t0/rocketW/2);
      if( tt > rocketW ) {
	fillcircle(g,Color.red,rocketX+rocketL/8,3*rocketW/2-tt,0.01);
      } else {
	fillcircle(g,Color.red,rocketX+rocketL/8,tt-rocketW/2,0.01);
      }



      fillellipse(g,Color.yellow,rocketX+0.4*rocketL,0.15,0.03,rocketL);
      fillellipse(g,Color.yellow,rocketX+0.4*rocketL,0.15,0.03,rocketL);

      phi=2*Math.PI*t0/rocketW/2;
      line(g,Color.black,
	   rocketX+rocketL*0.4,
	   0.15,
	   rocketX+rocketL*(0.4+0.03*Math.sin(phi)),
	   0.15+0.03*Math.cos(phi));
      
  }
}
