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

public class fractal extends Applet
{
	Panel Trans, Resol, Barra, Angle, Cont, T, Enter, Start, R, Nuevo,Gal;
	Ventana v;
	TextField ang, c, tx, ty;
	Button enter, start, nuevo;
	Choice r, cuad,gal;
	int res=0, counter=0, it=0, lado=50;
	double[][] trans=new double[4][8];
	int[][] figura=new int[2][4];
	boolean pressed=false,
		error1=false,
		error2=false,
		error3=false,
		error4=false;
	
	public void init()
	{
		setBackground(Color.black);
		setLayout(new BorderLayout());
		
		v=new Ventana();
		
		Barra=new Panel();
		Barra.setLayout(new GridLayout(1,2));
		Barra.setBackground(new Color(150,150,200));
		
		Trans=new Panel();
		Trans.setLayout(new GridLayout(4,1));
		
		Resol=new Panel();
		Resol.setLayout(new GridLayout(4,1));
		
		Angle=new Panel();
		Cont=new Panel();
		T=new Panel();
		Enter=new Panel();
		Start=new Panel();
		R=new Panel();
		Nuevo=new Panel();
		Gal=new Panel();
		
		ang=new TextField(5);
		c=new TextField(5);
		tx=new TextField(5);
		ty=new TextField(5);
		r=new Choice();
		cuad=new Choice();
		gal=new Choice();
		ang.setBackground(Color.white);
		c.setBackground(Color.white);
		tx.setBackground(Color.white);
		ty.setBackground(Color.white);
		r.setBackground(Color.white);
		cuad.setBackground(Color.white);
		enter=new Button("Entrar transformación");
		start=new Button("Dibujar fractal");
		nuevo=new Button("Nuevo fractal");
		nuevo.addActionListener(new MyButton());
		enter.addActionListener(new MyButton());
		start.addActionListener(new MyButton());
		r.addItemListener(new MyChoice());
		start.setEnabled(false);
		cuad.addItemListener(new MyChoice());
		gal.addItemListener(new MyChoice());
				
		r.add("0");
		r.add("1");
		r.add("2");
		r.add("3");
		r.add("4");
		r.add("5");
		r.add("6");
		r.add("7");
		r.add("8");
		r.add("9");
		r.add("10");
		
		cuad.add("50");
		cuad.add("100");
		cuad.add("150");
		cuad.add("200");
		cuad.add("250");
		cuad.add("300");

		gal.add("Hoja");
		gal.add("Sierpinski");
		gal.add("Monito");
		gal.add("Monito2");
		gal.add("Esponja de Menger");
						
		Angle.add(new Label("Ángulo:"));
		Angle.add(ang);
		Angle.add(new Label("°"));
		
		Cont.add(new Label("Factor de contracción:"));
		Cont.add(c);
		
		T.add(new Label("Translación:"));
		T.add(new Label("("));
		T.add(tx);
		T.add(new Label(","));
		T.add(ty);
		T.add(new Label(")"));
		
		Enter.add(enter);
		R.add(new Label("Resolución:"));
		R.add(r);
		Start.add(start);
		Nuevo.add(nuevo);
		R.add(new Label("Lado del cuadrado"));
		R.add(cuad);
		Gal.add(new Label("Ejemplos de fractales chidos:"));
		Gal.add(gal);
		
		Trans.add(Angle);
		Trans.add(Cont);
		Trans.add(T);
		Trans.add(Enter);
		
		Resol.add(Nuevo);
		Resol.add(R);
		Resol.add(Gal);
		Resol.add(Start);
		
		Barra.add(Trans);
		Barra.add(Resol);
		
		add("North",Barra);
		add("Center",v);
		
		figura[0][0]=0;   figura[1][0]=0;
		figura[0][1]=0;   figura[1][1]=50;
		figura[0][2]=50; figura[1][2]=50;
		figura[0][3]=50; figura[1][3]=0;
	}
	
	class MyButton implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			error1=false;
			error2=false;
			error3=false;
			error4=false;
				
