Here you can find the source of recombine(BufferedImage[][] blocks)
Parameter | Description |
---|---|
blocks | - the blocks to be combined. |
public static BufferedImage recombine(BufferedImage[][] blocks)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics; import java.awt.image.BufferedImage; public class Main { /**//from ww w .j a v a2s . c o m * Handles recombining a two-dimensional array of image blocks back into a * single image. * * @param blocks - the blocks to be combined. * @return a new image with all blocks combined. */ public static BufferedImage recombine(BufferedImage[][] blocks) { int type = 0; int totalWidth = 0; int totalHeight = 0; int blockWidth = 0; int blockHeight = 0; // Scan over all blocks to get the total width and height of the image. for (int row = 0; row < blocks.length; row++) { for (int col = 0; col < blocks[row].length; col++) { BufferedImage block = blocks[row][col]; // Get block size and image type information for the first block // only. if (row == 0 && col == 0) { type = BufferedImage.TYPE_INT_ARGB; // block.getType(); blockWidth = block.getWidth(); blockHeight = block.getHeight(); } // Sum all of the block widths for the first row. if (row == 0) { totalWidth += block.getWidth(); } // Sum all of the block height for the first column. if (col == 0) { totalHeight += block.getHeight(); } } } // Create a new image with the size of the calculated blocks. BufferedImage img = new BufferedImage(totalWidth, totalHeight, type); // Get the graphics from the new image so that it can be drawn on. Graphics g = img.getGraphics(); // Loop over all the image blocks and write them to the new output // image. for (int row = 0; row < blocks.length; row++) { for (int col = 0; col < blocks[row].length; col++) { BufferedImage block = blocks[row][col]; int x = col * blockWidth; int y = row * blockHeight; int w = block.getWidth(); int h = block.getHeight(); g.drawImage(block, x, y, w, h, null); } } // Release the graphics context. g.dispose(); return img; } }