Using Images in an Applet - Java Applet

Java examples for Applet:Applet Creation

Description

Using Images in an Applet

Demo Code

import java.awt.Container;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;

public class Main extends JApplet {
  JLabel imageLabel;//from w  w w  .j av  a  2  s  .  com

  @Override
  public void init() {
    Container contentPane = this.getContentPane();
    Image img = this.getWelcomeImage();
    if (img == null) {
      imageLabel = new JLabel("Image parameter not set...");
    } else {
      imageLabel = new JLabel(new ImageIcon(img));
    }
    contentPane.add(imageLabel);
  }

  private Image getWelcomeImage() {
    Image img = null;
    String imageURL = this.getParameter("welcomeImageURL");
    if (imageURL != null) {
      img = this.getImage(this.getDocumentBase(), imageURL);
    }
    return img;
  }
}

Contents of the imageapplet.html File

<html>
        <head>
                <title>Using Images in Applet</title>
        </head>
        <body>
                <applet code="Main"
                        width="250" height="200">
                        <param name="welcomeImageURL"
                               value="images/welcome.jpg"/>
                        This browser does not support Applets.
                </applet>
        </body>
</html>

Related Tutorials