Compare commits
No commits in common. "0ba2f59e5567aa20a63a55d194bbbda93c491f1c" and "8b719d1091238a5214ef4d4328eb1b8b146a58c7" have entirely different histories.
0ba2f59e55
...
8b719d1091
|
@ -1,258 +0,0 @@
|
||||||
package imagetoascii;
|
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Container;
|
|
||||||
import java.awt.Font;
|
|
||||||
import java.awt.GridLayout;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JDialog;
|
|
||||||
import javax.swing.JFileChooser;
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JLabel;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JScrollPane;
|
|
||||||
import javax.swing.JTextArea;
|
|
||||||
import javax.swing.JTextField;
|
|
||||||
import javax.swing.text.AttributeSet;
|
|
||||||
import javax.swing.text.SimpleAttributeSet;
|
|
||||||
import javax.swing.text.StyleConstants;
|
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
||||||
import javax.swing.text.BadLocationException;
|
|
||||||
import javax.swing.text.Document;
|
|
||||||
import javax.swing.SwingUtilities;
|
|
||||||
|
|
||||||
|
|
||||||
public class ImageToAscii extends JFrame {
|
|
||||||
private JTextArea asciiTextArea;
|
|
||||||
|
|
||||||
public ImageToAscii() {
|
|
||||||
setTitle("Image to ASCII");
|
|
||||||
setSize(800, 600);
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
|
|
||||||
// Obtener el contentPane y establecer un BorderLayout con un color de fondo
|
|
||||||
Container contentPane = getContentPane();
|
|
||||||
contentPane.setLayout(new BorderLayout());
|
|
||||||
contentPane.setBackground(Color.LIGHT_GRAY);
|
|
||||||
|
|
||||||
// Crear el panel para los botones con un fondo verde claro
|
|
||||||
JPanel buttonPanel = new JPanel();
|
|
||||||
buttonPanel.setBackground(new Color(200, 120, 255));
|
|
||||||
|
|
||||||
// Crear los botones con colores personalizados
|
|
||||||
JButton selectImageButton = createStyledButton("Seleccionar Imagen", Color.BLACK);
|
|
||||||
JButton clearButton = createStyledButton("Borrar", Color.RED);
|
|
||||||
JButton convertToTxtButton = createStyledButton("Convertir a TXT", Color.BLUE);
|
|
||||||
|
|
||||||
// Agregar los botones al panel de botones
|
|
||||||
buttonPanel.add(selectImageButton);
|
|
||||||
buttonPanel.add(clearButton);
|
|
||||||
buttonPanel.add(convertToTxtButton);
|
|
||||||
|
|
||||||
// Agregar el panel de botones en la región NORTH del BorderLayout
|
|
||||||
contentPane.add(buttonPanel, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Crear el área de texto para mostrar el arte ASCII con un fondo blanco
|
|
||||||
asciiTextArea = new JTextArea();
|
|
||||||
asciiTextArea.setEditable(false);
|
|
||||||
asciiTextArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
|
|
||||||
asciiTextArea.setBackground(Color.WHITE);
|
|
||||||
|
|
||||||
// Crear un JScrollPane para permitir el desplazamiento
|
|
||||||
JScrollPane scrollPane = new JScrollPane(asciiTextArea);
|
|
||||||
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
|
||||||
|
|
||||||
// Agregar el JScrollPane en la región CENTER del BorderLayout
|
|
||||||
contentPane.add(scrollPane, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleButtonClick(String buttonText) {
|
|
||||||
// Implementa el manejo de clics de botón según el texto del botón
|
|
||||||
switch (buttonText) {
|
|
||||||
case "Seleccionar Imagen":
|
|
||||||
showImageDialog();
|
|
||||||
break;
|
|
||||||
case "Borrar":
|
|
||||||
clearAsciiTextArea();
|
|
||||||
break;
|
|
||||||
case "Convertir a TXT":
|
|
||||||
saveAsTxt();
|
|
||||||
break;
|
|
||||||
// Agrega más casos según sea necesario
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JButton createStyledButton(String text, Color color) {
|
|
||||||
JButton button = new JButton(text);
|
|
||||||
button.setBackground(color);
|
|
||||||
button.setForeground(Color.WHITE);
|
|
||||||
button.addActionListener(e -> handleButtonClick(text)); // Reemplaza handleButtonClick con el manejo apropiado
|
|
||||||
return button;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showImageDialog() {
|
|
||||||
JDialog dialog = new JDialog(this, "Configurar Dimensiones de la Imagen", true);
|
|
||||||
dialog.setLayout(new GridLayout(3, 2));
|
|
||||||
|
|
||||||
JLabel widthLabel = new JLabel("Ancho:");
|
|
||||||
JTextField widthField = new JTextField();
|
|
||||||
JLabel heightLabel = new JLabel("Alto:");
|
|
||||||
JTextField heightField = new JTextField();
|
|
||||||
|
|
||||||
JButton okButton = new JButton("Aceptar");
|
|
||||||
okButton.addActionListener(e -> {
|
|
||||||
int width = Integer.parseInt(widthField.getText());
|
|
||||||
int height = Integer.parseInt(heightField.getText());
|
|
||||||
dialog.dispose();
|
|
||||||
selectImageAndConvert(width, height);
|
|
||||||
});
|
|
||||||
|
|
||||||
JButton cancelButton = new JButton("Cancelar");
|
|
||||||
cancelButton.addActionListener(e -> dialog.dispose());
|
|
||||||
|
|
||||||
dialog.add(widthLabel);
|
|
||||||
dialog.add(widthField);
|
|
||||||
dialog.add(heightLabel);
|
|
||||||
dialog.add(heightField);
|
|
||||||
dialog.add(okButton);
|
|
||||||
dialog.add(cancelButton);
|
|
||||||
|
|
||||||
dialog.pack();
|
|
||||||
dialog.setLocationRelativeTo(this);
|
|
||||||
dialog.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void selectImageAndConvert(int targetWidth, int targetHeight) {
|
|
||||||
// Crear un JFileChooser
|
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
|
||||||
fileChooser.setDialogTitle("Seleccione una imagen");
|
|
||||||
|
|
||||||
// Filtro para mostrar solo archivos de imagen
|
|
||||||
FileNameExtensionFilter imageFilter = new FileNameExtensionFilter("Archivos de imagen", "jpg", "png", "jpeg", "gif");
|
|
||||||
fileChooser.setFileFilter(imageFilter);
|
|
||||||
|
|
||||||
// Mostrar el diálogo de selección de archivo
|
|
||||||
int result = fileChooser.showOpenDialog(null);
|
|
||||||
|
|
||||||
// Verificar si se seleccionó un archivo
|
|
||||||
if (result == JFileChooser.APPROVE_OPTION) {
|
|
||||||
// Obtener la ruta de la imagen seleccionada
|
|
||||||
String imagePath = fileChooser.getSelectedFile().getAbsolutePath();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Cargar la imagen
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File(imagePath));
|
|
||||||
|
|
||||||
// Escalar la imagen a las dimensiones especificadas
|
|
||||||
BufferedImage scaledImage = scaleImage(originalImage, targetWidth, targetHeight);
|
|
||||||
|
|
||||||
// Obtener las dimensiones de la imagen escalada
|
|
||||||
int width = scaledImage.getWidth();
|
|
||||||
int height = scaledImage.getHeight();
|
|
||||||
|
|
||||||
// Crear un StringBuilder para construir el arte ASCII
|
|
||||||
StringBuilder asciiArt = new StringBuilder();
|
|
||||||
|
|
||||||
// Iterar sobre cada píxel y convertir a arte ASCII
|
|
||||||
for (int y = 0; y < height; y += 2) {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
// Obtener el color del píxel
|
|
||||||
Color color = new Color(scaledImage.getRGB(x, y));
|
|
||||||
|
|
||||||
// Calcular el nivel de gris (promedio de los componentes RGB)
|
|
||||||
int gray = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
|
|
||||||
|
|
||||||
// Convertir el nivel de gris a un carácter ASCII
|
|
||||||
char asciiChar = getAsciiChar(gray);
|
|
||||||
|
|
||||||
// Agregar el carácter al StringBuilder
|
|
||||||
asciiArt.append(asciiChar);
|
|
||||||
}
|
|
||||||
// Agregar un salto de línea al final de cada fila
|
|
||||||
asciiArt.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mostrar el arte ASCII en el JTextArea
|
|
||||||
asciiTextArea.setText(asciiArt.toString());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clearAsciiTextArea() {
|
|
||||||
// Borrar el contenido del JTextArea
|
|
||||||
asciiTextArea.setText("");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveAsTxt() {
|
|
||||||
// Crear un JFileChooser para seleccionar la ubicación de guardado
|
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
|
||||||
fileChooser.setDialogTitle("Guardar como archivo de texto");
|
|
||||||
fileChooser.setSelectedFile(new File("ascii_art.txt"));
|
|
||||||
|
|
||||||
// Mostrar el diálogo de guardado
|
|
||||||
int result = fileChooser.showSaveDialog(null);
|
|
||||||
|
|
||||||
// Verificar si se seleccionó una ubicación de guardado
|
|
||||||
if (result == JFileChooser.APPROVE_OPTION) {
|
|
||||||
// Obtener el archivo seleccionado
|
|
||||||
File outputFile = fileChooser.getSelectedFile();
|
|
||||||
|
|
||||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
|
|
||||||
// Guardar el contenido del JTextArea en el archivo
|
|
||||||
writer.write(asciiTextArea.getText());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private BufferedImage scaleImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
|
||||||
BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
|
||||||
java.awt.Graphics2D g = scaledImage.createGraphics();
|
|
||||||
g.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
|
||||||
g.dispose();
|
|
||||||
return scaledImage;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void appendColoredText(String text, Color color) {
|
|
||||||
SimpleAttributeSet attrs = new SimpleAttributeSet();
|
|
||||||
StyleConstants.setForeground(attrs, color);
|
|
||||||
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
Document doc = asciiTextArea.getDocument();
|
|
||||||
try {
|
|
||||||
doc.insertString(doc.getLength(), text, attrs);
|
|
||||||
} catch (BadLocationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void appendColoredChar(char character, Color color) {
|
|
||||||
appendColoredText(String.valueOf(character), color);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static char getAsciiChar(int gray) {
|
|
||||||
// Ajusta estos valores según tu preferencia
|
|
||||||
char[] asciiChars = {'@', '#', '8', '&', 'o', ':', '*', '.', ' '};
|
|
||||||
int index = gray * (asciiChars.length - 1) / 255;
|
|
||||||
return asciiChars[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
new ImageToAscii();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class Primero {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
System.out.println("chicharito");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue