// --------------------------------------------------------------------
// Author: Maciej Dziemianczuk
// www.faces-of-nature.art.pl/
// maciek.ciupa at gmail com
//



import java.awt.*;
import java.applet.Applet;


//---------------------------------------------------------------------
class CGraph extends java.applet.Applet
{
	int WIDTH;
	int HEIGHT;
	
	int OX = 20;
	int OY = 340;
	
	int ITERATIONS;
	
	double kA, kB, xA, xB;
	
	//-----------------------------------------------------------------
	public CGraph(int _width, int _height, double _kA, double _kB, double _xA, double _xB, int _iter)
	{
		WIDTH = _width;
		HEIGHT = _height;
		kA = _kA;
		kB = _kB;
		xA = _xA;
		xB = _xB;
		ITERATIONS = _iter;
	}
	
	//-----------------------------------------------------------------
	public void paint ( Graphics g )
	{        
		Rectangle r = getBounds();
		g.clearRect( 0, 0, r.width, r.height );
		g.setColor( Color.black );
		
		Font ft = new Font ( "Tahoma", Font.PLAIN, 10 );
        g.setFont (ft);
		
		// X axis
		g.drawLine(OX - 3, OY, OX + WIDTH + 10, OY); 
		g.drawLine(OX + WIDTH + 10, OY,  OX + WIDTH + 5, OY+3);
		g.drawLine(OX + WIDTH + 10, OY,  OX + WIDTH + 5, OY-3);
		g.drawLine(OX + WIDTH, OY-3, OX + WIDTH, OY+3); 
        g.drawString ("k = " + Double.toString(kB), OX + WIDTH - 30, OY + 13 );
		g.drawString (Double.toString(kA), OX - 5, OY + 13 );
        
		
		// Y asix
		g.drawLine(OX, OY + 3, OX, OY - HEIGHT - 10);
		g.drawLine(OX, OY - HEIGHT - 10, OX+3, OY - HEIGHT - 5);
		g.drawLine(OX, OY - HEIGHT - 10, OX-3, OY - HEIGHT - 5);
		g.drawLine(OX - 3, OY - HEIGHT, OX + 3, OY - HEIGHT);
		
		g.drawString ("x", OX - 12, OY - HEIGHT + 10 );
		g.drawString (Double.toString(1.0/xA - xB), OX + 10, OY - HEIGHT - 7 );
		if (xB == 0.0)
			g.drawString ("0.0", OX - 16, OY - 2);
		else
			g.drawString (Double.toString(-xB), OX - 16, OY - 2);
		
		drawBifur( g );
	}
	
	//-----------------------------------------------------------------
	void drawBifur ( Graphics g )
	{
		//g.setColor(Color.red);
		
		// - - - - - - - - - 
		// x --> kx(1-x)
		
		double k = kA;
		double x = 0.5;
		double dk = (kB - kA)/(double)(WIDTH);
		double dx = xA*HEIGHT;
		//xB -= 0.05;
		int X,Y;
		
		for (int ik=0; ik < WIDTH; ++ik)
		{
			x = 0.5;
			// begin iterations to pass
			for (int iP = 0; iP < 1000; ++iP)
			{
				x = k * x * (1 - x);
			}
			
			// next iterations to show on the screen
			for (int iP = 0; iP < ITERATIONS; ++iP)
			{
				x = k * x * (1 - x);
				
				Y = (int)(OY - (x + xB)*dx );
				X = OX + ik;
				if (X >= 0 && X <= WIDTH+OX && Y >= OY - HEIGHT && Y <= OY)
					g.drawLine(X, Y, X, Y);
			}
			
			
			k = k + dk;
			
		}
		
		
		
	}
	
};

//---------------------------------------------------------------------
//---------------------------------------------------------------------
public class CLogistic extends Applet
{
	Choice kL, nL, sequenceL;
	TextField tf1,  tf2, tf3, tf4, tf5;
	Button but1;
	
	CGraph tablet;
	int K, N, Seq;
	int WIDTH = 480;
	int HEIGHT = 320;
	double xA = 1.0, xB = 0.0;
	double kA = 2.7, kB = 4;
	int ITERATIONS = 300;
	
	//-----------------------------------------------------------------
	public void init()
	{        
		//getSize().width
		tablet = new CGraph(WIDTH, HEIGHT, kA, kB, xA, xB, ITERATIONS);
		
		tf1 = new TextField(Double.toString(kA), 2);
		tf2 = new TextField(Double.toString(kB), 2);
		tf3 = new TextField(Double.toString(xA), 2);
		tf4 = new TextField(Double.toString(xB), 2);
		tf5 = new TextField(Integer.toString(ITERATIONS), 2);
		but1 = new Button("Draw");
		
		
		kL = new Choice();
		nL = new Choice();
		sequenceL = new Choice();
		
		
		for (int i=0; i<=11; i++)
		{
			kL.addItem( new Integer(i).toString());
			nL.addItem( new Integer(i).toString());
		}
		
		
		setLayout( new BorderLayout() );
		
		Panel sPanel = new Panel();
		Panel nPanel = new Panel();
		
		sPanel.setLayout( new FlowLayout() );
		
		// Labels  in South panel
		
		Label L1 = new Label("K:");
		L1.setBackground(Color.gray);
		sPanel.add( L1 );
		sPanel.add( tf1 );
		L1 = new Label(" -");
		L1.setBackground(Color.gray);
		sPanel.add( L1 );
		sPanel.add( tf2 );
		
		L1 = new Label("Zoom:");
		L1.setBackground(Color.gray);
		sPanel.add( L1 );
		sPanel.add( tf3);
		L1 = new Label("Height:");
		L1.setBackground(Color.gray);
		sPanel.add( L1 );
		sPanel.add( tf4);
		L1 = new Label("Iter.:");
		L1.setBackground(Color.gray);
		sPanel.add( L1 );
		sPanel.add( tf5 );
		
		sPanel.add( but1 );
		
		// Labels and others in north panel
		L1 = new Label("Wykres bifurkacyjny odwzorowania logistycznego x ---> k*x*(1-x)");
		
		Font ft = new Font ( "Tahoma", Font.BOLD, 12 );
		L1.setFont(ft);
		
		L1.setBackground(Color.white);
		nPanel.add( L1 );
		//sPanel.add( sequenceL );
		
		
		
		
		add( "North", nPanel );
		add( "South", sPanel );
		add( "Center", tablet );
		

		tablet.setBackground(Color.white);
		sPanel.setBackground(Color.gray);
		nPanel.setBackground(Color.white);
		
		
		//but1.addActionListener(itemStateChanged);
		
	}
	
	public boolean action(Event e, Object arg)
	{
		
		if (e.target == but1)
		{
			
			kA = Double.parseDouble( tf1.getText() );
			kB = Double.parseDouble( tf2.getText() );
			xA = Double.parseDouble( tf3.getText() );
			xB = Double.parseDouble( tf4.getText() );
			ITERATIONS = Integer.parseInt( tf5.getText() );
			
		}
		
		remove( tablet );
		tablet = new CGraph(WIDTH, HEIGHT, kA, kB, xA, xB, ITERATIONS);
		tablet.setBackground(Color.white);
		add( "Center", tablet );
		validate();
		
		return true;
	}
	
};
