devxlogo

How to Display Images in Java Stand-alone Applications

How to Display Images in Java Stand-alone Applications

Question:
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?

Answer:
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 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();       }}

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist