Versión final.

This commit is contained in:
David 2024-02-28 08:17:46 -06:00
parent 480da985c0
commit 3a1ec7ee74
8 changed files with 753 additions and 0 deletions

14
mavenproject1/pom.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.mycompany.mavenproject1.Mavenproject1</exec.mainClass>
</properties>
</project>

View File

@ -0,0 +1,121 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Controlador;
import com.mycompany.editorascii.Vista.Editor;
import com.mycompany.editorascii.Vista.Menú;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author drdav
*/
public class GestiónBotónAbrirArchivo implements ActionListener{
private Menú menu;
public GestiónBotónAbrirArchivo(Menú menu){
menu = menu;
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT", "txt");
fc.setFileFilter(filter);
int seleccion = fc.showOpenDialog(menu);
if(seleccion == JFileChooser.APPROVE_OPTION){
File fichero = fc.getSelectedFile();
try (FileReader fr = new FileReader(fichero)){
String texto = "";
int valor = fr.read();
while(valor != -1){
texto = texto + (char) valor;
valor = fr.read();
}
System.out.print(texto);
fr.close();
//abrirEditor(texto);
char[][]matriz = textoAMatriz(texto);
int largo = matriz.length;
int ancho = (largo == 0) ? 0 : matriz[0].length;
Editor editor = new Editor(ancho,largo,matriz);
menu.dispose();
}catch(IOException ex){
System.err.println(ex);
}
}
}
public void abrirEditor(String texto){
System.out.print(texto);
int x = 0;
int y = 0;
int i = 0;
int maxX = 0;
char car = texto.charAt(i);
while(i != texto.length()-1 ){
x++;
if(car == '\n'){
y++;
if(x > maxX){
maxX = x;
}
x = 0;
}
i++;
car = texto.charAt(i);
}
i = 0;
char[][] matriz = new char[x][y];
for(int j = 0; j < x; j++){
for(int w = 0; w < y; w++){
matriz[j][w]= texto.charAt(i);
i++;
}
}
Editor editor = new Editor(x,y,matriz);
}
public static char[][] textoAMatriz(String texto) {
// Dividir el texto en líneas
String[] lineas = texto.split("\n");
// Encontrar la longitud máxima de línea
int longitudMaxima = 0;
for (String linea : lineas) {
longitudMaxima = Math.max(longitudMaxima, linea.length());
}
// Crear la matriz de caracteres
char[][] matriz = new char[lineas.length][longitudMaxima];
// Llenar la matriz con los caracteres del texto
for (int i = 0; i < lineas.length; i++) {
Arrays.fill(matriz[i], ' '); // Llenar con espacios en blanco por defecto
char[] caracteresLinea = lineas[i].toCharArray();
System.arraycopy(caracteresLinea, 0, matriz[i], 0, caracteresLinea.length);
}
for (char[] fila : matriz) {
System.out.println(Arrays.toString(fila));
}
return matriz;
}
}

View File

@ -0,0 +1,49 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Controlador;
import com.mycompany.editorascii.Vista.Editor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author drdav
*/
public class GestiónBotónGuardarDibujo implements ActionListener{
private Editor editor;
public GestiónBotónGuardarDibujo(Editor parEditor){
editor = parEditor;
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT", "txt");
fc.setFileFilter(filter);
fc.setApproveButtonText("Guardar");
fc.showSaveDialog(null);
File archivo = new File(fc.getSelectedFile()+".txt");
try{
BufferedWriter salida = new BufferedWriter(new FileWriter(archivo));
salida.write(editor.obtenerTextoMatriz());
salida.close();
}catch(IOException ex){
System.err.println(ex);
}
}
}

View File

@ -0,0 +1,51 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Controlador;
import com.mycompany.editorascii.Vista.Editor;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author drdav
*/
public class GestiónTecladoEscribirCaracter implements KeyListener{
private Editor editor;
int x;
int y;
public GestiónTecladoEscribirCaracter(Editor parEditor, int parX, int parY){
editor = parEditor;
x = parX;
y = parY;
}
@Override
public void keyTyped(KeyEvent e) {
System.out.println(x + " " + y+ editor.obtenerTextoCelda(x, y));
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
recortarTexto();
}
public void recortarTexto(){
String textoCelda = editor.obtenerTextoCelda(x, y);
if(textoCelda.length() > 1){
editor.insertarTextoCelda(x, y, String.valueOf(textoCelda.charAt(textoCelda.length()-1)) );
}
}
}

View File

