EvalFuncion.java



// ======================================================================
// Objetivo:	Graficar un conjunto de pares de valores x,y
// Autor:	J. Rafael R. Ochoa
// Fecha:	Julio 2004,
// Derechos:	SID - FIE - UMSNH
// ======================================================================

import java.io.*;
import java.util.Vector;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Runtime;
import java.awt.FileDialog;

public class EvalFuncion extends JFrame
{

	JTextField CTVF_DESDE, CTVF_HASTA, CTVF_INCR, CTVF_ARCH, CT_ECUA;	// Conjunto de campos de texto
	JButton B_GENERAR, B_SALIR, B_GRAFICAR;					// Conjunto de Botones


	// AtenciOn a la interrupciOn del sistema
	// -click en el botOn x de la ventana-
	//_____________________________________________________________
	class WEH extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }

	// AtenciOn a los eventos de los componentes de tipo JButton
	//_____________________________________________________________
	class EscuchaEvento implements ActionListener
	{
		public void actionPerformed(ActionEvent AE)
		{
			if (AE.getActionCommand().equals("GENERA DATOS"))	EVENTO_GENDAT();	// Pide valores de la funciOn
			if (AE.getActionCommand().equals("GRAFICAR"))		EVENTO_GRAFICAR();	// Abre ventana de selecciOn de archivos
			if (AE.getActionCommand().equals("SALIR"))		EVENTO_SALIR();	// SALIR
		}
	}

	// MAIN
	//_________________________________________________________
	public static void main(String R[]) throws Exception { new EvalFuncion(); }

	// Muestra la interfaz del sistema
	//_________________________________________________________
	public EvalFuncion()
	{
		CTVF_DESDE = new JTextField();			// Campo de Texto de la Ventana Flotante Vdesde
		CTVF_HASTA = new JTextField();			// Campo de Texto de la Ventana Flotante Vhasta
		CTVF_INCR  = new JTextField();			// Campo de Texto de la Ventana Flotante Vincremento
		CTVF_ARCH  = new JTextField();			// Campo de Texto de la Ventana Flotante Vnombre del archivo
		CT_ECUA    = new JTextField();			// Campo de Texto de la ecuaciOn
		B_GENERAR  = new JButton("GENERA DATOS");	// BotOn OK
		B_GRAFICAR = new JButton("GRAFICAR");		// BotOn GRAFICAR
		B_SALIR    = new JButton("SALIR");		// BotOn SALIR
	
		//----------------------------------------------------------------------------------------------------------
		getContentPane().setLayout(null);		// Contenedor nulo
		setTitle("Evaluador de funciones...");		// TItulo de la ventana
		setResizable(true);				// No es redimensionable
		setLocation(50,50);				// PosiciOn centrada
		setSize(410, 160);				// Dimensiones
		//---------------------------------------------------------------------------------------------------------
		addWindowListener(new EvalFuncion.WEH());

		JLabel L_DESDE = new JLabel("Valor inicial: ");
		JLabel L_HASTA = new JLabel("Valor final: ");
		JLabel L_INCR = new JLabel("Incremento: ");
		JLabel L_ARCH = new JLabel("Archivo: ");
		JLabel L_ECUA = new JLabel("ExpresiOn: ");

		L_ECUA.setBounds(10, 5,  120, 17);
		CT_ECUA.setBounds(10, 25, 380, 17);
		L_DESDE.setBounds(10, 55, 120, 17);
		L_HASTA.setBounds(10, 75, 120, 17);
		L_INCR.setBounds(10, 95, 120, 17);
		L_ARCH.setBounds(10, 115, 120, 17);

		B_GENERAR.setBounds(225, 75, 135, 15); B_GENERAR.addActionListener(new EscuchaEvento());
		B_GRAFICAR.setBounds(225, 95, 135, 15); B_GRAFICAR.addActionListener(new EscuchaEvento());
		B_SALIR.setBounds(225, 115, 135, 15); B_SALIR.addActionListener(new EscuchaEvento());

		CTVF_DESDE.setBounds(100, 55, 120, 17);
		CTVF_HASTA.setBounds(100, 75, 120, 17);
		CTVF_INCR.setBounds(100, 95, 120, 17);
		CTVF_ARCH.setBounds(100, 113, 120, 17);

		getContentPane().add(L_DESDE, null);
		getContentPane().add(L_HASTA, null);
		getContentPane().add(L_INCR, null);
		getContentPane().add(L_ARCH, null);
		getContentPane().add(L_ECUA, null);

		getContentPane().add(B_GENERAR, null);
		getContentPane().add(B_SALIR, null);
		getContentPane().add(B_GRAFICAR, null);

		getContentPane().add(CTVF_DESDE, null);
		getContentPane().add(CTVF_HASTA, null);
		getContentPane().add(CTVF_INCR, null);
		getContentPane().add(CTVF_ARCH, null);
		getContentPane().add(CT_ECUA, null);
		show();						// Mostrar la ventana
	}

	// Se guarda el contenido de la pila "ARCHIVOS_DE_DATOS" en un archivo
	// y se termina la ejecucin del sistema.
	//__________________________________________________________________________
	void EVENTO_SALIR() { System.exit(0); }

	// Atencin al evento "Leer datos" y pasar el control a la clase GRAFICA
	//_____________________________________________________________________________
	void EVENTO_GRAFICAR()
	{
		CTVF_DESDE.setText("");
		CTVF_HASTA.setText("");
		CTVF_INCR.setText("");
		CTVF_ARCH.setText("");
		SeleccionaArchivo();
	}
	// Interfaz para la seleccin de un archivo de una lista de archivos
	//__________________________________________________________________
	void SeleccionaArchivo()
	{
		FileDialog FD = new FileDialog(new JFrame(), "Seleccione un archivo de datos");
		FD.show();						// Hacer visible FD
		String Archivo[] = {FD.getFile()};
		try { if (Archivo != null) {xy_plot XYPLOT = new xy_plot(Archivo); } }
		catch(IOException e){}
	}

	// Funciones:
	// 1.- Verifica que todos los campos tengan datos. Si no hay alguno, no se contina
	// 2.- Se respalda la informaciOn introducida en variables locales
	// 3.- Desaparecer la ventana flotante.
	// 4.- Evaluar la ecuacin guardando los resultados en un archivo.
	// 5.- Aadir el nombre del archivo nuevo a la pila de archivos "ARCHIVOS_DE_DATOS"
	//	 solamente si no existe.
	//________________________________________________________________________________________
	void EVENTO_GENDAT()
	{
		double ValIni=0, ValFin=0, Increm=0;
		String NomArc="";

		// Si alguno de estos datos falta, no se hace nada
		if (CTVF_DESDE.getText().length() != 0 || CTVF_HASTA.getText().length() != 0 ||
			CTVF_INCR.getText().length() != 0 || CTVF_ARCH.getText().length() != 0 ||
			CT_ECUA.getText().length() != 0)
		{
			ValIni = Double.parseDouble(CTVF_DESDE.getText());	// Se extraen los valores
			ValFin = Double.parseDouble(CTVF_HASTA.getText());
			Increm = Double.parseDouble(CTVF_INCR.getText());
			NomArc = CTVF_ARCH.getText()+".txt";

			FileOutputStream FOS = null;					// Declaracin del objeto FOS

			try {	FOS = new FileOutputStream(NomArc); }		// Creacin del archivo de salida
			catch (IOException IOE) {}

			String ExprInfija="", ExprPostfija="",STVALOR="";
			ExprInfija = CT_ECUA.getText();

			InfijaAPostfija IAP = new InfijaAPostfija(ExprInfija);	// Convercin al formato posfijo
			ExprPostfija = IAP.Convertir();

			EvaluaPostfija EP = new EvaluaPostfija(ExprPostfija);		// Declaracin de la evaluacin

			for (double k=ValIni; k<=ValFin; k+=Increm)			// Generacin de los valores de la funcin
			{
				double Valor = EP.Evalua(k);
				STVALOR = "" + k + "," + Valor + "\n";

				try	{	FOS.write(STVALOR.getBytes());	}
				catch (IOException e)	{	System.out.println("Error: " + e.toString());	}
			}
		}
	}
}