import javax.swing.*; // 引入javax.swing包,用于创建图形用户界面组件
import java.awt.*; // 引入java.awt包,用于绘制组件
import java.awt.image.BufferedImage; // 引入BufferedImage类,用于处理图像
import java.io.File; // 引入File类,用于文件操作
import java.io.IOException; // 引入IOException类,用于处理IO异常
import javax.imageio.ImageIO; // 引入ImageIO类,用于读取图像文件

public class PhotoWall extends JPanel {
    private BufferedImage[] images; // 存储图像的数组
    private int rows; // 照片墙的行数
    private int cols; // 照片墙的列数

    // 构造方法,接收图像路径数组、行数和列数
    public PhotoWall(String[] imagePaths, int rows, int cols) throws IOException {
        this.rows = rows; // 初始化行数
        this.cols = cols; // 初始化列数
        images = new BufferedImage[imagePaths.length]; // 创建存储图像的数组
        
        // 遍历图像路径数组,读取每张图像
        for (int i = 0; i < imagePaths.length; i++) {
            images[i] = ImageIO.read(new File(imagePaths[i])); // 读取图像文件并存储到数组中
        }
    }

    // 重写paintComponent方法,绘制组件
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // 调用父类的paintComponent方法
        int width = getWidth() / cols; // 计算每个图像的宽度
        int height = getHeight() / rows; // 计算每个图像的高度

        // 遍历每一行
        for (int row = 0; row < rows; row++) {
            // 遍历每一列
            for (int col = 0; col < cols; col++) {
                int index = row * cols + col; // 计算当前图像的索引
                if (index < images.length) { // 如果索引在图像数组范围内
                    g.drawImage(images[index], col * width, row * height, width, height, this); // 绘制图像
                }
            }
        }
    }

    // 主方法,程序入口
    public static void main(String[] args) {
        // 使用SwingUtilities.invokeLater方法在事件调度线程中创建和显示GUI
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("七夕魔方照片墙"); // 创建一个JFrame窗口
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作
            frame.setSize(800, 800); // 设置窗口大小

            // 定义图像路径数组
            String[] imagePaths = {
                "path/to/photo1.jpg",
                "path/to/photo2.jpg",
                "path/to/photo3.jpg",
                "path/to/photo4.jpg",
                "path/to/photo5.jpg",
                "path/to/photo6.jpg"
            };

            try {
                // 创建PhotoWall对象
                PhotoWall photoWall = new PhotoWall(imagePaths, 3, 2); // 3行2列
                frame.add(photoWall); // 将PhotoWall添加到JFrame中
                frame.setVisible(true); // 设置窗口可见
            } catch (IOException e) {
                e.printStackTrace(); // 打印异常信息
            }
        });
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
配图说明

请确保你替换代码中的"path/to/photoX.jpg"为实际图像文件的路径。

以下是一个示意图,用于展示程序运行后生成的照片墙布局。假设我们有6张图片,并且我们将它们布置成3行2列的网格:

+---------+---------+
| photo1  | photo2  |
+---------+---------+
| photo3  | photo4  |
+---------+---------+
| photo5  | photo6  |
+---------+---------+
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

请替换图片路径,并运行代码以查看实际效果。代码完成后会创建一个800x800像素的窗口,显示一个3行2列的照片墙。

java代码七夕魔方照片墙_照片墙