Here you can find the source of crop(BufferedImage source, int startX, int startY, int endX, int endY)
public static BufferedImage crop(BufferedImage source, int startX, int startY, int endX, int endY)
//package com.java2s; import java.awt.image.BufferedImage; public class Main { public static BufferedImage crop(BufferedImage source, int startX, int startY, int endX, int endY) { int width = source.getWidth(); int height = source.getHeight(); if (startX <= -1) { startX = 0;/* w w w .ja v a2 s . c o m*/ } if (startY <= -1) { startY = 0; } if (endX <= -1) { endX = width - 1; } if (endY <= -1) { endY = height - 1; } BufferedImage result = new BufferedImage(endX, endY, source.getType()); for (int y = startY; y < endY + startY; y++) { for (int x = startX; x < endX + startX; x++) { int rgb = source.getRGB(x, y); result.setRGB(x - startX, y - startY, rgb); } } return result; } }