From 47b2006ef072b89344a13178e3367892fbf04521 Mon Sep 17 00:00:00 2001 From: prunzinho Date: Wed, 28 Feb 2024 08:07:44 -0600 Subject: [PATCH] Proyecto Ascii Art by UVinci --- AsciiArt/build.xml | 73 + AsciiArt/manifest.mf | 3 + AsciiArt/nbproject/build-impl.xml | 1771 +++++++++++++++++++ AsciiArt/nbproject/genfiles.properties | 8 + AsciiArt/nbproject/project.properties | 95 + AsciiArt/nbproject/project.xml | 15 + AsciiArt/src/asciiart/EditorAsciiArt.java | 248 +++ AsciiArt/src/asciiart/Principal.java | 65 + AsciiArt/src/asciiart/SeleccionarImage.java | 89 + AsciiArt/src/asciiart/TransformarAscii.java | 209 +++ 10 files changed, 2576 insertions(+) create mode 100644 AsciiArt/build.xml create mode 100644 AsciiArt/manifest.mf create mode 100644 AsciiArt/nbproject/build-impl.xml create mode 100644 AsciiArt/nbproject/genfiles.properties create mode 100644 AsciiArt/nbproject/project.properties create mode 100644 AsciiArt/nbproject/project.xml create mode 100644 AsciiArt/src/asciiart/EditorAsciiArt.java create mode 100644 AsciiArt/src/asciiart/Principal.java create mode 100644 AsciiArt/src/asciiart/SeleccionarImage.java create mode 100644 AsciiArt/src/asciiart/TransformarAscii.java diff --git a/AsciiArt/build.xml b/AsciiArt/build.xml new file mode 100644 index 0000000..2cde3f6 --- /dev/null +++ b/AsciiArt/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project AsciiArt. + + + diff --git a/AsciiArt/manifest.mf b/AsciiArt/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/AsciiArt/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/AsciiArt/nbproject/build-impl.xml b/AsciiArt/nbproject/build-impl.xml new file mode 100644 index 0000000..800b8bc --- /dev/null +++ b/AsciiArt/nbproject/build-impl.xml @@ -0,0 +1,1771 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AsciiArt/nbproject/genfiles.properties b/AsciiArt/nbproject/genfiles.properties new file mode 100644 index 0000000..bf005ee --- /dev/null +++ b/AsciiArt/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=99eed0a2 +build.xml.script.CRC32=23435b25 +build.xml.stylesheet.CRC32=f85dc8f2@1.106.0.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=99eed0a2 +nbproject/build-impl.xml.script.CRC32=3cf7f477 +nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.106.0.48 diff --git a/AsciiArt/nbproject/project.properties b/AsciiArt/nbproject/project.properties new file mode 100644 index 0000000..df1c190 --- /dev/null +++ b/AsciiArt/nbproject/project.properties @@ -0,0 +1,95 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.modulepath=\ + ${run.modulepath} +debug.test.classpath=\ + ${run.test.classpath} +debug.test.modulepath=\ + ${run.test.modulepath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/AsciiArt.jar +dist.javadoc.dir=${dist.dir}/javadoc +dist.jlink.dir=${dist.dir}/jlink +dist.jlink.output=${dist.jlink.dir}/AsciiArt +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.modulepath= +javac.processormodulepath= +javac.processorpath=\ + ${javac.classpath} +javac.source=17 +javac.target=17 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.modulepath=\ + ${javac.modulepath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.html5=false +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +# The jlink additional root modules to resolve +jlink.additionalmodules= +# The jlink additional command line parameters +jlink.additionalparam= +jlink.launcher=true +jlink.launcher.name=AsciiArt +main.class=asciiart.Principal +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.modulepath=\ + ${javac.modulepath} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +run.test.modulepath=\ + ${javac.test.modulepath} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/AsciiArt/nbproject/project.xml b/AsciiArt/nbproject/project.xml new file mode 100644 index 0000000..f06a7b3 --- /dev/null +++ b/AsciiArt/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + AsciiArt + + + + + + + + + diff --git a/AsciiArt/src/asciiart/EditorAsciiArt.java b/AsciiArt/src/asciiart/EditorAsciiArt.java new file mode 100644 index 0000000..a32d509 --- /dev/null +++ b/AsciiArt/src/asciiart/EditorAsciiArt.java @@ -0,0 +1,248 @@ +package asciiart; + +/* + * 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 + */ + +/** + * + * @author mario + */ +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.awt.event.*; +import java.io.*; +import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; + +class PanelImagen extends JPanel { + private BufferedImage imagenOriginal; + private BufferedImage imagenEscalaGrises; + private BufferedImage imagenMostrada; + private float opacidad = 1.0f; + private JTextArea textArea; + + public PanelImagen() { + setBackground(Color.WHITE); + setLayout(new BorderLayout()); + textArea = new JTextArea() { + @Override + protected void paintComponent(Graphics g) { + if (!isOpaque() && getBackground().getAlpha() < 255) { + g.setColor(getBackground()); + g.fillRect(0, 0, getWidth(), getHeight()); + } + super.paintComponent(g); + } + }; + textArea.setFont(new Font("Monospaced", Font.PLAIN, 12)); + textArea.setOpaque(false); + add(textArea, BorderLayout.CENTER); + } + + public void establecerImagenFondo(BufferedImage imagen) { + imagenOriginal = imagen; + imagenEscalaGrises = convertirAEscalaDeGrises(imagen); + imagenMostrada = aplicarOpacidad(imagenEscalaGrises, opacidad); + repaint(); + } + + private BufferedImage convertirAEscalaDeGrises(BufferedImage imagen) { + BufferedImage gris = new BufferedImage(imagen.getWidth(), imagen.getHeight(), BufferedImage.TYPE_BYTE_GRAY); + Graphics g = gris.getGraphics(); + g.drawImage(imagen, 0, 0, null); + g.dispose(); + return gris; + } + + private BufferedImage aplicarOpacidad(BufferedImage imagen, float opacidad) { + BufferedImage resultado = new BufferedImage(imagen.getWidth(), imagen.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = resultado.createGraphics(); + AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacidad); + g2d.setComposite(alphaComposite); + g2d.drawImage(imagen, 0, 0, null); + g2d.dispose(); + return resultado; + } + + public void establecerOpacidad(float opacidad) { + if (imagenEscalaGrises != null) { + this.opacidad = opacidad / 100.0f; + imagenMostrada = aplicarOpacidad(imagenEscalaGrises, this.opacidad); + repaint(); + } + } + + public JTextArea getTextArea() { + return textArea; + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + if (imagenMostrada != null) { + g.drawImage(imagenMostrada, 0, 0, getWidth(), getHeight(), this); + } + } +} + +public class EditorAsciiArt extends JFrame { + private int filas = 10; + private int columnas = 10; + private PanelImagen panelImagen; + private JSlider deslizadorOpacidad; + private JTextField campoFilas; + private JTextField campoColumnas; + + public EditorAsciiArt() { + setTitle("Editor de Arte ASCII"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(new BorderLayout()); + + JPanel panelIzquierdo = new JPanel(); + panelIzquierdo.setLayout(new BoxLayout(panelIzquierdo, BoxLayout.Y_AXIS)); + + JButton botonImagenFondo = new JButton("Seleccionar Imagen Fondo"); + botonImagenFondo.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + seleccionarImagenFondo(); + } + }); + panelIzquierdo.add(botonImagenFondo); + + JButton botonGuardar = new JButton("Guardar ASCII Art"); + botonGuardar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + guardarASCIIArt(); + } + }); + panelIzquierdo.add(botonGuardar); + + JButton botonCopiar = new JButton("Copiar ASCII Art"); + botonCopiar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + copiarASCIIArt(); + } + }); + panelIzquierdo.add(botonCopiar); + + JButton botonCargar = new JButton("Cargar ASCII Art"); + botonCargar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + cargarASCIIArt(); + } + }); + panelIzquierdo.add(botonCargar); + + panelIzquierdo.add(new JLabel("Opacidad:")); + deslizadorOpacidad = new JSlider(0, 100, 100); + deslizadorOpacidad.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + int valor = deslizadorOpacidad.getValue(); + panelImagen.establecerOpacidad(valor); + } + }); + panelIzquierdo.add(deslizadorOpacidad); + + panelIzquierdo.add(Box.createVerticalGlue()); + + JButton botonHome = new JButton("Inicio"); + botonHome.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e){ + regresarInicio(); + } + }); + + panelIzquierdo.add(botonHome); + + add(panelIzquierdo, BorderLayout.WEST); + + panelImagen = new PanelImagen(); + add(panelImagen, BorderLayout.CENTER); + + setSize(800, 600); + setLocationRelativeTo(null); + setVisible(true); + } + + private void seleccionarImagenFondo() { + JFileChooser fileChooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos de Imagen", "jpg", "jpeg", "png", "gif"); + fileChooser.setFileFilter(filter); + int returnVal = fileChooser.showOpenDialog(this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File file = fileChooser.getSelectedFile(); + try { + BufferedImage imagen = ImageIO.read(file); + panelImagen.establecerImagenFondo(imagen); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + + private void guardarASCIIArt() { + try { + JTextArea textArea = panelImagen.getTextArea(); + String asciiArt = textArea.getText(); + File file = new File("asciiart.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(asciiArt); + writer.close(); + JOptionPane.showMessageDialog(this, "ASCII Art guardado exitosamente."); + } catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(this, "Error al guardar el ASCII Art.", "Error", JOptionPane.ERROR_MESSAGE); + } + } + + private void copiarASCIIArt() { + JTextArea textArea = panelImagen.getTextArea(); + StringSelection stringSelection = new StringSelection(textArea.getText()); + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + clipboard.setContents(stringSelection, null); + JOptionPane.showMessageDialog(this, "ASCII Art copiado al portapapeles."); + } + + private void cargarASCIIArt() { + try { + File file = new File("asciiart.txt"); + BufferedReader reader = new BufferedReader(new FileReader(file)); + StringBuilder asciiArtBuilder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + asciiArtBuilder.append(line); + asciiArtBuilder.append("\n"); + } + reader.close(); + panelImagen.getTextArea().setText(asciiArtBuilder.toString()); + } catch (Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(this, "Error al cargar el ASCII Art.", "Error", JOptionPane.ERROR_MESSAGE); + } + } + + private void regresarInicio(){ + Principal nuevaVentana = new Principal(); + nuevaVentana.setVisible(true); + dispose(); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + new EditorAsciiArt(); + }); + } +} \ No newline at end of file diff --git a/AsciiArt/src/asciiart/Principal.java b/AsciiArt/src/asciiart/Principal.java new file mode 100644 index 0000000..f19d1ae --- /dev/null +++ b/AsciiArt/src/asciiart/Principal.java @@ -0,0 +1,65 @@ +/* + * 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 asciiart; + +/** + * + * @author mario + */ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Principal extends JFrame { + public Principal() { + setTitle("ASCII Art Converter"); // Título original + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); // Establecer un diseño nulo para usar setBounds + + // Creación de botones + JButton asciiArtButton = new JButton("Crear ASCII Art"); + asciiArtButton.setBounds(50, 60, 150, 30); // Establecer posición y tamaño + asciiArtButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // Abre la clase AsciiArt + new EditorAsciiArt().setVisible(true); + dispose(); // Cierra el menú principal + } + }); + + JButton imageToAsciiButton = new JButton("Transformar imagen a ASCII Art"); + imageToAsciiButton.setBounds(220, 60, 220, 30); // Establecer posición y tamaño + imageToAsciiButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // Abre la clase SeleccionarImage + new SeleccionarImage().setVisible(true); + dispose(); // Cierra el menú principal + } + }); + + // Agregar botones al JFrame + add(asciiArtButton); + add(imageToAsciiButton); + + setSize(500, 150); // Tamaño del JFrame + setLocationRelativeTo(null); // Centrar en la pantalla + + // Label con el título (agregado después de setSize y setLocationRelativeTo) + JLabel titleLabel = new JLabel("ASCII ART by UVinci", SwingConstants.CENTER); + titleLabel.setFont(new Font("Arial", Font.BOLD, 24)); // Establecer fuente y tamaño + titleLabel.setForeground(Color.BLACK); // Establecer color de texto a negro + titleLabel.setBounds(0, 10, getWidth(), 30); // Centrar y establecer posición + add(titleLabel); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + new Principal().setVisible(true); + }); + } +} \ No newline at end of file diff --git a/AsciiArt/src/asciiart/SeleccionarImage.java b/AsciiArt/src/asciiart/SeleccionarImage.java new file mode 100644 index 0000000..d313a35 --- /dev/null +++ b/AsciiArt/src/asciiart/SeleccionarImage.java @@ -0,0 +1,89 @@ +package asciiart; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; + +public class SeleccionarImage extends JFrame { + + private static final String CHARACTERS = "a%k*t=-:. "; + String imagePath = ""; + int width = 0; + int height = 0; + + public SeleccionarImage() { + initComponents(); + } + + private void initComponents() { + JButton seleccionarBoton = new JButton("Seleccionar Imagen"); + seleccionarBoton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos de Imagen", "jpg", "jpeg", "png", "gif"); + fileChooser.setFileFilter(filter); + int result = fileChooser.showOpenDialog(SeleccionarImage.this); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + imagePath = selectedFile.getAbsolutePath(); // Actualizar la variable imagePath + generarASCII(imagePath); // Llamar al método para generar el arte ASCII + } + } + }); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + getContentPane().add(seleccionarBoton, "North"); + pack(); + setLocationRelativeTo(null); + } + + private void generarASCII(String imagePath) { + try { + BufferedImage image = ImageIO.read(new File(imagePath)); + String asciiArt = convertToASCII(image); + TransformarAscii nuevaVentana = new TransformarAscii(width,height,asciiArt); // Suponiendo que CrearAscii es el nombre de tu otra ventana + nuevaVentana.setVisible(true); + dispose(); + + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private String convertToASCII(BufferedImage image) { + StringBuilder asciiArt = new StringBuilder(); + + width = image.getWidth(); + height = image.getHeight(); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + //Obtener el color del píxel en formato RGB + int color = image.getRGB(x, y); + //Convertir el color a escala de grises + int gray = (int) (0.21 * ((color >> 16) & 0xff) + 0.72 * ((color >> 8) & 0xff) + 0.07 * (color & 0xff)); + //Calcular el índice del carácter en función del nivel de gris + int index = (int) ((gray / 255.0) * (CHARACTERS.length() - 1)); + asciiArt.append(CHARACTERS.charAt(index)); + + } + // Agregar un salto de línea al final de cada fila + asciiArt.append("\n"); + } + + return asciiArt.toString(); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + new SeleccionarImage().setVisible(true); + } + }); + } +} \ No newline at end of file diff --git a/AsciiArt/src/asciiart/TransformarAscii.java b/AsciiArt/src/asciiart/TransformarAscii.java new file mode 100644 index 0000000..a522355 --- /dev/null +++ b/AsciiArt/src/asciiart/TransformarAscii.java @@ -0,0 +1,209 @@ +package asciiart; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.datatransfer.*; +import java.io.*; + +public class TransformarAscii extends javax.swing.JFrame { + int width = 0; + int height = 0; + String copia = ""; + String ruta = "C:/Users/CDGN_/Desktop/ProyectoFinal/frontend/"; + + public TransformarAscii() { + initComponents(); + + } + + public TransformarAscii(int width, int height, String asci) { + initComponents(); + this.width = width; + this.height = height; + this.setSize(600, 600); + Font font = new Font(Font.MONOSPACED, Font.PLAIN, 12); // Fuente de ancho fijo + AsciiText.setFont(font); + AsciiText.setText(asci); + copia = asci; + JScrollPane scrollPane = new JScrollPane(AsciiText); + jPanel1.add(scrollPane); + } + + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jMenuBar1 = new javax.swing.JMenuBar(); + jMenu1 = new javax.swing.JMenu(); + jMenu2 = new javax.swing.JMenu(); + AsciiText1 = new javax.swing.JTextField(); + jPanel1 = new javax.swing.JPanel(); + JPanel zoomPanel = new JPanel(new FlowLayout()); + JPanel actionPanel = new JPanel(new FlowLayout()); + AsciiText = new javax.swing.JTextArea(); + JButton copiar = new JButton("Copiar"); + JButton guardar = new JButton("Guardar"); + JButton zoomInButton = new JButton("Mas Zoom"); + JButton SeleccionarImagen = new JButton("Seleccionar Imagen"); + JButton zoomOutButton = new JButton("Menos Zoom"); + JButton BotonHome = new JButton("Inicio"); + JButton BotonReduccion = new JButton("Reducir Signos"); + + zoomInButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Font currentFont = AsciiText.getFont(); + Font newFont = currentFont.deriveFont(currentFont.getSize() + 2f); + AsciiText.setFont(newFont); + } + }); + + zoomOutButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Font currentFont = AsciiText.getFont(); + Font newFont = currentFont.deriveFont(currentFont.getSize() - 2f); + AsciiText.setFont(newFont); + } + }); + + copiar.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e){ + copiarAlPortapapeles(copia); + } + }); + + guardar.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e){ + String nombrearchivo = "asciiArt1.txt"; + guardarEnArchivo(copia,ruta, nombrearchivo); + } + }); + + SeleccionarImagen.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e){ + SeleccionarImagenMetodo(); + } + }); + + BotonHome.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e){ + regresarInicio(); + } + }); + + BotonReduccion.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println(reduceAscii(copia)); + + } + }); + + jMenu1.setText("File"); + jMenuBar1.add(jMenu1); + + jMenu2.setText("Edit"); + jMenuBar1.add(jMenu2); + + AsciiText1.setText("a"); + AsciiText1.setToolTipText(""); + AsciiText1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AsciiText1ActionPerformed(evt); + } + }); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jPanel1.setLayout(new BorderLayout()); + jPanel1.add(AsciiText, BorderLayout.CENTER); + + zoomPanel.add(zoomInButton); + zoomPanel.add(zoomOutButton); + actionPanel.add(copiar); + actionPanel.add(guardar); + actionPanel.add(SeleccionarImagen); + actionPanel.add(BotonHome); + jPanel1.add(zoomPanel, BorderLayout.NORTH); + jPanel1.add(actionPanel, BorderLayout.SOUTH); + + + getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); + + pack(); + + }// + + private void AsciiText1ActionPerformed(java.awt.event.ActionEvent evt) { + } + + public static void main(String args[]) { + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new TransformarAscii().setVisible(true); + } + }); + } + + + public static void copiarAlPortapapeles(String texto) { + // Obtiene el Toolkit del sistema + Toolkit toolkit = Toolkit.getDefaultToolkit(); + Clipboard clipboard = toolkit.getSystemClipboard(); + StringSelection strSel = new StringSelection(texto); + clipboard.setContents(strSel, null); + } + + public static void guardarEnArchivo(String texto, String rutaCarpeta, String nombreArchivo) { + try { + File carpeta = new File(rutaCarpeta); + if (!carpeta.exists()) { + carpeta.mkdirs(); + } + String rutaCompleta = rutaCarpeta + nombreArchivo; + FileWriter writer = new FileWriter(rutaCompleta); + writer.write(texto); + writer.close(); + System.out.println("Texto guardado en el archivo: " + rutaCompleta); + } catch (IOException e) { + System.out.println("Error al guardar el archivo: " + e.getMessage()); + } + } + + private void SeleccionarImagenMetodo(){ + new SeleccionarImage().setVisible(true); + dispose(); + } + + private void regresarInicio(){ + Principal nuevaVentana = new Principal(); + nuevaVentana.setVisible(true); + dispose(); + } + + private String reduceAscii(String ascii) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < ascii.length(); i++) { + char currentChar = ascii.charAt(i); + sb.append(currentChar); + if (i % 2 == 0 && currentChar == '.') { // Elimina un punto cada dos símbolos + sb.deleteCharAt(sb.length() - 2); + } + } + return sb.toString(); + } + + private javax.swing.JTextArea AsciiText; + private javax.swing.JTextField AsciiText1; + private javax.swing.JMenu jMenu1; + private javax.swing.JMenu jMenu2; + private javax.swing.JMenuBar jMenuBar1; + private javax.swing.JPanel jPanel1; +}