import java.awt.*;
import java.awt.event.*;
import java.applet.*;




public class kupszelet extends Applet implements ActionListener{
    int w,h,ix0,iy0,d;
    double eps=.1,fi=Math.PI/4.;
    Label se1;
    Scrollbar sb1;
    Panel p3;
    Graphics g;
    javax.swing.Timer timer;
    boolean fuss=false;

    void tablatorles(){
	g.setColor(Color.yellow);
	g.fillRect(0,0,w,h+100);
	g.setColor(Color.blue);
	g.fillOval(10+d-5,h/2+45,10,10);
	g.drawLine(10,0,10,h+100);
    }



    void gorbe(){
	double dfi,y,x,x1,y1,r,p,fi2,x2,x3,y2,y3;
	dfi=.04;
	p=d*eps;
	r=p/(1.+eps*Math.cos(fi));
	x1=10+d-r*Math.cos(fi);
	y1=h/2+50-r*Math.sin(fi);
	g.setColor(Color.green);
	g.drawLine(10+d,h/2+50,(int)x1,(int)y1);
	g.drawLine(10,(int)y1,(int)x1,(int)y1);
	//while(fi<2.*Math.PI){  
	    g.setColor(Color.yellow);
	    g.drawLine(10+d,h/2+50,(int)x1,(int)y1);
	    g.drawLine(10,(int)y1,(int)x1,(int)y1);
	    g.fillOval((int)x1-5,(int)y1-5,10,10);
	    fi+=dfi;
	    if(fi>2.*Math.PI) fi-=2.*Math.PI;
	    if(eps>.999&&eps*Math.cos(fi)<-.999) fi=Math.PI+Math.acos(1./eps)+dfi;
	    r=p/(1.+eps*Math.cos(fi)); 
	    x=10+d-r*Math.cos(fi);
	    y=h/2+50-r*Math.sin(fi);
	    g.setColor(Color.green);
	    g.drawLine(10+d,h/2+50,(int)x,(int)y);
	    g.drawLine(10,(int)y,(int)x,(int)y);

	    g.setColor(Color.blue);
	    g.fillOval(10+d-5,h/2+45,10,10);
	    g.drawLine(10,0,10,h+100);
	    g.setColor(Color.pink);
	    g.fillOval((int)x-5,(int)y-5,10,10);

	    g.setColor(Color.red);
	    fi2=0;
	    r=p/(1.+eps*Math.cos(fi2));
	    x3=10+d-r*Math.cos(fi2);
	    y3=h/2+50-r*Math.sin(fi2);
	    while(fi2<2.*Math.PI){ 
		if(Math.abs(eps-1.)<.001){ fi2+=dfi*2.3;}else{fi2+=dfi;}
		boolean kell=true;
		if(eps>.999&&eps*Math.cos(fi2)<-.999){ 
		    fi2=Math.PI+Math.acos(1./eps)+dfi*2.3;kell=false;
		}else{kell=true;}
		r=p/(1.+eps*Math.cos(fi2));
		x2=10+d-r*Math.cos(fi2);
		y2=h/2+50-r*Math.sin(fi2);
		if(kell) g.drawLine((int)x2,(int)y2,(int)x3,(int)y3);
		x3=x2;y3=y2;
	    }
	    x1=x;y1=y; 
	    //}
	
    }
   



    public boolean OK=false;
    public void elindit(){OK=true;}

    public void init(){

	

	timer = new javax.swing.Timer(200, this);


	p3=new Panel();
	add(p3);
	p3.setBackground(Color.white);
	p3.setLayout(new GridLayout(1,2));

	sb1=new Scrollbar(Scrollbar.HORIZONTAL,2,1,2,21);
	sb1.setSize(100,10);
	p3.add(sb1);
	
	eps=sb1.getValue()*.1;
	int nh=new Double(eps).toString().length();
	if(nh>5) nh=5;
	se1=new Label("  excentricitás (epsilon): "+new Double(eps).toString().substring(0,nh)+" ");p3.add(se1);

	g=getGraphics();
	w=this.getBounds().width;
	h=this.getBounds().height-80;
	ix0=this.getBounds().x;
	iy0=this.getBounds().y;
	d=w/4;




	sb1.addAdjustmentListener(new AdjustmentListener(){
		public void adjustmentValueChanged( AdjustmentEvent adje){
		    eps=sb1.getValue()*.1;
		    int nh=new Double(eps).toString().length();
		    if(nh>5) nh=5;
		    se1.setText("  excentricitás (epsilon): "+new Double(eps).toString().substring(0,nh)+" ");
		   
		}
		});

	this.addMouseListener(new MouseListener(){
		public void 	mouseClicked(MouseEvent e){
		   
		    
}
		// Invoked when the mouse button has been clicked (pressed and released) on a component.
		public void 	mouseEntered(MouseEvent e){}
 //Invoked when the mouse enters a component.
		public  void 	mouseExited(MouseEvent e){}
 //Invoked when the mouse exits a component.
		public void 	mousePressed(MouseEvent e){}
 //Invoked when a mouse button has been pressed on a component.
		public void 	mouseReleased(MouseEvent e){
		    //if(e.getButton()==e.BUTTON1){
		    if(!fuss){
			tablatorles();
			timer.start();
			fuss=true;
		    }else{timer.stop();fuss=false;}
}


});


    }

    public void actionPerformed(ActionEvent ae){
	gorbe();
    }

    public void stop(){}
    public void destroy(){}
    public void paint(Graphics g){
	w=this.getBounds().width;
	h=this.getBounds().height-80;
	ix0=this.getBounds().x;
	iy0=this.getBounds().y;
	tablatorles();
	gorbe();
	
	
		    
    }
}





