How to scaling images in java applet, JAVA Programming

Assignment Help:

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. 


Related Discussions:- How to scaling images in java applet

Method overriding, how to write method overriding and example programe

how to write method overriding and example programe

Explain the term naming packages in details, Explain the term Naming Packag...

Explain the term Naming Packages in details? As you saw previously name space conflicts arise if two pieces of code declare the similar name. Java remains track of what belongs

Develop a shopping carts application , To develop a shopping carts applicat...

To develop a shopping carts application for an online store of your choice Outcomes: 1. Apply the GUI components of Java and other tools to create user-friendly interfaces.

what is meant by binding in rmi, Binding is a method of associating or re...

Binding is a method of associating or registering a name for a remote object that can be used at a later time to look up that remote object. A remote object can be linked with a na

How are this () and super() used with constructors, How are this () and sup...

How are this () and super() used with constructors? this() Constructors: is used to pointing current class instance. Can be used with methods or variables. Used to call

Method overriding, how to write java programe and example programe

how to write java programe and example programe

Want an experienced ios and android expert, Want an Experienced iOS and And...

Want an Experienced iOS and Android expert Project Description: I am seeking a super talented team or Individual for a Real Estate app. The project wants to get done in a ver

Nonrecursive implementations of the min() and floor() method, Write nonrecu...

Write nonrecursive implementations of the min() and floor() methods in BST.java. Make sure to use only one return in your method. Use an insertion sort (discussed in class) to s

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd