0

I'm trying to use an image that I created in photoshop in a applet. Every time I try to load the image it doesn't show up, but if I use an image from the internet it works fine. I've tried using both png and jpg neither work.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;


public class Test extends Applet{

    final int WIDTH = 1000, HEIGHT = 400, MIN = 1;
    private Image img;

    public void init() {
        img = getImage(getDocumentBase(), "image.jpg");

        setSize(WIDTH, HEIGHT);

    }

    public void paint(Graphics g) {
        g.drawImage(img, 20, 20, null);
    }

}
Danh
  • 5,916
  • 7
  • 30
  • 45
  • and what does it say? did you try to check for null pointers in img==null or other?? – gpasch Dec 15 '16 at 20:01
  • 1) Always copy/paste error and exception output! 2) Why code an applet? If it is due to the teacher specifying it, 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/). 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Dec 16 '16 at 22:25
  • .. 4) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 5) `g.drawImage(img, 20, 20, null);` should be `g.drawImage(img, 20, 20, this);` (though in Swing, we'd typically just use a `JLabel` to display an image). 6) When overriding a paint method, call the super method first. 7) **What [encoding](https://en.wikipedia.org/wiki/JPEG#JPEG_codec_example) is used for the image?** 8) `setSize(WIDTH, HEIGHT);` an applet's size is set in HTML. Don't change! – Andrew Thompson Dec 16 '16 at 22:30

0 Answers0