split Image - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

split Image

Demo Code


//package com.java2s;

import java.awt.image.*;
import java.awt.*;

public class Main {
    public static BufferedImage[] splitImage(BufferedImage img, int cols,
            int rows) {
        int w = img.getWidth() / cols;
        int h = img.getHeight() / rows;
        int num = 0;
        BufferedImage imgs[] = new BufferedImage[w * h];
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                imgs[num] = new BufferedImage(w, h, img.getColorModel()
                        .getTransparency());
                // Tell the graphics to draw only one block of the image   
                Graphics2D g = imgs[num].createGraphics();
                g.drawImage(img, 0, 0, w, h, w * x, h * y, w * x + w, h * y
                        + h, null);//from  w ww. j  a  v a  2 s  .  c o m
                g.dispose();
                num++;
            }
        }
        return imgs;
    }
}

Related Tutorials