Need help with drawImage()

I simply don’t understand this method, can anyone explain to me how this works or any links to tutorials on this?

I know (dx1,dy1,dx2,dy2) is the rectangle to draw (right?) … but the (sx1,sy1,sx2,sy2) has got me totally confused.

I read this example (http://www.javaworld.com/javaworld/javatips/jw-javatip32.html) over and over again, but just couldn’t understand how he can draw that image (first image).

public abstract boolean drawImage(Image img,
                                  int dx1,
                                  int dy1,
                                  int dx2,
                                  int dy2,
                                  int sx1,
                                  int sy1,
                                  int sx2,
                                  int sy2,
                                  ImageObserver observer)

This drawImage method take an image (source, sx,sy) and draws that same image to the graphics context used in g.
It draws it there with a new set of coordinates (destination dx, dy) and these coordinates could be set up to stretch, shrink or flip the image.

Also the source coords could specify a smaller area in the source image to draw, so you end up with a cropped image.

The first image is an example of image cropping, he took a small chunk of the original image by using sx,sy that defined a smaller area than the entire image.

It’s funny. I took that JavaWorld logo and performed the same operation for image#1, and I ended up with a totally different image. Appearently that example is plain wrong, and that’s what got me confused, since I assumed it was correct! :\

Yeah, when I was learning Java2D I actually used the same example and also got confused. Basically the first values tell how much of the actual image you will draw, then the last values specify where it is drawn, as well as width, height, flipping, etc.

The different drawImages you can do:

drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
Draws the image at (x,y), but transparent piexels are filled with bgcolor.
drawImage(Image img, int x, int y, ImageObserver observer)
Simply draws the image at (x,y)
drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
Draws the image at (x,y) with resized to width and height dimensions and transparency filled in with bgcolor.
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
My favorite one: it draws the image at (x,y) and resizes it to width by height
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
The most powerful one. Does what I said below, but transparent pixels are filled with bgcolor
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
Will offset sx1,sy1 from the top left corner of the source and sx2-sx1 width by sy2-sy1 height and then draw that to (dx1,dy1) dx2-dx1 width by dy2-dy1 height.

Hopefully that makes sense.