double scalex = dwidth/swidth;
double scaley = dheight/sheight;
double transx = sx1*scalex;
double transy = sy1*scaley;
AffineTransform tx = AffineTransform.getTranslateInstance(dx1-transx,dy1-transy);
tx.scale(scalex,scaley);
BufferedImage mask = new BufferedImage(img.getWidth(observer), img.getHeight(observer), BufferedImage.TYPE_BYTE_BINARY);
Graphics g = mask.getGraphics();
g.fillRect(sx1,sy1, (int)swidth, (int)sheight);
drawImage(img, mask, tx, null, observer);