Home | | Internet Programming | Java - Image handling

Chapter: Web or internet Programming : Java Programming

Java - Image handling

The basic manner of displaying an Image in an applet/application is to call the drawImage() method of the Graphics class in the paint() method of the component to display the image.

Image handling

 

       The basic manner of displaying an Image in an applet/application is to call the drawImage() method of the Graphics class in the paint() method of the component to display the image.

 

       There are six varieties of the method that deal with scaling and background colors for transparent images. The basic manner of calling is as follows:

g.drawImage(image, xPosition, yPosition, this);

 

       The last argument is what's called an ImageObserver and helps deal with the asynchronous loading of image data. Generally speaking, you just pass a reference to the applet as the image observer so that as more data is loaded the applet (the observer) is notified and redraws itself.

 

       ‖ To make matters simpler for an applet, allowing you to move an applet between hosts without having to hardcode where the images come from, you can use a second variety of the method:

Image image = getImage(baseURL, file);

 

       Provide the base URL and filename separately. The two are combined to form the specific URL to get the image file from. The getDocumentBase() method allows you to start with a base URL of where the HTML file is located. And, the getCodeBase() method allows you to start with a base URL of where the .class file is located. Using one of the two you can find an image in the same directory as either with something like this:

Image image = getImage(getDocumentBase(), "image.jpg");

 

public Image getImage(URL u)

       This method retrieves the image data at the specified URL and puts it in a

java.awt.Image object.

       For example:

 

Image myLogo = getImage(new URL("http://metalab.unc.edu/java/cup.gif"));

 

The getImage( ) method relies on the AppletContext (provided by the web browser or applet viewer) to retrieve and interpret the image. Thus this method can get images only in formats understood by the AppletContext in which the applet is running. Currently, all contexts that run in a graphical environment understand the GIF format.

 

       Most contexts also understand JPEG though JPEG support was omitted from some vendors' early alpha and beta releases. The applet that loads and displays the image referenced by the IMAGE parameter, which comes from a <PARAM> tag in the HTML file.

 

Example:Display an Image via a

URL import java.applet.*;

 

import java.awt.*;

import java.net.*;

 

public class ImageView extends Applet {

Image picture;

 

public void init( ) { try {

 

URL u = new URL(this.getCodeBase( ), this.getParameter("IMAGE")); this.picture = this.getImage(u);

System.out.println(u);

}

 

catch (MalformedURLException e) {

// shouldn't happen, the codebase is never malformed

 

}

}

 

public void paint (Graphics g) { g.drawImage(this.picture, 0, 0, this);

}

 

}

 

public Image getImage(URL path, String filename)

 

 

       This is similar to the previous method, except that it uses the path argument (a URL) to find the image's directory and the filename argument (a String) to get the name of the image file. For example:

 

Image logo = this.getImage(new URL("http://metalab.unc.edu/java/"),"cup.gif");

       This version of getImage( ) is frequently used with getCodeBase( ) or

 

getDocumentBase( ). You would use getCodeBase ( ) in an applet that might be used on many different web servers but whose images would always be in the same directory as the applet.

       For example:

Image logo = this.getImage(this.getCodeBase( ), "logo.gif"));

 

If the applet exists on only one web server but is embedded on many different pages, each of which loads different images, you would use getDocumentBase( ) to locate the images:

Image logo = this.getImage( this.getDocumentBase( ), "logo.gif"));

 

       This technique would be useful in an animator applet; the applet would probably read the names of some image files from parameters included in the HTML. You can use the filename argument to add to the path you get from the URL component. For example, if the pictures are in a directory called images, which is in the same directory as the HTML page, you would load the file logo.gif like this:

 

Image logo = this.getImage(this.getDocumentBase( ), "images/logo.gif"));

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Java Programming : Java - Image handling |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.