Here you can find the source of cropImage(Image img, int x, int y, int w, int h)
public static Image cropImage(Image img, int x, int y, int w, int h)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Shape; import java.awt.image.BufferedImage; public class Main { public static Image cropImage(Image image, Shape shape, int xOffset, int yOffset) { BufferedImage out = createImage(shape.getBounds()); Graphics g = out.getGraphics(); g.setClip(shape);/*from w w w . ja v a 2s . co m*/ g.drawImage(image, -xOffset, -yOffset, null); g.dispose(); return out; } public static Image cropImage(Image img, int x, int y, int w, int h) { BufferedImage out = createImage(w, h); Graphics g = out.getGraphics(); BufferedImage sub = ((BufferedImage) img).getSubimage(x, y, w, h); g.drawImage(sub, 0, 0, w, h, null); g.dispose(); return out; } public static BufferedImage createImage(Rectangle bounds) { return createImage(bounds, BufferedImage.TYPE_INT_ARGB); } public static BufferedImage createImage(Rectangle bounds, int type) { return createImage(bounds.width, bounds.height, type); } public static BufferedImage createImage(Image image) { return createImage(image, BufferedImage.TYPE_INT_ARGB); } public static BufferedImage createImage(Image image, int type) { return createImage(image.getWidth(null), image.getHeight(null), type); } public static BufferedImage createImage(int width, int height) { return createImage(width, height, BufferedImage.TYPE_INT_ARGB); } public static BufferedImage createImage(int width, int height, int type) { return new BufferedImage(width, height, type); } }