import java.applet.*;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.lang.Thread;
import javax.swing.*;


class cl_EinSchwing
{
	void EinS_Rech(double t, int t0, double w)
	{
		t = t*0.5;
		if (t < t0){			
			nq = t/t0-Math.sin(w*t)/(w*t0);}
			
		else {
			
			nq = 1+(Math.sin(w*(t-t0))-Math.sin(w*t))/(w*t0);
		}
			
		
	}
	double nq;
}
public class Cl_Einschwing_app	extends Applet {
	Scrollbar sb_t0, sb_w;
	TextField tx_t0, tx_w;
	int t0 = 5;
	double w = 1.0;
    Graphics Buffergrafik;
    Image offscreen;
    int Punkte = 500;
    
	public class Panel extends JPanel
	{
		
		double[] Erg_Einsw = new double[Punkte];
		cl_EinSchwing EinSw = new cl_EinSchwing();
		class Rechenthread extends Thread
		{
			public void run()
			{
				for(int z = 0; z < Punkte-1; z++)
				{
					
					EinSw.EinS_Rech(z, t0, w);
					
					Erg_Einsw[z+1]= (double) EinSw.nq;
					
				}
				this.stop();
			}
		}
		public void paint(Graphics g)
		{
			double skali = 0.45;
	    	int ym = getHeight();
	        int xm = getWidth();
	        double zt;
	    	if (offscreen == null){
		    	offscreen  = createImage(getWidth(),getHeight());}
			Buffergrafik = offscreen.getGraphics(); 
		   	Buffergrafik.clearRect(0, 0, getWidth(), getHeight());
		   	Buffergrafik.drawRect(1, 2, xm-2, ym-3);
		   	Buffergrafik.setColor(Color.GRAY);
		   	Buffergrafik.drawLine(20, (int) ym-10, (int)t0*10+20, (int) (ym-ym*skali-10));
    		Buffergrafik.drawLine((int)t0*10+20, (int)(ym- ym*skali-10), xm,(int )(ym-ym*skali-10));
				
			Buffergrafik.setColor(Color.DARK_GRAY);
		   	Buffergrafik.drawLine(20, ym, 20,2);
		   	Buffergrafik.drawLine(1, ym-10, xm, ym-10);
		   	Thread thEins = new Rechenthread();
	        thEins.run();
	        double yskal = skali*getHeight();
	        
	        double f = 0.2;
	        Buffergrafik.setColor(Color.RED);
	        Buffergrafik.drawString("t/t0", 490, 410);
	        Buffergrafik.drawString("q/q_st", 25, 15);
	        for (double i = 1; i <11; i++)
	        {
	        	
	        	f = f*100;
				f = Math.round(f);
				f = f/100;
				Buffergrafik.setColor(Color.RED);
	           	Buffergrafik.drawString(String.valueOf(f), 2, (int)(ym-(i*ym/10)*skali*2)-12);
	           	Buffergrafik.setColor(Color.DARK_GRAY);
	           	Buffergrafik.drawLine(8, (int)(ym-(i*ym/10)*skali*2)-10, 20, (int)(ym-(i*ym/10)*skali*2)-10);
	           
	           	f = f + 0.2;
	        }
	        
	       
	 	     for (int z = 0; z < Punkte-1; z++)
		        {
	 	    	    Buffergrafik.setColor(Color.BLUE);
	 	    	    
		        	Buffergrafik.drawLine( 20+ z*5,(int)(ym- 10- Erg_Einsw[z]*yskal ), 20+ (z +1)*5,(int)  (ym-10-(Erg_Einsw[z+1]*yskal)));
		        	zt = (double) z/t0;
		        	if (zt % 5 == 0)
		        	{
		        		
		        		Buffergrafik.setColor(Color.red);
		        		Buffergrafik.drawString(String.valueOf(zt/5), z*2+20, ym-15);
		        		Buffergrafik.setColor(Color.DARK_GRAY);
			           	Buffergrafik.drawLine(z*2+20, ym-3,z*2+20 ,ym-10);
		              
		        	}
		        }
	 	    
	 	     double Verhae = t0*w;
	 	     Verhae = Verhae*100;
	 	     Verhae = Math.round(Verhae);
	 	     Verhae = Verhae/100;
	 	     Buffergrafik.setColor(Color.black);
	 	     Buffergrafik.drawString("t0*ω", 400, 330);
	 	     Buffergrafik.drawString(String.valueOf(Verhae), 400, 350);
	  	     if (offscreen != null){
	  	    	 
	  	    	 g.drawImage(offscreen, 0, 0, this);}
		}
		public void upadate(Graphics g)
		{
			paint(g);
		}
	}
	public void init()
	{
		setLayout(null);
		
		Panel SwPanel = new Panel();
		SwPanel.setBounds(20, 40, 520, 440);
		add(SwPanel);
		
		tx_t0 = new TextField();
		tx_t0.setBounds(550, 300, 45, 20);
		tx_t0.setText(String.valueOf(t0));
		tx_t0.disable();
		add(tx_t0);
		
		Label l_t0 = new Label("t0");
		l_t0.setBounds(560, 60, 40, 14);
		add(l_t0);
		
		Label l_w = new Label("ω");
		l_w.setBounds(600, 60, 120, 14);
		add(l_w);
		
		sb_t0 = new Scrollbar(Scrollbar.VERTICAL, 0,1,1,40);
		sb_t0.setBounds(560, 80, 20, 200);
		sb_t0.setValue(5);
		add(sb_t0);
		class t0Change implements AdjustmentListener{
			    public void adjustmentValueChanged(AdjustmentEvent ae){
			    t0 = sb_t0.getValue();
			    repaint();
				tx_t0.setText(String.valueOf(t0)); 
			    }
			  }
		sb_t0.addAdjustmentListener(new t0Change());
		
		tx_w = new TextField();
		tx_w.setBounds(600, 300, 45, 20);
		tx_w.setText(String.valueOf(w));
		tx_w.disable();
		add(tx_w);
		
		sb_w = new Scrollbar(Scrollbar.VERTICAL, 0,1,1,100);
		sb_w.setBounds(600, 80, 20, 200);
		sb_w.setValue(50);
		add(sb_w);
		class wChange implements AdjustmentListener{
			    public void adjustmentValueChanged(AdjustmentEvent ae){
			    w = (double) sb_w.getValue()/50;
			    repaint();
				tx_w.setText(String.valueOf(w)); 
			    }
			  }
		sb_w.addAdjustmentListener(new wChange());
	}

}
