by Dinesh Thakur Category: AWT and Applets

There are four different overloaded drawImage methods in Graphics.

First, we'll show you the least complicated call:

if (image != null) {
   g.drawImage (image,xloc,yloc, this) ;

This causes the image to be drawn lifesize (no scaling is applied), with the upper left corner of the image positioned at (xloc,yloc) in the current graphics context clipping rectangle. If any errors or events occur while the image is being rendered, they are ignored (potentially leaving the image in some strange display state). If the image contains transparent pixels, the destination pixels are left as they were before the drawImage call was made.

Next we'll show you the image-loading call we used in the How to loading images in java

if (image != null) {
   g.drawImage (image,xloc,yloc, getBackground() ,this);

This call draws the image scaled normally (100%) with the upper left corner of the image positioned at (xloc,yloc). If the image has transparent pixels, those pixels in the destination graphics context are drawn in the color passed to drawImage. Our example passes the background color of the container we're drawing in. This is different than the previous call, where the destination pixels were left alone.

Both of the methods we just discussed draw the image at a one-to-one ratio. Each pixel in the source image is drawn directly into the destination graphics context. While this works for the vast majority of cases, there are situations where the source image needs to be scaled (up or down) to fit into a particular region of the destination graphics context. Two variations of drawImage provide width and height parameters to be used in scaling the destination image. Regardless of the source image size, the destination image is rendered to the size specified in the width and height. This example leaves the transparent pixels as they were before the call to drawImage.

if (image != null) {
   g.drawImage(image,xloc,yloc,width,height,this) ;

The next example draws the transparent pixels in light gray. All pixels other than the transparent pixels are drawn in the source pixel color.

if (image != null) {
 g.drawImage{image,xloc,yloc, width,height,Color.lightGray,this);

If something interesting happens while an image is being drawn, the AWTimage-drawing code notifies this. imageUpdate. For most of your code, you can just pass this as the image observer. The default behavior implemented by Component is usually sufficient to get the image drawn. If your application is using incremental drawing, you may need to implement your own imageUpdate method. When the imageUpdate method is called, it receives a small block of status information:
• Image img-the image being referenced
• int infoflags-the logical OR of the following bits, each of which indicates that the related information is available
• int x-the X location of the image
• int y-the Y location of the image
• int width-if (infoflags&WIDTH), then this is the width of the image
• int height-if (infoflags&HEIGHT), then this is the height of the image

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles