CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Applets

Applets
Locating Resource
Displaying image

<applet code="DisplayImageWithURL.class" width=350 height=200></applet>
import javax.swing.*;

public class DisplayImageWithURL extends JApplet {
  public DisplayImageWithURL() {
    java.net.URL url = this.getClass().getResource("../image/butterfly.gif");
    add(new JLabel(new ImageIcon(url)));
  }

  /** Main method */
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("DisplayImageWithURL");

    // Create an instance of the applet
    DisplayImageWithURL applet = new DisplayImageWithURL();
    applet.init();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, java.awt.BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Display the frame
    frame.setSize(200, 500);
    frame.setVisible(true);
  }
}
Previous | Locating Resource from Applets | Creating ImageIcon Using Absolute File Names | Creating ImageIcon Using Relative File Names | Using the URL Class | Creating a URL from a Class Reference | Displaying image