Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.7 kB
6
Indexable
Never
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class MakeImage {
    public static void main(String[] args) {
        File folder = new File("images/tiles"); // specify the path to your folder
        File[] imageFiles = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".png"));
        if (imageFiles == null || imageFiles.length == 0) {
            System.out.println("No images found in the specified folder.");
            return;
        }

        int rows = 12;
        int cols = 12;
        int cellWidth = 100;  // Change this to your desired cell width
        int cellHeight = 100; // Change this to your desired cell height
        BufferedImage collage = new BufferedImage(cols * cellWidth, rows * cellHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics g = collage.getGraphics();

        Random random = new Random();

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                File randomImageFile = imageFiles[random.nextInt(imageFiles.length)];
                try {
                    BufferedImage image = ImageIO.read(randomImageFile);
                    g.drawImage(image, col * cellWidth, row * cellHeight, cellWidth, cellHeight, null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        g.dispose();
        try {
            ImageIO.write(collage, "png", new File("collage.png"));
            System.out.println("Collage created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Leave a Comment