How to Display Images in Java Stand-alone Applications

How do I refer to/utilize the getImage(…) method whenusing it in an application, as I can only get it to work for an applet?

To display images in a stand-alone Java application, you need to usethe Toolkit class in the AWT package. Among the methods of theToolkit class you’ll find a couple of different flavors ofgetImage() 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 canthen be drawn using drawImage.

You can’t instantiate a Toolkit object directly, however, becauseit is an abstract class and as such, its methods are implementedonly in its subclasses (the AWT implementation underneath your GUI).To get hold of a Toolkit object, you use the getToolkit() methodof 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 animage 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*;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();     ;       }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS