javatomcatappletappletviewer

Applet Does Not Display Image From URL in Tomcat


An applet I made loads images from a URL. When I run this applet inside Eclipse using AppletViewer it displays the images. However when I run the applet in a browser after deploying it in Tomcat 7, the image does not display, only the grey background of the applet is displayed and no errors are printed to console.

I have also tried packing the image in the jar and loading it from there, but I get the same problem (works in Eclipse but not in Tomcat).

Is Tomcat the problem or is it something else?

Here is the complete code:

import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LoadImage extends JApplet 
{   
Image image;

public LoadImage()
{
    try {
        URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Morus_bassanus_adu.jpg/50px-Morus_bassanus_adu.jpg");
        image = ImageIO.read(url);

        setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        add(label);
        setVisible(true);
    }
    catch (Exception e) {
        System.out.println(e.toString());
    }
}
}

Solution

  • Try

     URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Morus_bassanus_adu.jpg/50px-Morus_bassanus_adu.jpg");
    
                    BufferedImage image = ImageIO.read(url);        
    
                    JLabel label = new JLabel(new ImageIcon(image));
                    label.setMinimumSize(new Dimension(200,200));
                    this.add(label);