Here you can find the source of drawImageWithClipping(Graphics g, BufferedImage img)
public static void drawImageWithClipping(Graphics g, BufferedImage img)
//package com.java2s; /*/* w ww .j a v a2s .c o m*/ * Copyright 2015 Laszlo Balazs-Csiki * * This file is part of Pixelitor. Pixelitor is free software: you * can redistribute it and/or modify it under the terms of the GNU * General Public License, version 3 as published by the Free * Software Foundation. * * Pixelitor is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pixelitor. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Graphics; import java.awt.Rectangle; import java.awt.image.BufferedImage; public class Main { public static void drawImageWithClipping(Graphics g, BufferedImage img) { assert img != null; Rectangle clipBounds = g.getClipBounds(); int clipX = (int) clipBounds.getX(); int clipY = (int) clipBounds.getY(); int clipWidth = (int) clipBounds.getWidth(); int clipHeight = (int) clipBounds.getHeight(); int clipX2 = clipX + clipWidth; int clipY2 = clipY + clipHeight; g.drawImage(img, clipX, clipY, clipX2, clipY2, clipX, clipY, clipX2, clipY2, null); } }