			if (e.getSource()==enter)
			{
				try{new Double(ang.getText()).doubleValue();}
				catch(NumberFormatException ex)
				{
					error1=true;
					ang.setText("");
					v.repaint();
				}
				try{new Double(c.getText()).doubleValue();}
				catch(NumberFormatException ex)
				{
					error2=true;
					c.setText("");
					v.repaint();
				}
				try{new Double(tx.getText()).doubleValue();}
				catch(NumberFormatException ex)
				{
					error3=true;
					tx.setText("");
					v.repaint();
				}
				try{new Double(ty.getText()).doubleValue();}
				catch(NumberFormatException ex)
				{
					error4=true;
					ty.setText("");
					v.repaint();
				}
				
				if (!error1 && !error2 && !error3 && !error4)
				{
					if ((new Double(c.getText()).doubleValue())>=1 || (new Double(c.getText()).doubleValue())<0)
					{
						error2=true;
						c.setText("");
						v.repaint();
					}
					if (Math.abs((new Double(tx.getText()).doubleValue()))>200)
					{
						error3=true;
						tx.setText("");
						v.repaint();
					}
					if (Math.abs((new Double(ty.getText()).doubleValue()))>200)
					{
						error4=true;
						ty.setText("");
						v.repaint();
					}
				}					
					
				if (!error1 && !error2 && !error3 && !error4)
				{
					trans[0][counter]=((new Double(ang.getText()).doubleValue()))*Math.PI/180;
					trans[1][counter]=(new Double(c.getText()).doubleValue());
					trans[2][counter]=(int) ((new Double(tx.getText()).doubleValue()));
					trans[3][counter]=(int) ((new Double(ty.getText()).doubleValue()));
					ang.setText("");
					c.setText("");
					tx.setText("");
					ty.setText("");
					counter++;
					if (counter==1)
						start.setEnabled(true);
					if (counter>5)
						enter.setEnabled(false);
					v.repaint();
				}
			}
			if (e.getSource()==start)
			{
				pressed=true;
				v.repaint();
			}
			if (e.getSource()==nuevo)
			{
				counter=0;
				pressed=false;
				start.setEnabled(false);
				enter.setEnabled(true);
				v.repaint();
			}
		}
	}
	
	class MyChoice implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			if (e.getSource()==r)
			{
				if (e.getItem()=="0")
					res=0;
				if (e.getItem()=="1")
					res=1;
				if (e.getItem()=="2")
					res=2;
				if (e.getItem()=="3")
					res=3;
				if (e.getItem()=="4")
					res=4;
				if (e.getItem()=="5")
					res=5;
				if (e.getItem()=="6")
					res=6;
				if (e.getItem()=="7")
					res=7;
				if (e.getItem()=="8")
					res=8;
				if (e.getItem()=="9")
					res=9;
				if (e.getItem()=="10")
					res=10;
			}
			if (e.getSource()==cuad)
			{
				if (e.getItem()=="50")
				{
					figura[0][1]=0;   figura[1][1]=50;
					figura[0][2]=50; figura[1][2]=50;
					figura[0][3]=50; figura[1][3]=0;
				}
				if (e.getItem()=="100")
				{
					figura[0][1]=0;   figura[1][1]=100;
					figura[0][2]=100; figura[1][2]=100;
					figura[0][3]=100; figura[1][3]=0;
				}if (e.getItem()=="150")
				{
					figura[0][1]=0;   figura[1][1]=150;
					figura[0][2]=150; figura[1][2]=150;
					figura[0][3]=150; figura[1][3]=0;
				}if (e.getItem()=="200")
				{
					figura[0][1]=0;   figura[1][1]=200;
					figura[0][2]=200; figura[1][2]=200;
					figura[0][3]=200; figura[1][3]=0;
				}if (e.getItem()=="250")
				{
					figura[0][1]=0;   figura[1][1]=250;
					figura[0][2]=250; figura[1][2]=250;
					figura[0][3]=250; figura[1][3]=0;
				}if (e.getItem()=="300")
				{
					figura[0][1]=0;   figura[1][1]=300;
					figura[0][2]=300; figura[1][2]=300;
					figura[0][3]=300; figura[1][3]=0;
				}
			}
			if (e.getSource()==gal)
			{
				if (e.getItem()=="Hoja")
				{
					enter.setEnabled(false);
					start.setEnabled(true);
					counter=4;
					trans[0][0]=0;           trans[1][0]=0.75;   trans[2][0]=0;    trans[3][0]=0;
					trans[0][1]=0;           trans[1][1]=0.75;   trans[2][1]=0;    trans[3][1]=100;
					trans[0][2]=-Math.PI/3;  trans[1][2]=0.5;    trans[2][2]=0;    trans[3][2]=0;
					trans[0][3]=Math.PI/3;   trans[1][3]=0.5;    trans[2][3]=0;    trans[3][3]=0;
				}
				if (e.getItem()=="Sierpinski")
				{
					enter.setEnabled(false);
					start.setEnabled(true);
					counter=3;
					trans[0][0]=0;           trans[1][0]=0.5;    trans[2][0]=0;    trans[3][0]=0;
					trans[0][1]=0;           trans[1][1]=0.5;    trans[2][1]=0;    trans[3][1]=100;
					trans[0][2]=0;           trans[1][2]=0.5;    trans[2][2]=100;    trans[3][2]=0;
				}
				if (e.getItem()=="Monito2")
				{
					enter.setEnabled(false);
					start.setEnabled(true);
					counter=4;
					trans[0][0]=0;           trans[1][0]=0.5;   trans[2][0]=0;    trans[3][0]=0;
					trans[0][1]=0;           trans[1][1]=0.5;   trans[2][1]=0;    trans[3][1]=100;
					trans[0][2]=Math.PI/2;  trans[1][2]=0.5;    trans[2][2]=200;    trans[3][2]=0;
					trans[0][3]=Math.PI/3;   trans[1][3]=0.5;    trans[2][3]=0;    trans[3][3]=0;
				}
				if (e.getItem()=="Monito")
				{
					enter.setEnabled(false);
					start.setEnabled(true);
					counter=3;
					trans[0][0]=0;           trans[1][0]=0.5;   trans[2][0]=0;    trans[3][0]=0;
					trans[0][1]=0;           trans[1][1]=0.5;   trans[2][1]=100;    trans[3][1]=0;
					trans[0][2]=Math.PI/2;  trans[1][2]=0.5;    trans[2][2]=0;    trans[3][2]=200;
				}
				if (e.getItem()=="Esponja de Menger")
				{
					enter.setEnabled(false);
					start.setEnabled(true);
					counter=8;					
					trans[0][0]=0;   trans[1][0]=(1.0/3);    trans[2][0]=0;             trans[3][0]=0;
					trans[0][1]=0;   trans[1][1]=(1.0/3);    trans[2][1]=200*(1.0/3);   trans[3][1]=0;
					trans[0][2]=0;   trans[1][2]=(1.0/3);    trans[2][2]=400*(1.0/3);   trans[3][2]=0;
					trans[0][3]=0;   trans[1][3]=(1.0/3);    trans[2][3]=0;             trans[3][3]=200*(1.0/3);
					trans[0][4]=0;   trans[1][4]=(1.0/3);    trans[2][4]=0;             trans[3][4]=400*(1.0/3);
					trans[0][5]=0;   trans[1][5]=(1.0/3);    trans[2][5]=200*(1.0/3);   trans[3][5]=400*(1.0/3);
					trans[0][6]=0;   trans[1][6]=(1.0/3);    trans[2][6]=400*(1.0/3);   trans[3][6]=200*(1.0/3);
					trans[0][7]=0;   trans[1][7]=(1.0/3);    trans[2][7]=400*(1.0/3);   trans[3][7]=400*(1.0/3);					
				}
				
			
			}
			
				
							
		}
	}
	
	public void iteracion(Graphics g, int[][] fig)
	{
		int [][] aux=new int[fig.length][fig[0].length];
		
		if (it<res)
		{
			for (int i=0; i<counter; i++)
			{
				for (int j=0; j<fig[0].length; j++)
				{
					aux[0][j]=(int) (trans[1][i]*(Math.cos(trans[0][i])*fig[0][j]+Math.sin(trans[0][i])*fig[1][j]) + trans[2][i]);
					aux[1][j]=(int) (trans[1][i]*(-Math.sin(trans[0][i])*fig[0][j]+Math.cos(trans[0][i])*fig[1][j]) + trans[3][i]);
				}
				it++;
				iteracion(g,aux);
				it--;
			}
		}
		else
		{
			Polygon p= new Polygon();
			for (int i=0; i<fig[0].length; i++)
				p.addPoint(fig[0][i]+200,fig[1][i]+200);
			g.fillPolygon(p);
		}
	}
		
	class Ventana extends Canvas
	{
		public void paint(Graphics g)
		{
			g.setColor(Color.red);
			if (error1)
				g.drawString("El ángulo (en grados) debe ser un número real",150,50);
			if (error2)
				g.drawString("El factor de contracción debe ser un número real entre 0 y 1",150,75);
			if (error3)
				g.drawString("La traslación en ´x´ debe ser un número real entre -200 y 200",150,100);
			if (error4)
				g.drawString("La traslación en ´y´ debe ser un número real entre -200 y 200",150,125);
			if (pressed)
			{
				iteracion(g,figura);
			}
		}	
	}
}

Volver a introducción