lunes, 21 de marzo de 2011

ALGORITMO CONGRUENCIAL ADITIVO


/* Algoritmo Congruencial Aditivo
 *
 * Este algoritmo requiere una secuencia previa de numeros enteros, X1, X2, X3,...,Xn. para generar una secuencia nueva
 * de numeros enteros que emieza en Xn+1, Xn+2,...
 * Su ecuacion recursiva es:
 * Xi=(Xi-1 + Xi-n) mod (M)
 *
 * i=n+1, n+2, n+3, ..., N.
 *
 * Los primeros ri pueden ser generados mediante la ecuacion:
 * ri=Xi/(M-1)
 *
 *
 * Fuente:   .....------          Conocimientos adquiridos e investigacion en distintas fuentes          ------.....

                                      *********************************************
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  SiMuLaCiOn                               *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

class AlgoritmoCongruencialAditivo extends JFrame
{
private Container contenedor;
private GridBagLayout esquema;
private GridBagConstraints restricciones;
JLabel l1, l2, l3, l4, l5, l6;
JButton b1,b2;
JTextField t1,t2, t3, t4, t5, t6;
JTextArea a1, a2, a3;
int numero=0, valor1=0, veces=0;
String valor, ccuadrado="", sale="", cua="", erre="", algo="", lfinal="", rtot="";
LinkedList lista=new LinkedList();
int n, num1, num2, ta1=0, ta2=0, ta3=0;
double y;

public AlgoritmoCongruencialAditivo()
{
setSize(350,350);
setTitle("Algoritmo Congruencial Aditivo");
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(HIDE_ON_CLOSE);

contenedor=getContentPane();
esquema=new GridBagLayout();
contenedor.setLayout(esquema);

//**********************************************************
//- Permite ingresar imagen de fondo.              -
//----------------------------------------------------------
((JPanel)getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("16.jpg");
JLabel fondo = new JLabel(img);
fondo.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
getLayeredPane().add(fondo,JLayeredPane.FRAME_CONTENT_LAYER);
//----------------------------------------------------------

restricciones=new GridBagConstraints();
restricciones.insets=new Insets(5,5,5,5);

restricciones.fill=GridBagConstraints.BOTH;

l1=new JLabel("Algoritmo Congruencial Aditivo");
l1.setFont (new Font ("Arial", Font.BOLD, 25));
         l1.setForeground (Color.BLACK);
         establecerValores(0,0,4,1);
         restricciones.fill=GridBagConstraints.BOTH;
         esquema.setConstraints(l1,restricciones);
         contenedor.add(l1);
        
//----- Cantidad de datos a ingresar -----
l2=new JLabel("Cantidad de datos a ingresar:");
l2.setFont(new Font("Bell MT",Font.PLAIN,14));
l2.setForeground(Color.BLUE);
establecerValores(0,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l2,restricciones);
contenedor.add(l2);

t1=new JTextField(10);
t1.setToolTipText("Escribe Numero");
t1.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 3, Color.BLACK));
establecerValores(2,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t1,restricciones);
contenedor.add(t1);

//----- Valor del modulo -----
l3=new JLabel("Valor del modulo:");
l3.setFont(new Font("Bell MT",Font.PLAIN,14));
l3.setForeground(Color.BLUE);
establecerValores(0,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l3,restricciones);
contenedor.add(l3);

t2=new JTextField(10);
t2.setToolTipText("Escribe Numero");
t2.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 3, Color.BLACK));
establecerValores(2,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t2,restricciones);
contenedor.add(t2);

//----- Cantidad de numeros a generar -----
l4=new JLabel("Cantidad de numeros a generar:");
l4.setFont(new Font("Bell MT",Font.PLAIN,14));
l4.setForeground(Color.BLUE);
establecerValores(0,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

t3=new JTextField(10);
t3.setToolTipText("Escribe Numero");
t3.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 3, Color.BLACK));
establecerValores(2,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t3,restricciones);
contenedor.add(t3);

b1=new JButton("HaCeR");
b1.setToolTipText("Clic Para Convertir");
b1.setBorder(new EmptyBorder(10,15,10,15));
b1.setMnemonic(KeyEvent.VK_H);
b1.setFont(new Font("Bell MT",Font.PLAIN,14));
b1.setForeground(Color.RED);
establecerValores(1,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b1,restricciones);
contenedor.add(b1);

b2=new JButton("LiMpIaR");
b2.setToolTipText("Clic Para Limpiar Datos");
b2.setBorder(new EmptyBorder(10,15,10,15));
b2.setMnemonic(KeyEvent.VK_L);
b2.setFont(new Font("Bell MT",Font.PLAIN,14));
b2.setForeground(Color.RED);
establecerValores(3,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b2,restricciones);
contenedor.add(b2);

l4=new JLabel("Numeros Enteros");
l4.setFont(new Font("Bell MT",Font.PLAIN,14));
l4.setForeground(Color.BLUE);
establecerValores(1,8,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

a1=new JTextArea(2,15);
a1.setEditable(false);
a1.setBorder(BorderFactory.createMatteBorder(1, 2, 1, 1, Color.BLACK));
establecerValores(1,9,3,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(a1,restricciones);
contenedor.add(a1);

l5=new JLabel("Xi");
l5.setFont(new Font("Bell MT",Font.PLAIN,14));
l5.setForeground(Color.BLUE);
establecerValores(2,10,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l5,restricciones);
contenedor.add(l5);

a2=new JTextArea(10,6);
a2.setEditable(false);
a2.setBorder(BorderFactory.createMatteBorder(1, 2, 1, 1, Color.BLACK));
establecerValores(2,11,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(a2,restricciones);
contenedor.add(a2);

l6=new JLabel("Ri");
l6.setFont(new Font("Bell MT",Font.PLAIN,14));
l6.setForeground(Color.BLUE);
establecerValores(3,10,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l6,restricciones);
contenedor.add(l6);

a3=new JTextArea(10,6);
a3.setEditable(false);
a3.setBorder(BorderFactory.createMatteBorder(1, 2, 1, 1, Color.BLACK));
establecerValores(3,11,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(a3,restricciones);
contenedor.add(a3);

ManejadorCamposTexto m=new ManejadorCamposTexto();
b1.addActionListener(m);
b2.addActionListener(m);

pack();
}

//********************************************************************************************
public void establecerValores(int columna,int fila, int anchura, int altura)
{
restricciones.gridx=columna;
restricciones.gridy=fila;

restricciones.gridwidth=anchura;
restricciones.gridheight=altura;
}
//*******************************************************************************************

public static void main(String args[])
{
new AlgoritmoCongruencialAditivo();
}

private class ManejadorCamposTexto implements ActionListener
{
public void actionPerformed(ActionEvent evento)
{
if (evento.getSource()==b1)
{
if(t1.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Ingresa numero de datos deseados");
}

if(t2.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Ingresa modulo");
}

if(t3.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Ingresa cantidad de datos a generar");
}

else
{

try
{
int datos=Integer.parseInt(t1.getText());
int modulo=Integer.parseInt(t2.getText());
int cantidad=Integer.parseInt(t3.getText());

if(datos<=0)
{
JOptionPane.showMessageDialog(null,"El numero de datos debe ser \nmayorr a 0","error",JOptionPane.ERROR_MESSAGE);
t1.setText("");
}

else
{

if(modulo<=0)
{
JOptionPane.showMessageDialog(null,"El numero modulo debe ser \nmayor a 0","error",JOptionPane.ERROR_MESSAGE);
t2.setText("");
}

else
{

if(cantidad<=0)
{
JOptionPane.showMessageDialog(null,"El numero de datos debe ser \nmayor a 0","error",JOptionPane.ERROR_MESSAGE);
t3.setText("");
}

else
{

for(int i=0; i<datos; i++)
{

n=Integer.parseInt(JOptionPane.showInputDialog("Ingresa dato "+(i+1)+" : "));
lista.addLast(n);
sale+=n+" ";

ta1++;
}

for(int i=0; i<cantidad; i++)
{
String dat1=String.valueOf(lista.get(datos-1+i));
int da1=Integer.parseInt(dat1);

String dat2=String.valueOf(lista.get(i));
int da2=Integer.parseInt(dat2);

ta2++;
ta3=ta1+ta2;

n=(da1+da2)% modulo;
lista.addLast(n);
lfinal+="x"+ta3+":  "+n+"\n";

y=n*1.0/(modulo-1);
String yt=String.format("%.5f",y);
rtot+="r"+i+": "+yt+"\n";
}

a1.setText(sale);
a2.setText(lfinal);
a3.setText(rtot);


}
}
}
}

catch(java.lang.NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Dato invalido");
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");


}

}

}

if(evento.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
a1.setText("");
a2.setText("");
a3.setText("");

//----- Permite vaciar el arreglo -----

if(sale != null && lfinal != null && rtot != null)
{
sale="";
lfinal="";
rtot="";
}

lista.clear();

}

}
}


}

ALGORITMO CONGRUENCIAL MULTIPLICATIVO


/* Algoritmo Congruencial Multiplicativo
 *
 * Fuente:   .....------          Conocimientos adquiridos e investigacion en distintas fuentes          ------.....

                                      *********************************************
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ             *
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS            *
                                      *  SiMuLaCiOn                                                                *
                                      *  ErNeStO rOsAlEs CaRlOs                                          *
                                      *  ernestorosales@live.com                                              *
                                      *********************************************
*/


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class AlgoritmoCongruencialMultiplicativo extends JFrame
{
private Container contenedor;
private GridBagLayout esquema;
private GridBagConstraints restricciones;
JLabel l1, l2, l3, l4, l5, l6;
JButton b1,b2;
JTextField t1,t2, t3, t4, t5, t6;
JTextArea a1, a2, a3;
int numero=0, valor1=0, veces=0, cantidad=0, x=0, k=0, g=0, m=1, num=2, a=0;
String valor, ccuadrado="", sale="", cua="", erre="";
String cero="0.";
int[] v;
double[] r;

public AlgoritmoCongruencialMultiplicativo()
{
setSize(350,350);
setTitle("Algoritmo Congruencial Multiplicativo");
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(HIDE_ON_CLOSE);

contenedor=getContentPane();
esquema=new GridBagLayout();
contenedor.setLayout(esquema);

//**********************************************************
//- Permite ingresar imagen de fondo.              -
//----------------------------------------------------------
((JPanel)getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("3.jpg");
JLabel fondo = new JLabel(img);
fondo.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
getLayeredPane().add(fondo,JLayeredPane.FRAME_CONTENT_LAYER);
//----------------------------------------------------------

restricciones=new GridBagConstraints();
restricciones.insets=new Insets(5,5,5,5);

restricciones.fill=GridBagConstraints.BOTH;

l1=new JLabel("Algoritmo Congruencial Multiplicativo");
l1.setFont (new Font ("Berlin Sans FB", Font.BOLD, 30));
         l1.setForeground (Color.DARK_GRAY);
         establecerValores(0,0,4,1);
         restricciones.fill=GridBagConstraints.BOTH;
         esquema.setConstraints(l1,restricciones);
         contenedor.add(l1);

// ----- El valor de Xi o Semilla principal -----

l2=new JLabel("Ingresa Valor de Semilla:");
l2.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
l2.setForeground(Color.DARK_GRAY);
establecerValores(0,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l2,restricciones);
contenedor.add(l2);

t1=new JTextField(10);
t1.setToolTipText("Escribe Numero");
t1.setBorder(BorderFactory.createLoweredBevelBorder());
establecerValores(2,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t1,restricciones);
contenedor.add(t1);

// ----- El valor de la constante K -----

l3=new JLabel("Valor de Constante 'K':");
l3.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
l3.setForeground(Color.DARK_GRAY);
establecerValores(0,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l3,restricciones);
contenedor.add(l3);

t2=new JTextField(10);
t2.setToolTipText("Escribe Numero");
t2.setBorder(BorderFactory.createLoweredBevelBorder());
establecerValores(2,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t2,restricciones);
contenedor.add(t2);

// ----- El valor de el modulo -----

l4=new JLabel("Valor de G:");
l4.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
l4.setForeground(Color.DARK_GRAY);
establecerValores(0,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

t3=new JTextField(10);
t3.setToolTipText("Escribe Numero");
t3.setBorder(BorderFactory.createLoweredBevelBorder());
establecerValores(2,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t3,restricciones);
contenedor.add(t3);

//----- Boton que ejecuta la accion a realizar -----

b1=new JButton("HaCeR");
b1.setToolTipText("Clic Para Convertir");
b1.setBorder(new EmptyBorder(10,15,10,15));
b1.setMnemonic(KeyEvent.VK_H);
b1.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
b1.setForeground(Color.RED);
establecerValores(1,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b1,restricciones);
contenedor.add(b1);

//----- Boton que permite la limpieza de todas las cajas de texto -----

b2=new JButton("LiMpIaR");
b2.setToolTipText("Clic Para Limpiar Datos");
b2.setBorder(new EmptyBorder(10,15,10,15));
b2.setMnemonic(KeyEvent.VK_L);
b2.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
b2.setForeground(Color.RED);
establecerValores(3,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b2,restricciones);
contenedor.add(b2);

//----- Area deonde aparecera el resultado de la Xi -----

l4=new JLabel("Xi");
l4.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
l4.setForeground(Color.DARK_GRAY);
establecerValores(1,8,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

a1=new JTextArea(10,6);
JScrollPane scrollPane1 = new JScrollPane(a1);
a1.setEditable(false);
a1.setBorder(BorderFactory.createLoweredBevelBorder());
establecerValores(1,9,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(scrollPane1,restricciones);
contenedor.add(scrollPane1);

//----- Area donde aparecera el resultado de la Ri -----

l5=new JLabel("Ri");
l5.setFont(new Font("Berlin Sans FB",Font.BOLD,14));
l5.setForeground(Color.DARK_GRAY);
establecerValores(2,8,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l5,restricciones);
contenedor.add(l5);

a2=new JTextArea(10,8);
JScrollPane scrollPane2 = new JScrollPane(a2);
a2.setEditable(false);
a2.setBorder(BorderFactory.createLoweredBevelBorder());
establecerValores(2,9,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(scrollPane2,restricciones);
contenedor.add(scrollPane2);

//----- Escuchador de eventos -----

ManejadorCamposTexto m=new ManejadorCamposTexto();
b1.addActionListener(m);
b2.addActionListener(m);

pack();

}

//----- Posiciones de cada elemento dentro de nuestra ventana-----
//********************************************************************************************
public void establecerValores(int columna,int fila, int anchura, int altura)
{
restricciones.gridx=columna;
restricciones.gridy=fila;

restricciones.gridwidth=anchura;
restricciones.gridheight=altura;
}
//*******************************************************************************************

public static void main(String args[])
{
new AlgoritmoCongruencialMultiplicativo();
}

private class ManejadorCamposTexto implements ActionListener
{
public void actionPerformed(ActionEvent evento)
{
if (evento.getSource()==b1)
{
  //********** Permite verificar si la caja de texto esta vacia **********//

if(t1.getText().equals("")|| t2.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Ingresa dato faltante","error",JOptionPane.ERROR_MESSAGE);
}

else
{

try
{
x=Integer.parseInt(t1.getText());
k=Integer.parseInt(t2.getText());
g=Integer.parseInt(t3.getText());

if(x%2==0)
{
JOptionPane.showMessageDialog(null,"X debe ser numero impar","error",JOptionPane.ERROR_MESSAGE);
t1.setText("");
}

else
{

if(x<=0)
{
JOptionPane.showMessageDialog(null,"X debe ser valor positivo","error",JOptionPane.ERROR_MESSAGE);
t1.setText("");
}

else
{

if(k<=0)
{
JOptionPane.showMessageDialog(null,"K debe ser mayor a 0","error",JOptionPane.ERROR_MESSAGE);
t2.setText("");
}

else
{

if(g<=0)
{
JOptionPane.showMessageDialog(null,"G debe ser mayor a 0","error",JOptionPane.ERROR_MESSAGE);
t3.setText("");
}

else
{

for(int j=0; j<g; j++)
{
m*=num;
System.out.println(m);
}

int cantidad=m/4;

a=5+(8*k);

for(int i=0; i<cantidad; i++)
{
numero=(a*x)%m;

String n1=String.valueOf(numero);
x=Integer.parseInt(n1);


v=new int[cantidad];
r=new double[cantidad];
v[i]=x;
r[i]=(v[i]*1.0)/(m-1);
sale+="x"+(i+1)+":  "+v[i]+"\n";

String err=String.valueOf(r[i]);
String yo=String.format("%.5f", r[i]);
erre+="r"+(i+1)+":  "+yo+"\n";

}
}
}
}
}

a1.setText(sale);
a2.setText(erre);

}

catch(java.lang.NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Dato invalido","error",JOptionPane.ERROR_MESSAGE);
t1.setText("");
t2.setText("");
t3.setText("");


}

}

}

if(evento.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
a1.setText("");
a2.setText("");
m=1;
num=2;

//----- Permite vaciar el arreglo -----

if(sale != null && erre != null)
{
sale="";
erre="";
}

if(v != null && r != null)
{
for(int l=0; l<cantidad; l++)
{
v=null;
r=null;

}
}

}

}
}


}

ALGORITMO CONGRUENCIAL LINEAL


/* Algoritmo Congruencial Lineal
 * Este algoritmo congruencial fue propuesto por D. H. Lehmer en 1951. Segun Law y Kelton este algoritmo ha sido el mas
 * usado. El algoritmo congruencial lineal genera una secuencia de numeros enteros por medio de la siguiente ecuacion
 * recursiva:
 *   Xi+1=(AXi+C)mod(M) i=0,1,2,...,n
 *
 * Donde X0 es la semilla, A es la constante multiplicativa, C es la constante aditiva y M es el modulo; X0 es mayor a 0,
 * A es mayor a 0, C mayor a 0 y M mayor a 0. Deben ser numeros enteros. Es importante senalar que la ecuacion recursiva
 * del algoritmo congruencial lineal genera una secuencia de numeros enteros: s={0,1,2,3,..., m-1}
 * Y que para obtener numeros seudoaleatorios en el intervalo (0,1) se requiere la siguiente ecuacion:
 * ri=(Xi)/(m-1)                 i=1,2,3,...,n.
 *
 * Fuente:   .....------          Conocimientos adquiridos e investigacion en distintas fuentes          ------.....

                                      *********************************************
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  SiMuLaCiOn                               *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class AlgoritmoCongruencialLineal extends JFrame
{
private Container contenedor;
private GridBagLayout esquema;
private GridBagConstraints restricciones;
JLabel l1, l2, l3, l4, l5, l6;
JButton b1,b2;
JTextField t1,t2, t3, t4, t5, t6;
JTextArea a1, a2, a3;
int numero=0, valor1=0, veces=0, cantidad;
String valor, ccuadrado="", sale="", cua="", erre="";
String err;
double[] r;
int[] v;
int num=2, m=1;

public AlgoritmoCongruencialLineal()
{
setSize(350,350);
setTitle("Algoritmo Congruencial Lineal");
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(HIDE_ON_CLOSE);

contenedor=getContentPane();
esquema=new GridBagLayout();
contenedor.setLayout(esquema);

//**********************************************************
//- Permite ingresar imagen de fondo.              -
//----------------------------------------------------------
((JPanel)getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("10.jpg");
JLabel fondo = new JLabel(img);
fondo.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
getLayeredPane().add(fondo,JLayeredPane.FRAME_CONTENT_LAYER);
//----------------------------------------------------------

restricciones=new GridBagConstraints();
restricciones.insets=new Insets(5,5,5,5);

restricciones.fill=GridBagConstraints.BOTH;

l1=new JLabel("Algoritmo Congruencial Lineal");
l1.setFont (new Font ("Courant", Font.BOLD, 30));
         l1.setForeground (Color.BLACK);
         establecerValores(0,0,4,1);
         restricciones.fill=GridBagConstraints.BOTH;
         esquema.setConstraints(l1,restricciones);
         contenedor.add(l1);

l2=new JLabel("Ingresa Valor de Semilla:");
l2.setFont(new Font("Courant",Font.PLAIN,14));
l2.setForeground(Color.BLACK);
establecerValores(0,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l2,restricciones);
contenedor.add(l2);

t1=new JTextField(10);
t1.setToolTipText("Escribe Numero");
t1.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.BLUE));
establecerValores(2,2,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t1,restricciones);
contenedor.add(t1);

l3=new JLabel("Valor de Constante multiplicativa 'K':");
l3.setFont(new Font("Courant",Font.PLAIN,14));
l3.setForeground(Color.BLACK);
establecerValores(0,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l3,restricciones);
contenedor.add(l3);

t2=new JTextField(10);
t2.setToolTipText("Escribe Numero");
t2.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.BLUE));
establecerValores(2,3,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t2,restricciones);
contenedor.add(t2);

l4=new JLabel("Valor de Constante aditiva 'C':");
l4.setFont(new Font("Courant",Font.PLAIN,14));
l4.setForeground(Color.BLACK);
establecerValores(0,4,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

t3=new JTextField(10);
t3.setToolTipText("Escribe Numero");
t3.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.BLUE));
establecerValores(2,4,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t3,restricciones);
contenedor.add(t3);

l5=new JLabel("Valor de G:");
l5.setFont(new Font("Courant",Font.PLAIN,14));
l5.setForeground(Color.BLACK);
establecerValores(0,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l5,restricciones);
contenedor.add(l5);

t4=new JTextField(10);
t4.setToolTipText("Escribe Numero");
t4.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.BLUE));
establecerValores(2,5,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t4,restricciones);
contenedor.add(t4);

l6=new JLabel("Cantidad de numeros a generar:");
l6.setFont(new Font("Courant",Font.PLAIN,14));
l6.setForeground(Color.BLACK);
establecerValores(0,6,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l6,restricciones);
contenedor.add(l6);

t5=new JTextField(10);
t5.setToolTipText("Escribe Numero");
t5.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.BLUE));
establecerValores(2,6,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(t5,restricciones);
contenedor.add(t5);

b1=new JButton("HaCeR");
b1.setToolTipText("Clic Para Realizar Calculo");
b1.setBorder(new EmptyBorder(10,15,10,15));
b1.setMnemonic(KeyEvent.VK_H);
b1.setFont(new Font("Courant",Font.PLAIN,14));
b1.setForeground(Color.BLUE);
establecerValores(1,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b1,restricciones);
contenedor.add(b1);

b2=new JButton("LiMpIaR");
b2.setToolTipText("Clic Para Limpiar Datos");
b2.setBorder(new EmptyBorder(10,15,10,15));
b2.setMnemonic(KeyEvent.VK_L);
b2.setFont(new Font("Courant",Font.PLAIN,14));
b2.setForeground(Color.BLUE);
establecerValores(3,7,2,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(b2,restricciones);
contenedor.add(b2);

l4=new JLabel("Xi");
l4.setFont(new Font("Courant",Font.PLAIN,14));
l4.setForeground(Color.BLACK);
establecerValores(1,8,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l4,restricciones);
contenedor.add(l4);

a1=new JTextArea(10,6);
a1.setEditable(false);
a1.setBorder(BorderFactory.createMatteBorder(4, 1, 1, 1, Color.BLUE));
establecerValores(1,9,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(a1,restricciones);
contenedor.add(a1);

l5=new JLabel("Ri");
l5.setFont(new Font("Courant",Font.PLAIN,14));
l5.setForeground(Color.BLACK);
establecerValores(2,8,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(l5,restricciones);
contenedor.add(l5);

a2=new JTextArea(10,6);
a2.setEditable(false);
a2.setBorder(BorderFactory.createMatteBorder(4, 1, 1, 1, Color.BLUE));
establecerValores(2,9,1,1);
restricciones.fill=GridBagConstraints.CENTER;
esquema.setConstraints(a2,restricciones);
contenedor.add(a2);

ManejadorCamposTexto m=new ManejadorCamposTexto();
b1.addActionListener(m);
b2.addActionListener(m);

// ----- Para que aparezcan todos los componentes sin la necesidad
// ----- de mover la ventana.
pack();

}

//********************************************************************************************
public void establecerValores(int columna,int fila, int anchura, int altura)
{
restricciones.gridx=columna;
restricciones.gridy=fila;

restricciones.gridwidth=anchura;
restricciones.gridheight=altura;
}
//*******************************************************************************************

public static void main(String args[])
{
new AlgoritmoCongruencialLineal();
}

private class ManejadorCamposTexto implements ActionListener
{
public void actionPerformed(ActionEvent evento)
{
if (evento.getSource()==b1)
{
if(t1.getText().equals("")|| t2.getText().equals("") || t3.getText().equals("") || t4.getText().equals("") || t5.getText().equals(""))   //********** Permite verificar si la caja de texto esta vacia **********//
{
JOptionPane.showMessageDialog(null,"Ingresa dato faltante");
}

else
{

try
{
int x=Integer.parseInt(t1.getText());
int k=Integer.parseInt(t2.getText());
int c=Integer.parseInt(t3.getText());
int g=Integer.parseInt(t4.getText());
int cantidad=Integer.parseInt(t5.getText());

if(x<=0)
{
JOptionPane.showMessageDialog(null,"Semilla X debe ser mayor a 0");
t1.setText("");
}

else
{

if(k<=0)
{
JOptionPane.showMessageDialog(null,"Constante K debe ser mayor a 0");
t2.setText("");
}

else
{

if(c<=0)
{
JOptionPane.showMessageDialog(null,"Constante C debe ser mayor a 0");
t3.setText("");
}

else
{

if(g<=0)
{
JOptionPane.showMessageDialog(null,"Constante G debe ser mayor a 0");
t4.setText("");
}

else
{

if(cantidad<=0)
{
JOptionPane.showMessageDialog(null,"Cantidad de numeros que deseas generar?");
t5.setText("");
}

else
{
for(int j=0; j<g; j++)
{
m*=num;
}

int a=1+(4*k);

for(int i=0; i<cantidad; i++)
{
numero=(a*x+c)%m;

String n1=String.valueOf(numero);
x=Integer.parseInt(n1);


v=new int[cantidad];
r=new double[cantidad];
v[i]=x;
r[i]=(v[i]*1.0)/(m-1);
sale+="y"+(i+1)+":  "+v[i]+"\n";

String err=String.format("%.5f",r[i]);
erre+="r"+i+":  "+err+"\n";

}
}
}
}
}
}

a1.setText(sale);
a2.setText(erre);

}

catch(java.lang.NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Dato invalido");
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
}

}


}

if(evento.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
a1.setText("");
a2.setText("");

if(sale != null && erre != null)
{
sale="";
err="";
erre="";
}


if(v != null && r != null)
for(int j=0; j<cantidad; j++)
{
v=null;
r=null;
}

}

}
}


}