From 2ea2f91b364a7b5d9eb6a6b01f48ba9f31c3a99b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 23 Jun 2016 21:31:27 -0400 Subject: [PATCH] Bugs fixed. Linked list work. Non fullscreen. Comments fixed. --- src/cl/cromer/estructuras/Array.java | 6 +- .../cromer/estructuras/ArrayController.java | 8 +- src/cl/cromer/estructuras/ArrayTipos.java | 45 +++ src/cl/cromer/estructuras/Cola.java | 2 +- src/cl/cromer/estructuras/ColaController.java | 2 +- src/cl/cromer/estructuras/Colores.java | 2 +- src/cl/cromer/estructuras/Grafico.java | 35 ++- .../estructuras/ListaEnlazadaTipos.java | 49 +++ .../estructuras/ListaEnlazdaController.java | 284 ++++++++++++++++++ src/cl/cromer/estructuras/Main.java | 3 +- src/cl/cromer/estructuras/MenuController.java | 24 +- src/cl/cromer/estructuras/Pila.java | 2 +- src/cl/cromer/estructuras/PilaController.java | 2 +- .../estructuras/fxml/listaEnlazada.fxml | 30 ++ src/cl/cromer/estructuras/fxml/menu.fxml | 2 +- 15 files changed, 466 insertions(+), 30 deletions(-) create mode 100644 src/cl/cromer/estructuras/ArrayTipos.java create mode 100644 src/cl/cromer/estructuras/ListaEnlazadaTipos.java create mode 100644 src/cl/cromer/estructuras/ListaEnlazdaController.java create mode 100644 src/cl/cromer/estructuras/fxml/listaEnlazada.fxml diff --git a/src/cl/cromer/estructuras/Array.java b/src/cl/cromer/estructuras/Array.java index 1261077..663ed04 100644 --- a/src/cl/cromer/estructuras/Array.java +++ b/src/cl/cromer/estructuras/Array.java @@ -293,9 +293,9 @@ public class Array { } /** - * - * @param izquerda int: La posición del quick desded la izquerda. - * @param derecha int: La posición del quick desded la derecha. + * Particionar el array desded la izquerda y derecho usando un pivot. + * @param izquerda int: La posición del quick desde la izquerda. + * @param derecha int: La posición del quick desde la derecha. * @param pivot String: El valor a comparar con los otros. * @return ParticionarResult: Los resultados de particionar. */ diff --git a/src/cl/cromer/estructuras/ArrayController.java b/src/cl/cromer/estructuras/ArrayController.java index 80c9143..d844462 100644 --- a/src/cl/cromer/estructuras/ArrayController.java +++ b/src/cl/cromer/estructuras/ArrayController.java @@ -217,7 +217,7 @@ public class ArrayController implements Initializable { if (encontrado != -1) { generarGrafico(); grafico = new Grafico(scene); - grafico.destacer(encontrado, "rectangulo"); + grafico.destacer(encontrado, Grafico.RECTANGULO); } else { errorNoEsta(); @@ -265,8 +265,10 @@ public class ArrayController implements Initializable { scene = contenidoArray.getScene(); grafico = new Grafico(scene); this.array = new Array(10); - Array array = (Array) scene.getUserData(); - this.array.setOrdered(array.isOrdered()); + ArrayTipos arrayTipos = (ArrayTipos) scene.getUserData(); + if (arrayTipos.getTipo() == ArrayTipos.ORDENADO) { + this.array.setOrdered(true); + } } /** diff --git a/src/cl/cromer/estructuras/ArrayTipos.java b/src/cl/cromer/estructuras/ArrayTipos.java new file mode 100644 index 0000000..3e2e4ec --- /dev/null +++ b/src/cl/cromer/estructuras/ArrayTipos.java @@ -0,0 +1,45 @@ +package cl.cromer.estructuras; + +/** + * Esta clase contiene los tipos de array. + */ +final public class ArrayTipos { + /** + * Tipo de array simple. + */ + static final public int SIMPLE = 0; + /** + * Tipo de array ordenado. + */ + static final public int ORDENADO = 1; + + /** + * El tipo que está elegido. + */ + private int tipo; + + /** + * Inicilizar el tipo. + * @param tipo int: Tipo de array, {@value #SIMPLE} o {@value #ORDENADO} + */ + public ArrayTipos(int tipo) { + switch (tipo) { + case SIMPLE: + this.tipo = SIMPLE; + break; + case ORDENADO: + this.tipo = ORDENADO; + break; + default: + this.tipo = SIMPLE; + } + } + + /** + * Devolver el tipo. + * @return int: El tipo de array. + */ + public int getTipo() { + return tipo; + } +} \ No newline at end of file diff --git a/src/cl/cromer/estructuras/Cola.java b/src/cl/cromer/estructuras/Cola.java index 079e9fd..a1878c9 100644 --- a/src/cl/cromer/estructuras/Cola.java +++ b/src/cl/cromer/estructuras/Cola.java @@ -91,7 +91,7 @@ public class Cola { * @return String: El valor que está guardado en el indice. */ public String getIndice(int indice) { - if (indice >= 0 && indice < cola.length) { + if (cola != null && indice >= 0 && indice < cola.length) { return cola[indice]; } else { diff --git a/src/cl/cromer/estructuras/ColaController.java b/src/cl/cromer/estructuras/ColaController.java index e448918..36df888 100644 --- a/src/cl/cromer/estructuras/ColaController.java +++ b/src/cl/cromer/estructuras/ColaController.java @@ -192,7 +192,7 @@ public class ColaController implements Initializable { int encontrado = cola.peek(); if (encontrado != Integer.MIN_VALUE) { generarGrafico(); - grafico.destacer(0, "rectangulo"); + grafico.destacer(0, Grafico.RECTANGULO); } else { errorVacia(); diff --git a/src/cl/cromer/estructuras/Colores.java b/src/cl/cromer/estructuras/Colores.java index a04b813..a524379 100644 --- a/src/cl/cromer/estructuras/Colores.java +++ b/src/cl/cromer/estructuras/Colores.java @@ -10,7 +10,7 @@ public class Colores { /** * Cuantos colores estan definidos en esta clase. */ - static public int MAX_COLORS = 7; + static final public int MAX_COLORS = 7; /** * El color actual en forma numerica. diff --git a/src/cl/cromer/estructuras/Grafico.java b/src/cl/cromer/estructuras/Grafico.java index e9ae5f1..75af501 100644 --- a/src/cl/cromer/estructuras/Grafico.java +++ b/src/cl/cromer/estructuras/Grafico.java @@ -16,6 +16,16 @@ import javafx.util.Duration; * @author Chris Cromer */ public class Grafico { + /** + * Tipo de dibujo rectuangular. + */ + static final public int RECTANGULO = 0; + + /** + * Tipo de dibujo circular. + */ + static final public int CIRCULO = 1; + /** * Contiene la animación de destacar. */ @@ -29,7 +39,7 @@ public class Grafico { /** * El tipo de objeto que está destacado. */ - private String tipo; + private int tipo; /** * El color original de fondo para volver cuando no es destacado. @@ -138,22 +148,25 @@ public class Grafico { /** * Destacar un elemento * @param valor int: El indice a destacar. - * @param tipo String: El tipo de objeto a destacer(rectangulo o cicurlo) + * @param tipo int: El tipo de objeto a destacer, {@value #RECTANGULO} o {@value #CIRCULO} */ - public void destacer(int valor, String tipo) { - if (!tipo.equals("rectangulo") && !tipo.equals("circulo")) { + public void destacer(int valor, int tipo) { + if (tipo != RECTANGULO && tipo != CIRCULO) { return; } - this.tipo = tipo; + else { + this.tipo = tipo; + } + destacado = valor; Colores colores = new Colores(); Rectangle rectangle = null; Circle circle = null; - if (tipo.equals("rectangulo")) { + if (this.tipo == RECTANGULO) { rectangle = (Rectangle) scene.lookup("#border_" + String.valueOf(valor)); destacadoBG = (Color) rectangle.getFill(); } - else if (tipo.equals("circulo")) { + else if (this.tipo == CIRCULO) { circle = (Circle) scene.lookup("#border_" + String.valueOf(valor)); destacadoBG = (Color) circle.getFill(); } @@ -161,10 +174,10 @@ public class Grafico { destacadoFG = (Color) text.getStroke(); PauseTransition changeColor[] = new PauseTransition[Colores.MAX_COLORS]; for (int i = 0; i < Colores.MAX_COLORS; i++) { - if (tipo.equals("rectangulo")) { + if (this.tipo == RECTANGULO) { changeColor[i] = createPauseTransition(rectangle, text, colores.getFondo(), colores.getTexto()); } - else if (tipo.equals("circulo")) { + else if (this.tipo == CIRCULO) { changeColor[i] = createPauseTransition(circle, text, colores.getFondo(), colores.getTexto()); } colores.siguinteColor(); @@ -180,11 +193,11 @@ public class Grafico { public void removerDestacar() { if (destacado != -1) { blinkTransition.stop(); - if (tipo.equals("rectangulo")) { + if (tipo == RECTANGULO) { Rectangle rectangle = (Rectangle) scene.lookup("#border_" + String.valueOf(destacado)); rectangle.setFill(destacadoBG); } - else if (tipo.equals("circulo")) { + else if (tipo == CIRCULO) { Circle circle = (Circle) scene.lookup("#border_" + String.valueOf(destacado)); circle.setFill(destacadoBG); } diff --git a/src/cl/cromer/estructuras/ListaEnlazadaTipos.java b/src/cl/cromer/estructuras/ListaEnlazadaTipos.java new file mode 100644 index 0000000..318cebf --- /dev/null +++ b/src/cl/cromer/estructuras/ListaEnlazadaTipos.java @@ -0,0 +1,49 @@ +package cl.cromer.estructuras; + +final public class ListaEnlazadaTipos { + /** + * Tipo simple. + */ + static final public int SIMPLE = 0; + /** + * Tipo circular. + */ + static final public int CIRCULAR = 1; + /** + * Tipo doblemente enlazada. + */ + static final public int DOBLEMENTE_ENLAZADA = 2; + + /** + * El tipo elegido. + */ + private int tipo; + + /** + * Inicilizar el tipo de lista enlazada. + * @param tipo int: El tipo de lista enlazada, {@value #SIMPLE}, {@value #CIRCULAR} o {@value #DOBLEMENTE_ENLAZADA} + */ + public ListaEnlazadaTipos(int tipo) { + switch (tipo) { + case SIMPLE: + this.tipo = SIMPLE; + break; + case CIRCULAR: + this.tipo = CIRCULAR; + break; + case DOBLEMENTE_ENLAZADA: + this.tipo = DOBLEMENTE_ENLAZADA; + break; + default: + this.tipo = SIMPLE; + } + } + + /** + * Devolver el tipo de lista enlazada. + * @return int: El tipo. + */ + public int getTipo() { + return tipo; + } +} diff --git a/src/cl/cromer/estructuras/ListaEnlazdaController.java b/src/cl/cromer/estructuras/ListaEnlazdaController.java new file mode 100644 index 0000000..ac043ee --- /dev/null +++ b/src/cl/cromer/estructuras/ListaEnlazdaController.java @@ -0,0 +1,284 @@ +package cl.cromer.estructuras; + +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.Scene; +import javafx.scene.control.ButtonBar; +import javafx.scene.control.ButtonType; +import javafx.scene.control.Dialog; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; + +import java.net.URL; +import java.util.Random; +import java.util.ResourceBundle; +import java.util.Scanner; +import java.util.logging.Level; + +/** + * Esta clase es para controlar todos la interfaz de ListaEnlazada. + * @author Chris Cromer + */ +public class ListaEnlazdaController implements Initializable { + + /** + * La caja para ingresar textos. + */ + @FXML private TextFieldLimited valorLista; + + /** + * Donde poner el contenido de array. + */ + @FXML private VBox contenidoLista; + + /** + * Donde va el codigo a mostrar a la pantalla. + */ + @FXML private Text codigoLista; + + /** + * La escena donde está cosas graficas. + */ + private Scene scene; + + /** + * Donde está guardado los idiomas. + */ + private ResourceBundle resourceBundle; + + /** + * El array usado en la aplicación. + */ + private Array array; + + /** + * Grafico rectangulos. + */ + private Grafico grafico; + + /** + * Inicializar todos los datos y dibujar las graficas. + * @param location URL: El URL de fxml en uso. + * @param resourceBundle ResourceBundle: Tiene datos de idioma. + */ + @Override + public void initialize(URL location, ResourceBundle resourceBundle) { + this.resourceBundle = resourceBundle; + + scene = null; + Colores colores = new Colores(); + + for (int i = 0; i < 10; i++) { + contenidoLista.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i))); + colores.siguinteColor(); + } + } + + /** + * Llenar el array con numeros al azar. + */ + @FXML + protected void botonLlenar() { + if (scene == null) { + initializeArray(); + } + + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; + + for (int i = array.size(); i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + while (array.buscar(numero) != -1) { + numero = random.nextInt(rango) + minimo; + } + array.insertar(numero); + } + generarGrafico(); + } + + /** + * Vaciar el array de todos los valores. + */ + @FXML + protected void botonVaciar() { + if (scene == null) { + initializeArray(); + } + + if (array.isOrdered()) { + array = new Array(10); + array.setOrdered(true); + } + else { + array = new Array(10); + array.setOrdered(false); + } + generarGrafico(); + } + + /** + * Insertar un valor al array y mostrar el codigo en la pantalla. + */ + @FXML + protected void botonInsertar() { + if (scene == null) { + initializeArray(); + } + + // Mostrar el codigo + String tipo = (array.isOrdered())?"Ordenado":"Simple"; + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next(); + codigoLista.setText(codigoTexto); + + if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { + try { + boolean exito = array.insertar(Integer.valueOf(valorLista.getText())); + if (exito) { + generarGrafico(); + } + else { + ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); + Dialog dialog = new Dialog<>(); + dialog.setTitle(resourceBundle.getString("error")); + if (array.size() == 10) { + dialog.setContentText(resourceBundle.getString("arrayLleno")); + } + else { + dialog.setContentText(resourceBundle.getString("arrayValorExiste")); + } + dialog.getDialogPane().getButtonTypes().add(botonCerrar); + dialog.show(); + } + } + catch (NumberFormatException exception) { + // El error no es fatal, sigue + Logs.log(Level.WARNING, "No es tipo int."); + errorNoValor(); + } + } + else { + errorNoValor(); + } + } + + /** + * Eliminar un valor del array si existe y mostrar el codigo en la pantalla. + */ + @FXML + protected void botonEliminar() { + if (scene == null) { + initializeArray(); + } + + // Mostrar el codigo + String tipo = (array.isOrdered())?"Ordenado":"Simple"; + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/eliminar")).useDelimiter("\\Z").next(); + codigoLista.setText(codigoTexto); + + try { + if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { + boolean exito = array.eliminar(Integer.valueOf(valorLista.getText())); + if (exito) { + generarGrafico(); + } + else { + errorNoEsta(); + } + } + else { + errorNoValor(); + } + } + catch (NumberFormatException exception) { + // El error no es fatal, sigue + Logs.log(Level.WARNING, "No es tipo int."); + errorNoValor(); + } + } + + /** + * Buscar si existe un elemento en el array y mostrar el codigo en la pantalla + * Si existe el valor destacarlo. + */ + @FXML + protected void botonBuscar() { + if (scene == null) { + initializeArray(); + } + + // Mostrar el codigo + String tipo = (array.isOrdered())?"Ordenado":"Simple"; + String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/buscar")).useDelimiter("\\Z").next(); + codigoLista.setText(codigoTexto); + + try { + if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { + int encontrado = array.buscar(Integer.valueOf(valorLista.getText())); + if (encontrado != -1) { + generarGrafico(); + grafico = new Grafico(scene); + grafico.destacer(encontrado, Grafico.RECTANGULO); + } + else { + errorNoEsta(); + } + } + else { + errorNoValor(); + } + } + catch (NumberFormatException exception) { + // El error no es fatal, sigue + Logs.log(Level.WARNING, "No es tipo int."); + errorNoValor(); + } + } + + /** + * Se muestra un error si la persona no ingresa un valor en el TextField. + */ + private void errorNoValor() { + ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); + Dialog dialog = new Dialog<>(); + dialog.setTitle(resourceBundle.getString("error")); + dialog.setContentText(resourceBundle.getString("arrayNoValor")); + dialog.getDialogPane().getButtonTypes().add(botonCerrar); + dialog.show(); + } + + /** + * Error cuando el valor no está en el array. + */ + private void errorNoEsta() { + ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); + Dialog dialog = new Dialog<>(); + dialog.setTitle(resourceBundle.getString("error")); + dialog.setContentText(resourceBundle.getString("arrayNoEsta")); + dialog.getDialogPane().getButtonTypes().add(botonCerrar); + dialog.show(); + } + + /** + * Crear el array de tamaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. + */ + private void initializeArray() { + scene = contenidoLista.getScene(); + grafico = new Grafico(scene); + this.array = new Array(10); + Array array = (Array) scene.getUserData(); + this.array.setOrdered(array.isOrdered()); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + grafico.removerDestacar(); + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#caja_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } +} diff --git a/src/cl/cromer/estructuras/Main.java b/src/cl/cromer/estructuras/Main.java index 64da54c..3a89ce8 100644 --- a/src/cl/cromer/estructuras/Main.java +++ b/src/cl/cromer/estructuras/Main.java @@ -15,6 +15,7 @@ import java.util.logging.Level; * Estructuras de Datos * Creado como proyecto semestral para la asignatura de estructuras de datos por la profesora Karina Rojas y el profesor Jorge Elgueta. * Creado en 2016-1 + * Se necesita java 8 instalado. * @author Chris Cromer * @version 1.0 */ @@ -58,7 +59,7 @@ public class Main extends Application { stage.close(); } - stage.setMaximized(true); + //stage.setMaximized(true); stage.setMinHeight(640); stage.setMinWidth(768); stage.show(); diff --git a/src/cl/cromer/estructuras/MenuController.java b/src/cl/cromer/estructuras/MenuController.java index f553606..6568af3 100644 --- a/src/cl/cromer/estructuras/MenuController.java +++ b/src/cl/cromer/estructuras/MenuController.java @@ -45,13 +45,12 @@ public class MenuController extends VBox implements Initializable { */ @FXML protected void menuArraySimple() { - Array array = new Array(1); - array.setOrdered(false); + ArrayTipos arrayTipos = new ArrayTipos(ArrayTipos.SIMPLE); loadStage( resourceBundle.getString("tituloArraySimple"), "/cl/cromer/estructuras/fxml/array.fxml", "/cl/cromer/estructuras/css/style.css", - array + arrayTipos ); } @@ -60,13 +59,12 @@ public class MenuController extends VBox implements Initializable { */ @FXML protected void menuArrayOrdenado() { - Array array = new Array(1); - array.setOrdered(true); + ArrayTipos arrayTipos = new ArrayTipos(ArrayTipos.ORDENADO); loadStage( resourceBundle.getString("tituloArrayOrdenado"), "/cl/cromer/estructuras/fxml/array.fxml", "/cl/cromer/estructuras/css/style.css", - array + arrayTipos ); } @@ -142,6 +140,20 @@ public class MenuController extends VBox implements Initializable { ); } + /** + * Click en Lista Enlazada Simple. + */ + @FXML + protected void menuListaEnlazadaSimple() { + ListaEnlazadaTipos listaEnlazadaTipos = new ListaEnlazadaTipos(ListaEnlazadaTipos.SIMPLE); + loadStage( + resourceBundle.getString("tituloMerge"), + "/cl/cromer/estructuras/fxml/listaEnlazada.fxml", + "/cl/cromer/estructuras/css/style.css", + listaEnlazadaTipos + ); + } + /** * Click en Pila. */ diff --git a/src/cl/cromer/estructuras/Pila.java b/src/cl/cromer/estructuras/Pila.java index cd884cb..7b467ad 100644 --- a/src/cl/cromer/estructuras/Pila.java +++ b/src/cl/cromer/estructuras/Pila.java @@ -90,7 +90,7 @@ public class Pila { * @return String: El valor que está guardado en el indice. */ public String getIndice(int indice) { - if (indice >= 0 && indice < pila.length) { + if (pila != null && indice >= 0 && indice < pila.length) { return pila[indice]; } else { diff --git a/src/cl/cromer/estructuras/PilaController.java b/src/cl/cromer/estructuras/PilaController.java index 3e2a376..4a944a5 100644 --- a/src/cl/cromer/estructuras/PilaController.java +++ b/src/cl/cromer/estructuras/PilaController.java @@ -192,7 +192,7 @@ public class PilaController implements Initializable { int encontrado = pila.peek(); if (encontrado != Integer.MIN_VALUE) { generarGrafico(); - grafico.destacer(pila.size() - 1, "rectangulo"); + grafico.destacer(pila.size() - 1, Grafico.RECTANGULO); } else { errorVacia(); diff --git a/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml b/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml new file mode 100644 index 0000000..9ba595b --- /dev/null +++ b/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + +