how to scaling images in java appletyou can scale


How to Scaling Images in java applet?

You can scale an image within a particular rectangle using this version of the drawImage() method:

public boolean drawImage(Image img, int x, int y, int width,
int height, ImageObserver io)

width and height specify the size of the rectangle to scale the image into. All other arguments are the similar as before. If the scale is not in proportion to the size of the image, it can end up looking quite squashed.

To prevent disproportionate scaling use the image's getHeight() and getWidth() methods to denotes the actual size. Then scale appropriately. For examples this is how you would draw an Image scaled through one quarter in each dimension:

g.drawImage(img, 0, 0, img.getWidth(this)/4, img.getHeight(this)/4, this);

This program reads a GIF file in the same directory as the HTML file and displays it at a particular magnification. The name of the GIF file and the magnification factor are specified through PARAMs.
import java.awt.*;
import java.applet.*;

public class MagnifyImage extends Applet {

private Image image;
private int scaleFactor;

public void init() {
String filename = this.getParameter("imagefile");
this.image = this.getImage(this.getDocumentBase(), filename);
this.scaleFactor = Integer.parseInt(this.getParameter("scalefactor"));
}

public void paint (Graphics g) {
int width = this.image.getWidth(this);
int height = this.image.getHeight(this);
scaledWidth = width * this.scaleFactor;
scaledHeight = height * this.scaleFactor;
g.drawImage(this.image, 0, 0, scaledWidth, scaledHeight, this);
}

}
This applet is straightforward. The init() method reads two PARAMs, one the name of the image file, the other the magnification factor. The paint() method computes the scale and then draws the image. 

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: how to scaling images in java appletyou can scale
Reference No:- TGS0285261

Expected delivery within 24 Hours