0

I'm having trouble displaying an image in an applet in eclipse. I'm wondering if there is any alternate way of doing this. I also want to know if there is a reliable online applet tester that displays an applet I made on the web. I'm following the oracle tutorials but they don't work. Here is my code:

Displaying class:

import java.applet.Applet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;


public class usb extends Applet{
static BufferedImage background;
/**
 * 
 */
private static final long serialVersionUID = 1L;
@Override
public void init() {
    // TODO Auto-generated method stub
    try {
        URL url = new URL(getCodeBase(), "resources/usb_homescreen.png");
        background = ImageIO.read(url);
    } catch (IOException e) {
    }
    setSize(1000,500);
    add(new goat());
}

}

Canvas class:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;


public class goat extends Canvas {

/**
 * 
 */
private static final long serialVersionUID = 1L;
goat() {
    setSize(1000,500);
    setBackground(Color.white);
}

@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);
    g.drawImage(usb.background, 0, 0, null);
}
}

Any ideas about what's wrong?

user2884344
  • 195
  • 1
  • 1
  • 9
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 19 '13 at 02:43
  • Is there a swing applet? – user2884344 Nov 19 '13 at 02:55
  • [`javax.swing.JApplet`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JApplet.html) as seen since the introduction of Swing (1.2). See also the example on the [applet tag info. page](http://stackoverflow.com/tags/applet/info) here at SO. It uses `JApplet`. – Andrew Thompson Nov 19 '13 at 06:21

1 Answers1

0

Applets are executed in a special environment. When using them as plain objects, this environment is missing, i.e. the init method will never get called and the method getCodeBase() will not work. If you want to test an Applet there’s the program appletviewer.exe shipped with the JDK. It requires a HTML page containing an <applet> or <object> tag.

But there is also a way to instantiate an Applet within a stand-alone java application which will generate the required context. Given an Applet class “MyApplet” the necessary code looks like:

MyApplet applet=(MyApplet)java.beans.Beans.instantiate(
  MyApplet.class.getClassLoader(), MyApplet.class.getName());
System.out.println(applet.getCodeBase());//prove that the context is now there
// now you can use applet like a normal AWT/Swing component
Holger
  • 285,553
  • 42
  • 434
  • 765