@ -0,0 +1,66 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Modelo;
/**
*
* @author drdav
*/
public class Dibujo {
private int alto;
private int ancho;
private char[][]lienzo = new char[ancho][alto];
public Dibujo(int alto, int ancho){
this.alto = alto;
this.ancho = ancho;
}
public void insertarCaracter(int x, int y, char letra){
lienzo[x][y] = letra;
}
public void llenarLienzo(char dibujo[][]){
lienzo = dibujo;
}
public void limpiarLienzo(){
for(int i = 0; i < ancho; i++){
for(int j = 0; j < alto; j++){
lienzo[i][j] = ' ';
}
}
}
public char obtenerCaracterEn(int x, int y){
return lienzo[x][y];
}
public int getAlto() {
return alto;
}
public void setAlto(int alto) {
this.alto = alto;
}
public int getAncho() {
return ancho;
}
public void setAncho(int ancho) {
this.ancho = ancho;
}
public char[][] getLienzo() {
return lienzo;
}
public void setLienzo(char[][] lienzo) {
this.lienzo = lienzo;
}
}

View File

@ -0,0 +1,221 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Vista;
import com.mycompany.editorascii.Controlador.GestiónBotónGuardarDibujo;
import com.mycompany.editorascii.Controlador.GestiónTecladoEscribirCaracter;
import com.mycompany.editorascii.Modelo.Dibujo;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author drdav
*/
public class Editor extends JFrame{
JPanel pEditor = new JPanel(new CardLayout());
JTextArea taEditor = new JTextArea();
JPanel pBotonesDer = new JPanel(new GridBagLayout());
JButton bGuardar = new JButton("Guardar");
JButton bBorrar = new JButton("Borrar");
JPanel pBotonesSup = new JPanel(new GridBagLayout());
JButton bRegresar = new JButton("Regresar");
JPanel pMatriz = new JPanel(new GridBagLayout());
Dibujo dibujo;
int x;
int y;
JTextField [][]celdas;
public Editor(int parX, int parY) throws HeadlessException {
super("Editar");
x = parX;
y = parY;
celdas = new JTextField[x][y];
dibujo = new Dibujo(x,y);
this.setLayout(new BorderLayout());
this.agregarComponentes();
this.agregarListeners();
//this.llenarTextarea(x, y);
this.crearCeldas(x, y);
this.limpiarCeldas();
iniciar();
}
public Editor(int parX, int parY, char parDibujo[][]){
super("Editar");
x = parX;
y = parY;
celdas = new JTextField[x][y];
dibujo = new Dibujo(x,y);
dibujo.llenarLienzo(parDibujo);
this.setLayout(new BorderLayout());
this.agregarComponentes();
this.agregarListeners();
//this.llenarTextarea(x, y);
this.crearCeldas(x, y);
llenarCeldas();
iniciar();
}
public void llenarCeldas(){
for (int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
celdas[i][j].setText( String.valueOf(dibujo.obtenerCaracterEn(i, j)) );
}
}
}
public void limpiarCeldas(){
for (int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
celdas[i][j].setText( "");
}
}
}
public String obtenerTextoMatriz(){
String texto = "";
for (int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
if("".equals(celdas[i][j].getText())){
texto = texto + " ";
}else{
texto = texto + celdas[i][j].getText();
}
}
texto = texto + '\n';
}
return texto;
}
public void agregarComponentes(){
pEditor.add(this.taEditor);
GridBagConstraints con = new GridBagConstraints();
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
pBotonesDer.add(this.bGuardar,con);
con.gridx = 0;
con.gridy = 1;
con.gridheight = 1;
con.gridwidth = 1;
pBotonesDer.add(this.bBorrar,con);
con.gridx = 0;
con.gridy = 1;
con.gridheight = 1;
con.gridwidth = 1;
pBotonesSup.add(this.bRegresar,con);
this.add(this.pEditor,BorderLayout.CENTER);
this.add(this.pBotonesDer,BorderLayout.EAST);
this.add(this.pBotonesSup,BorderLayout.NORTH);
}
public void agregarListeners(){
bRegresar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Menú menu = new Menú();
dispose();
}
});
this.bGuardar.addActionListener(new GestiónBotónGuardarDibujo(this));
this.bBorrar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
limpiarCeldas();
}
});
}
public void crearCeldas(int parX, int parY){
GridBagConstraints con = new GridBagConstraints();
JTextField tfAux;
for(int i = 0; i < parX; i++){
for(int j = 0; j < parY; j ++){
tfAux = new JTextField("");
tfAux.setColumns(1);
tfAux.addKeyListener(new GestiónTecladoEscribirCaracter(this,i,j));
con.gridx = j;
con.gridy = i;
con.gridheight = 1;
con.gridwidth = 1;
System.out.println(i+" "+j+ " "+celdas.length);
celdas[i][j] = tfAux;
pMatriz.add(celdas[i][j],con);
}
}
this.add(this.pMatriz,BorderLayout.CENTER);
}
public String obtenerTextoCelda(int posX, int posY){
return celdas[posX][posY].getText();
}
public void insertarTextoCelda(int posX, int posY, String texto){
celdas[posX][posY].setText(texto);
}
public void llenarTextarea(int parX, int parY){
this.add(this.pEditor,BorderLayout.CENTER);
taEditor.setBounds(0, 0, parX, parY);
for(int i = 0; i < parY; i++){
for(int j = 0; j < parX; j ++){
this.taEditor.append("X");
}
this.taEditor.append("\n");
}
}
public void iniciar(){
this.setBounds(100,100,700,700);
//this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@ -0,0 +1,69 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Vista;
import com.mycompany.editorascii.Controlador.GestiónBotónAbrirArchivo;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*
* @author drdav
*/
public class Menú extends JFrame{
JButton bNuevoDibujo = new JButton("Nuevo Dibujo");
JButton bAbrirDibujo = new JButton("Abrir Dibujo");
public Menú() {
super("Menú");
this.setLayout(new GridBagLayout());
agregarComponentes();
this.iniciar();
}
public void agregarComponentes(){
GridBagConstraints con = new GridBagConstraints();
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
this.add(this.bNuevoDibujo,con);
con.gridx = 0;
con.gridy = 2;
con.gridheight = 1;
con.gridwidth = 1;
this.add(this.bAbrirDibujo,con);
this.agregarListeners();
}
public void agregarListeners(){
JFrame aux = this;
bNuevoDibujo.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
VentanaTamaño ventanaTamaño = new VentanaTamaño(aux);
}
});
this.bAbrirDibujo.addActionListener(new GestiónBotónAbrirArchivo(this));
}
public void iniciar(){
this.setBounds(100,100,300,300);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@ -0,0 +1,162 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.editorascii.Vista;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author drdav
*/
public class VentanaTamaño extends JFrame{
JPanel pAlto = new JPanel(new GridBagLayout());
JLabel lAlto = new JLabel("Alto:");
JSpinner spAlto = new JSpinner();
JPanel pAncho = new JPanel(new GridBagLayout());
JLabel lAncho = new JLabel("Ancho:");
JSpinner spAncho = new JSpinner();
JPanel pBotones = new JPanel(new GridBagLayout());
JButton bAceptar = new JButton("Aceptar");
JButton bCancelar = new JButton("Cancelar");
JFrame ventanaJefe;
public VentanaTamaño(JFrame parVentana) throws HeadlessException {
super("Seleccionar Tamaño");
ventanaJefe = parVentana;
this.agregarComponentes();
this.agregarListeners();
iniciar();
}
public void agregarComponentes(){
GridBagConstraints con = new GridBagConstraints();
this.setLayout(new GridBagLayout());
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
this.add(pAlto,con);
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
pAlto.add(this.lAlto,con);
con.gridx = 0;
con.gridy = 1;
con.gridheight = 1;
con.gridwidth = 4;
pAlto.add(this.spAlto,con);
spAlto.setValue(4);
con.gridx = 0;
con.gridy = 1;
con.gridheight = 1;
con.gridwidth = 1;
this.add(pAncho,con);
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
pAncho.add(lAncho,con);
con.gridx = 0;
con.gridy = 1;
con.gridheight = 1;
con.gridwidth = 4;
pAncho.add(this.spAncho,con);
spAncho.setValue(4);
con.gridx = 0;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
pBotones.add(this.bAceptar,con);
con.gridx = 1;
con.gridy = 0;
con.gridheight = 1;
con.gridwidth = 1;
pBotones.add(this.bCancelar, con);
con.gridx = 0;
con.gridy = 2;
con.gridheight = 1;
con.gridwidth = 1;
this.add(this.pBotones,con);
}
public void agregarListeners(){
bAceptar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int x = Integer.valueOf(spAncho.getValue().toString());
int y = Integer.valueOf(spAlto.getValue().toString());
Editor editor = new Editor(x,y);
ventanaJefe.dispose();
dispose();
}
});
bCancelar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
spAlto.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
if((Integer)spAlto.getValue() < 3){
spAlto.setValue(3);
}
if((Integer)spAlto.getValue() > 30){
spAlto.setValue(30);
}
}
});
spAncho.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
if((Integer)spAncho.getValue() < 3){
spAncho.setValue(3);
}
if((Integer)spAncho.getValue() > 30){
spAncho.setValue(30);
}
}
});
}
public void iniciar(){
this.setBounds(100,100,400,400);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}