How to Display Images in Java Stand-alone Applications

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of