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

Homography matrix, Open A java applet should appear on your screen. C...

Open A java applet should appear on your screen. Click on File, OpenImage and select Asterix. Check Homography box. By clicking on the images you can select points. The c

What is jsp page, A JSP page is a text-based document that having two types...

A JSP page is a text-based document that having two types of text: static template data, which can be expressed in any text-based format like HTML, SVG, WML, and XML, and JSP eleme

Threads, programming challenges

programming challenges

What is an immutable object, What is an immutable object? An immutable ...

What is an immutable object? An immutable object is one that we can't change once it's created. Steps involved in creation of an immutable object are: Make all of its data f

Explain any five buzz words in java, Question 1 Explain any five buzz word...

Question 1 Explain any five buzz words in Java Question 2 Explain exception classes and also explain common exceptions in java Question 3 Explain primitive and abstract data t

What do you understand by the term constructor in java, Question: (a) ...

Question: (a) What are the main differences between an object and a class? Illustrate with appropriate examples. (b) Differentiate between a global variable and a local va

Define the difference between stringbuffer and string class, Define the dif...

Define the difference between StringBuffer and String class ?

Query example in sparql - java and jena api, Exercise 1) Lets start by quer...

Exercise 1) Lets start by query example in SPARQL: QUERY: What are the school's names that belong to the "local authority district" 00AA? NOTE 1: "Authority districts" divide UK i

Use javascript create some basic client-side validation, The last exercise ...

The last exercise of each assignment will be continuous from assignment to assignment and will have the goal of building an e-store website by the end of the course. Date & Tim

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