// P1103.java:$BH*@/5A$N<LA|$K$h$k%U%i%/%?%k?^7A(B

package applet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Glib.*;

public class P1103 extends xApplet
	implements ActionListener
{	JComboBox cb;
	JButton[]bt=new JButton[3];
	String[]str={"Start","Clear","Exit"};
	xGraphics G;
	int k=800,N;
	xComplex a=new xComplex(0.0,0.0);
	xComplex b=new xComplex(1.0/2,1.0/3);
	xComplex c=new xComplex(0.0,0.0);
	xComplex d=new xComplex(2.0/3,0.0);
//	xComplex a=new xComplex(2.0/3,1.0/5);
//	xComplex b=new xComplex(0.0,0.0);
//	xComplex c=new xComplex(0.0,0.0);
//	xComplex d=new xComplex(2.0/3,0.0);
	xComplex z0=new xComplex(1.0,0.0);

	public void init()
	{	app_init(7);
		Container cp=getContentPane();
		cp.setBackground(getBackground());
		cp.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel lb=new JLabel("$B:F5"<!?t!'(BN=");
		lb.setForeground(Color.black);
		cp.add(lb);
		cb=new JComboBox();
		cb.setEditable(false);
		for(int i=0;i<18;i++)
			cb.addItem(Integer.toString(i));
		cb.addActionListener(this);
		cp.add(cb);
		for(int i=0;i<str.length;i++)
		{	bt[i]=new JButton(str[i]);
			bt[i].addActionListener(this);
			cp.add(bt[i]);
		}
		G=new xGraphics(getGraphics());
		G.set0(CA.x-k/2,CA.y,1);
	}

	public void actionPerformed(ActionEvent evt)
	{	if(evt.getSource()==cb)
			N=cb.getSelectedIndex();
		if(evt.getSource()==bt[0])
		{	z_map(z0,0);
			G.textout(10,60,"a=("+xMath.fdouble(a.x,3)+
				")+("+xMath.fdouble(a.y,3)+")i",0,16);
			G.textout(10,80,"b=("+xMath.fdouble(b.x,3)+
				")+("+xMath.fdouble(b.y,3)+")i",0,16);
			G.textout(10,100,"c=("+xMath.fdouble(c.x,3)+
				")+("+xMath.fdouble(c.y,3)+")i",0,16);
			G.textout(10,120,"d=("+xMath.fdouble(d.x,3)+
				")+("+xMath.fdouble(d.y,3)+")i",0,16);
		}
		if(evt.getSource()==bt[1])
			repaint();
		if(evt.getSource()==bt[2])
			System.exit(0);
	}

	private void z_map(xComplex z,int n)
	{	xComplex z_=new xComplex(z.x,-z.y);
		xComplex C1=new xComplex(1.0,0.0);
		xComplex z1,z2;
		G.putpixel(xMathC.fint(xMathC.mlt(k,z)),0);
		if(n<N)
		{	z1=xMathC.plus(xMathC.mlt(a,z),xMathC.mlt(b,z_));
			z2=xMathC.plus(xMathC.mlt(c,(xMathC.minus(z,C1))),
				xMathC.mlt(d,(xMathC.minus(z_,C1))),C1);
			z_map(z1,n+1);
			z_map(z2,n+1);
		}
	}
}
