Last active
June 27, 2020 00:27
-
-
Save javiluli/bc2d4ec27773cdce123d70f6255f22bd to your computer and use it in GitHub Desktop.
Generar imagenes en formato PNG desde la implementacion de graficos con Java.Graphics2D en una interfaz grafica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package GuardarCanvasImagen; | |
import java.awt.Color; | |
import java.awt.Graphics2D; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Random; | |
import javax.imageio.ImageIO; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
public class GuardarCanvasImagen { | |
private JFrame frame; | |
JLabel lblWallpaper; | |
// Tamaño de la imagen que se generara (pixeles). | |
final int WIDHT_IMG = 1280; | |
final int HEIGHT_IMG = 720; | |
/* | |
* Tamaño de un pixel a dibujar. Dado que un pixel se entiende como un cuadrado, | |
* este tendra todos los lados del mismo tamaño. | |
*/ | |
final int DIMENSION_PIXEL = 20; | |
// Cantida maxima de cuadrados por la altura y la anchura. | |
final int NUM_MAX_WIDHT = WIDHT_IMG / DIMENSION_PIXEL; | |
final int NUM_MAX_HEIGHT = HEIGHT_IMG / DIMENSION_PIXEL; | |
// Direccion de la imagen generada. | |
final String DIREC_WALLPAPER = "src//wallpaper.png"; | |
/* | |
* Mantiene la representacion de una imagen en memoria mientras se ejecurta la | |
* aplicacion. Para esta aplicion se usara mas en concreto para almacenar los | |
* Graphics2D generados con un color y una posicion concreta. | |
*/ | |
BufferedImage bimg = new BufferedImage(WIDHT_IMG, HEIGHT_IMG, BufferedImage.TYPE_INT_RGB); | |
/** | |
* Genera un Color aleatorio con sus 3 indices RGB. | |
*/ | |
public Color setRandomColor() { | |
Random rng = new Random(); | |
int r = rng.nextInt(255); // Rojo [r] | |
int g = rng.nextInt(255); // Verde [g] | |
int b = rng.nextInt(255); // Azul [b] | |
return new Color(r, g, b); | |
} | |
/** | |
* Genera los graficos en el JFrame y los almacena en un BufferedImage. | |
*/ | |
public void generarGraficos() { | |
Graphics2D graphic = bimg.createGraphics(); | |
for (int i = 0; i < NUM_MAX_WIDHT; i++) { | |
for (int j = 0; j < NUM_MAX_HEIGHT; j++) { | |
graphic.setColor(setRandomColor()); | |
// | Coordenada X | Coordenada Y | Ancho [W] | Alto [H] | | |
graphic.fillRect(i * DIMENSION_PIXEL, j * DIMENSION_PIXEL, DIMENSION_PIXEL, DIMENSION_PIXEL); | |
} | |
} | |
graphic.dispose(); // finaliza los graficos que se dibujan. | |
} | |
/** | |
* Guardar los graficos almacenados en el BufferedImage en un archivo PNG. | |
*/ | |
public void guardarGraficosEnImagen() { | |
File f = new File(DIREC_WALLPAPER); | |
try { | |
ImageIO.write(bimg, "png", f); // <--- | Aqui es donde se crea la imagen en formato PNG | |
} catch (IOException e) { | |
e.toString(); | |
} | |
} | |
// ================= METODO MAIN ==================== | |
public static void main(String[] args) { | |
GuardarCanvasImagen window = new GuardarCanvasImagen(); | |
window.frame.setVisible(true); | |
} | |
// ================================================== | |
/** | |
* Constructor de la clase Main para iniciar la aplicacion. | |
*/ | |
public GuardarCanvasImagen() { | |
initialize(); | |
generarGraficos(); | |
guardarGraficosEnImagen(); | |
lblWallpaper.setIcon(new ImageIcon(DIREC_WALLPAPER)); | |
} | |
/** | |
* Inicializacion de los componenes del frame. | |
*/ | |
private void initialize() { | |
frame = new JFrame("Generador de imagenes con Graphics2D"); | |
frame.setResizable(false); | |
frame.setBounds(100, 100, 1296, 759); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLocationRelativeTo(null); | |
frame.getContentPane().setLayout(null); | |
lblWallpaper = new JLabel(); | |
lblWallpaper.setBounds(0, 0, WIDHT_IMG, HEIGHT_IMG); | |
frame.getContentPane().add(lblWallpaper); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment