Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Mar 20, 1997

How to Display Images in Java Stand-alone Applications

Question:
How do I refer to/utilize the getImage(...) method when using it in an application, as I can only get it to work for an applet?

Answer:
To display images in a stand-alone Java application, you need to use the Toolkit class in the AWT package. Among the methods of the Toolkit class you'll find a couple of different flavors of getImage() that provide the same functionality as the getImage() method in the Applet class:

       public abstract Image getImage(String  filename);
       public abstract Image getImage(URL  url);
They take a string or a URL and return an Image object which can then be drawn using drawImage.

You can't instantiate a Toolkit object directly, however, because it is an abstract class and as such, its methods are implemented only in its subclasses (the AWT implementation underneath your GUI). To get hold of a Toolkit object, you use the getToolkit() method of class Component.

This is best described with the help of an example. The following Java application is a very primitive image viewer. It takes a single command-line argument which is the name of an image file or URL to display and it will image it on the screen. The program is invoked as follows:

               java ImageViewer 

               or

               java ImageViewer 



import java.awt.*;
import java.net.*;

public class ImageViewer extends Canvas {
       Image image;
       int width, height;

       public ImageViewer(String urlname) {
               //
               // Canvas is a subclass of Component, so you can
               // use the getToolkit() method in class Component to
               // get a Toolkit object.  You then use the Toolkit to
               // create an Image from the URL name
               //
               Toolkit tk = getToolkit();
               try {
                       //
                       // If the urlname is a valid URL, call the
                       // URL version of getImage()
                       //
                       URL url = new URL(urlname);
                       image = tk.getImage(url);
               } catch (MalformedURLException e) {
                       //
                       // Otherwise use the filename version of getImage()
                       //
                       image = tk.getImage(urlname);
               }

               //
               // Force the image to be loaded, don't proceed until
               // the picture is all loaded and the size is known
               // prepareImage() returns true when the image has been
               // loaded.
               //
               while (!tk.prepareImage(image, -1, -1, this)) {
                       try {
                               Thread.sleep(500);
                               System.out.println("Loading...");
                       } catch (Exception e) {}
               }
               width = image.getWidth(this);
               height = image.getHeight(this);
       }

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

       //
       // The following two methods are needed so Java knows how
       // big the canvas ought to be.  Without them, the window
       // occupies the entire screen.
       //
       public Dimension minimumSize() {
               return new Dimension(width, height);
       }

       public Dimension preferredSize() {
               return minimumSize();
       }

       public static void main(String argv[]) {
               Frame f = new Frame();
               ImageViewer iv = new ImageViewer(argv[0]);
               f.setLayout(new BorderLayout());
               f.add("Center", iv);
               f.pack();
               f.show();
       }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date