AccionImportar.java |
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class AccionImportar extends AbstractAction {
String[] datos;
File f;
float[][] tp; // tabla primitiva
JFrame jf;
Modelo m;
Lineal dl;
Dispersivo dv;
AccionImportar(Modelo m) {
dl = null;
dv = null;
jf = null;
this.m = m;
}
public void fijarMarco(JFrame jf) {
this.jf = jf;
}
public void fijarVistas(Lineal dl, Dispersivo dv) {
this.dl = dl;
this.dv = dv;
}
public void actionPerformed(ActionEvent ae) {
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new Filtro(".del"));
int resultado =jfc.showOpenDialog((JFrame)jf);
int num_columnas,i=0,j=0;
if (resultado==JFileChooser.APPROVE_OPTION)
{
f = jfc.getSelectedFile();
datos = AuxArchivosTexto.leerTodasLasLineasDeTexto(f);
}
else
return;
// El archivo está formado por parejas de números x,y
// con formato float
tp = new float[datos.length][2];
try
{
for(i=0;i<datos.length;i++)
{
StringTokenizer st = new StringTokenizer(datos[i], "*");
num_columnas = st.countTokens();
for(j=0;j<num_columnas;j++)
tp[i][j] = Float.parseFloat(st.nextToken());
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog( jf,
"Error en línea "+i+", columna "+j,
"¡Error de importación!",
JOptionPane.ERROR_MESSAGE);
}
m = new Modelo();
m.tp = tp;
dl.fijarModelo(m);
dv.fijarModelo(m);
((Marco)jf).fijarModelo(m);
jf.repaint();
}
}
|
AuxArchivosTexto.java |
/**
* AuxArchivosTexto.java
*
* Description:
* @author coti
* @version 1.0
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.awt.Component;
import java.util.Vector;
import javax.swing.*;
public class AuxArchivosTexto {
public AuxArchivosTexto() {
System.out.println("***Todos los métodos de
AuxArchivosTexto son estáticos.***");
}
public static File obtenerNombreArchivoTextoParaLeer
(Component propietario) {
File f = null;
JFileChooser selector = new JFileChooser();
if(selector.showOpenDialog(propietario)
==JFileChooser.APPROVE_OPTION)
f = selector.getSelectedFile();
else
f = null;
return f;
}
public static File obtenerNombreArchivoParaEscribir
(Component propietario) {
File f = null;
JFileChooser selector = new JFileChooser();
if(selector.showSaveDialog(propietario)
==JFileChooser.APPROVE_OPTION)
f = selector.getSelectedFile();
else
f = null;
return f;
}
public static String[] leerTodasLasLineasDeTexto(File f){
String[] templist;
String temp;
Vector v = new Vector();
FileInputStream fir;
InputStreamReader ir;
BufferedReader br;
try
{
fir = new FileInputStream(f);
}
catch(Exception fnf)
{
System.out.println(fnf);
return null;
}
ir = new InputStreamReader(fir);
br = new BufferedReader(ir);
do
{
try
{
temp = "";
temp = br.readLine();
}
catch(Exception e)
{
System.out.println("Error: " + e);
temp = "";
}
if (temp!=null)
v.addElement(temp);
}
while(temp!=null);
templist = new String[v.size()];
v.toArray(templist);
try
{
br.close();
}
catch(Exception e)
{
System.out.println(e);
}
return templist;
}
public static void escribirTodo(String texto, File f) {
FileWriter fw;
try
{
fw = new FileWriter(f);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,
e.toString(),
"Error de apertura para escribir",
JOptionPane.ERROR_MESSAGE);
return;
}
BufferedWriter bw = new BufferedWriter(fw);
try
{
bw.write(texto,0,texto.length());
}
catch(Exception e)
{
JOptionPane.showMessageDialog( null,
e.toString(),
"Error de lectura",
JOptionPane.ERROR_MESSAGE);
}
try
{
bw.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog( null,
e.toString(),
"Error de cierre al escribir",
JOptionPane.ERROR_MESSAGE);
}
}
public static String[] leerUnRegistroDeTextoDelimitado(
File f, char delim) {
String[] list = null;
return list;
}
public static String[] leerUnRegistroDeTextoEncolumnado(
File f, int[] lengths) {
String[] list = null;
return list;
}
static public void main(String[] args) {
AuxArchivosTexto atf = new AuxArchivosTexto();
String[] info =
{
"Really sorry old chap, this is just a humble deposit",
"of methods with no will of its own - literally,",
"we are meant only to serve!","",
"Anyway, obtenerNombreArchivoTextoParaLeer() will\
show a disk directory",
"and let you select a File for reading. That File will be",
"given back to you and may be used by all other methods",
"except of course obtenerNombreArchivoParaEscribir(),\
which will show a disk directory",
"and let you choose where to write your archivo and\
what to call it.",
""," The rest are self-explaining:","",
"String[] leerTodasLasLineasDeTexto(File f)",
"void escribirTodo(String[] sl, File f)",
"String[] leerUnRegistroDeTextoDelimitado(File f,\
char delim)",
"String[] leerUnRegistroDeTextoEncolumnado(File f,\
int[] lengths)",
"","Cheers, bruegel@hotmail.com"
};
for(int i=0;i<info.length;i++)
System.out.println(info[i]);
}
}
|
Diagramas.java |
/**
* Diagramas.java
*
* Description:
* @author coti
* @version 1.0
*/
import javax.swing.*;
public class Diagramas {
Lineal dl;
Dispersivo dv;
public Diagramas() {
AccionImportar aImportar = new AccionImportar(null);
Marco m = new Marco(aImportar);
aImportar.fijarMarco(m);
JTabbedPane jtp =new JTabbedPane(SwingConstants.TOP);
dl = new Lineal(null);
jtp.add("Lineal",dl);
dv = new Dispersivo(null);
jtp.add("Dispersivo", dv);
m.fijarVistas(dl,dv);
aImportar.fijarVistas(dl,dv);
m.getContentPane().add(jtp);
m.pack();
m.centrar();
m.setVisible(true);
}
static public void main(String[] args) {
new Diagramas();
}
}
|
Dispersivo.java |
import java.awt.*;
import javax.swing.*;
public class Dispersivo extends JPanel {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension pantalla;
Modelo modelo;
int numpaneles;
Dispersivo(Modelo m) {
this.modelo = m;
Font f = new Font("serif", Font.PLAIN, 12);
setFont(f);
pantalla = tk.getScreenSize();
if (pantalla.getWidth() >= 800)
setPreferredSize(new Dimension(640,480));
else
setPreferredSize(new Dimension(320,240));
numpaneles = 5;
}
public void fijarModelo(Modelo m) {
modelo = m;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int lx = (int)getSize().getWidth()*4/5;
int ly = (int)getSize().getHeight()*4/5;
int hx = (int)getSize().getWidth()/10;
int hy = (int)getSize().getHeight()/10;
FontMetrics fm = getGraphics().getFontMetrics();
// Recuadro de la gráfica
g.setColor(Color.red);
g.drawRect(hx,hy,lx,ly);
int sepx = lx / numpaneles;
int sepy = ly / numpaneles;
for(int i=0;i<5;i++)
{
g.drawLine(hx,hy+i*sepy,hx+lx,hy+i*sepy);
g.drawLine(hx+i*sepx,hy,hx+i*sepx,hy+ly);
}
if (modelo==null)
{
g.drawLine(hx,hy,hx+lx,ly+hy);
return;
}
int nfil = modelo.tp.length;
int[] px = new int[nfil];
int[] py = new int[nfil];
float xmin=modelo.tp[0][0], ymin = modelo.tp[0][1];
float xmax = xmin, ymax = ymin;
float tempx, tempy;
// Valores maximos y minimos
for(int i=0;i<nfil;i++)
{
tempx = modelo.tp[i][0];
tempy = modelo.tp[i][1];
xmin = tempx < xmin ? tempx : xmin;
xmax = tempx > xmax ? tempx : xmax;
ymin = tempy < ymin ? tempy : ymin;
ymax = tempy > ymax ? tempy : ymax;
}
g.setColor(Color.black);
String temp = "x = "+xmin;
int templen = fm.stringWidth(temp);
g.drawString(temp,hx-templen-5,hy+ly);
temp = "x = "+xmax;
templen = fm.stringWidth(temp);
g.drawString(temp,hx+lx+5,hy+ly);
temp = "y = "+ymin;
g.drawString(temp,hx,hy+ly+15);
temp = "y = "+ymax;
g.drawString(temp,hx,hy-10);
// Factores de escala
float fx = lx/(xmax-xmin);
float fy = ly/(ymax-ymin);
float dx = -xmin*fx;
float dy = -ymin*fy;
// Cálculo de la tabla representable
g.setColor(Color.green);
for(int i=0;i<nfil;i++)
{
px[i] = hx +(int)(dx + modelo.tp[i][0]*fx);
py[i] = hy+ly-(int)(dy + modelo.tp[i][1]*fy);
g.fillOval(px[i]-5, py[i]-5,10,10);
}
}
}
|
Filtro.java |
/**
* Filtro.java
*
* Description:
* @author coti
* @version 1.1
*/
import javax.swing.filechooser.FileFilter;
import java.io.File;
public class Filtro extends FileFilter {
String[] tipo;
Filtro(String[] s) {
tipo = s;
};
Filtro(String s) {
tipo = new String[1];
tipo[0] = s;
};
public boolean accept(File f) {
int num_tipos = tipo.length;
String nombre_archivo = f.getName().toUpperCase();
for(int i=0;i<num_tipos;i++)
if(nombre_archivo.endsWith(tipo[i].toUpperCase()))
return true;
if (f.isDirectory()) return true;
return false;
}
public String getDescription() {
String temp ="";
for(int i=0;i<tipo.length;i++)
temp += (tipo[i]+(i!=tipo.length-1?", ":""));
return temp;
}
}
|
JFrame6.java |
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
public class JFrame6 extends JFrame {
FileInputStream fis;
FileOutputStream fos;
ObjectInputStream ois;
ObjectOutputStream oos;
JFileChooser jfc = new JFileChooser();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension pantalla = tk.getScreenSize(), cuadro;
JFrame6() {
super("Sin titulo");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
JFrame6(String titulo) {
super(titulo);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void guardar(Object modelo) {
jfc.setFileFilter(null);
jfc.setApproveButtonText("Guardar");
jfc.setDialogTitle("Guardar (serializado, .bin)");
if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
{
try
{
fos = new FileOutputStream(jfc.getSelectedFile());
oos = new ObjectOutputStream(fos);
oos.writeObject(modelo);
oos.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog( null,
e.toString(),
"Error de escritura",
JOptionPane.ERROR_MESSAGE);
}
}
}
public Object leer(String tipo) {
Object temp = null;
jfc.setFileFilter(new Filtro(tipo));
jfc.setApproveButtonText("Abrir");
jfc.setDialogTitle("Abrir (serializado)");
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
try
{
fis = new FileInputStream(jfc.getSelectedFile());
ois = new ObjectInputStream(fis);
temp = ois.readObject();
ois.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog( null,
e.toString(),
"Error de lectura",
JOptionPane.ERROR_MESSAGE);
}
}
return temp;
}
JMenu preparar_menu( String nombre_menu,
String[] opciones,
String[] cadenas,
ActionListener al) {
JMenu temp = new JMenu(nombre_menu);
for(int i=0;i<opciones.length;i++)
{
JMenuItem jmi = new JMenuItem(opciones[i]);
jmi.setActionCommand(cadenas[i]);
jmi.addActionListener(al);
temp.add(jmi);
}
return temp;
}
JMenu preparar_menu( String nombre_menu,
String[] opciones,
String[] cadenas,
char[] ac,
ActionListener[] al) {
JMenu temp = new JMenu(nombre_menu);
for(int i=0;i<opciones.length;i++)
{
JMenuItem jmi = new JMenuItem(opciones[i]);
jmi.setActionCommand(cadenas[i]);
jmi.addActionListener(al[i]);
if (ac[i]!='*')
jmi.setAccelerator(KeyStroke.getKeyStroke(ac[i],
java.awt.Event.CTRL_MASK));
temp.add(jmi);
}
return temp;
}
public void centrar() {
int xpos, ypos;
pantalla = tk.getScreenSize();
cuadro = getContentPane().getPreferredSize();
xpos = (pantalla.width - cuadro.width)/2;
ypos = (pantalla.height - cuadro.height)/2;
setLocation(xpos,ypos);
}
}
|
Lineal.java |
import java.awt.*;
import javax.swing.*;
public class Lineal extends JPanel {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension pantalla;
Modelo modelo;
Lineal(Modelo m) {
this.modelo = m;
Font f = new Font("serif", Font.PLAIN, 12);
setFont(f);
pantalla = tk.getScreenSize();
if (pantalla.getWidth() >= 800)
setPreferredSize(new Dimension(640,480));
else
setPreferredSize(new Dimension(320,240));
}
public void fijarModelo(Modelo m) {
modelo = m;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int lx = (int)getSize().getWidth()*4/5;
int ly = (int)getSize().getHeight()*4/5;
int hx = (int)getSize().getWidth()/10;
int hy = (int)getSize().getHeight()/10;
FontMetrics fm = getGraphics().getFontMetrics();
// Recuadro de la gráfica
g.setColor(Color.red);
g.drawRect(hx,hy,lx,ly);
if (modelo==null)
{
g.drawLine(hx,hy,hx+lx,ly+hy);
return;
}
int nfil = modelo.tp.length;
int[] px = new int[nfil];
int[] py = new int[nfil];
float xmin=modelo.tp[0][0], ymin = modelo.tp[0][1];
float xmax = xmin, ymax = ymin;
float tempx, tempy;
// Valores maximos y minimos
for(int i=0;i<nfil;i++)
{
tempx = modelo.tp[i][0];
tempy = modelo.tp[i][1];
xmin = tempx < xmin ? tempx : xmin;
xmax = tempx > xmax ? tempx : xmax;
ymin = tempy < ymin ? tempy : ymin;
ymax = tempy > ymax ? tempy : ymax;
}
g.setColor(Color.black);
String temp = "x = "+xmin;
int templen = fm.stringWidth(temp);
g.drawString(temp,hx-templen-5,hy+ly);
temp = "x = "+xmax;
templen = fm.stringWidth(temp);
g.drawString(temp,hx+lx+5,hy+ly);
temp = "y = "+ymin;
g.drawString(temp,hx,hy+ly+15);
temp = "y = "+ymax;
g.drawString(temp,hx,hy-10);
// Factores de escala
float fx = lx/(xmax-xmin);
float fy = ly/(ymax-ymin);
float dx = -xmin*fx;
float dy = -ymin*fy;
// Cálculo de la tabla representable
for(int i=0;i<nfil;i++)
{
px[i] = hx +(int)(dx + modelo.tp[i][0]*fx);
py[i] = hy+ly-(int)(dy + modelo.tp[i][1]*fy);
}
g.drawPolyline(px,py,nfil);
}
}
|
Marco.java |
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Marco extends JFrame6 implements ActionListener{
JMenuBar jmb = null;
JMenu archivo = null;
JMenuItem iSalir = null;
JMenuItem iAbrir = null;
JMenuItem iGuardar = null;
JMenuItem iImportar = null;
Modelo modelo;
Lineal dl;
Dispersivo dv;
Marco(AccionImportar ai) {
super("Diagramas");
modelo = null;
dl = null;
dv = null;
jmb = new JMenuBar();
iAbrir = new JMenuItem("Abrir...");
iAbrir.setActionCommand("1");
iAbrir.addActionListener(this);
iGuardar = new JMenuItem("Guardar...");
iGuardar.setActionCommand("2");
iGuardar.addActionListener(this);
iImportar = new JMenuItem("Importar...");
iImportar.setActionCommand("3");
iImportar.addActionListener(ai);
iSalir = new JMenuItem("Salir");
iSalir.setActionCommand("0");
iSalir.addActionListener(this);
archivo = new JMenu("Archivo");
archivo.add(iAbrir);
archivo.add(iGuardar);
archivo.add(iImportar);
archivo.add(iSalir);
jmb.add(archivo);
setJMenuBar(jmb);
}
public void fijarVistas(Lineal dl, Dispersivo dv) {
this.dl = dl;
this.dv = dv;
}
public void fijarModelo(Modelo m) {
modelo = m;
}
public void actionPerformed(ActionEvent ae) {
int opcion = Integer.parseInt(ae.getActionCommand());
switch(opcion) {
case 0:
System.exit(0);
case 1: // abrir
modelo = (Modelo)leer(".bin");
dl.fijarModelo(modelo);
dv.fijarModelo(modelo);
getContentPane().repaint();
break;
case 2:
guardar(modelo);
break;
}
}
}
|
Modelo.java |
import java.io.Serializable;
public class Modelo implements Serializable {
float[][] tp;
Modelo() {
tp = null;
}
}
|