2

I have tried numerous solutions, and I can't seem to find the issue with my code. I have checked the path names, the permissions for the files, and tried several different file names for the image. The image isn't showing up.

private Character player;
private Image image, sprite;
private URL base;
private Graphics second;

@Override
public void init() {
    setSize(500,500);
    setBackground(Color.white);
    setFocusable(true);
    addKeyListener(this);
    Frame frame = (Frame)this.getParent().getParent();
    frame.setTitle("Joules");
    try {
        base = getDocumentBase();
        sprite = getImage(base,"kips.PNG");
    } catch (Exception e) {
    }

   //       sprite = getImage(base,"kips.PNG"); 
}

@Override
public void start() {
    player = new Character();

    Thread thread = new Thread(this);

    thread.start();
}

    @Override
public void update(Graphics g) {
    if (image == null) {
        image = createImage(this.getWidth(), this.getHeight());
        second = image.getGraphics();
    }

    second.setColor(getBackground());
    second.fillRect(0, 0, getWidth(), getHeight());
    second.setColor(getForeground());
    paint(second);

    g.drawImage(image, 0, 0, this);
}

@Override
public void paint(Graphics g) {
        g.drawImage(sprite,player.getCenterX(),player.getCenterY(),this);
}

@Override
public void run() {
    while (true) {
        repaint();
        try {
            Thread.sleep(34);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Please help...

user1610810
  • 99
  • 1
  • 5
  • Where is this `getImage` method? (What's it's declaring class?) – acdcjunior Apr 13 '13 at 07:18
  • 1
    @acdcjunior See [`Applet.getImage(URL)`](http://docs.oracle.com/javase/7/docs/api/java/applet/Applet.html#getImage%28java.net.URL%29) & the overloaded variant immediately below that. – Andrew Thompson Apr 13 '13 at 09:05
  • I've also tried to access the file by setting a url to the image's name in the computer. I did get a malformed url exception. I'm still suspecting that the path is somehow unreachable, but I am lost. – user1610810 Apr 13 '13 at 16:25
  • 1) Have you seen any Java load the image? 2) With the current code, the image needs to be in the same directory as the HTML. – Andrew Thompson Apr 13 '13 at 22:44
  • I only get the background color when I try to run the program. getDocumentBase() should have gotten me into the right directory, but obviously that's not working. Even when I try the full path name "C:/users/../kips.png" it won't show. I'm not hosting the file anywhere, so unless I've misunderstood you, I don't think HTML has any relevance. Thanks for your response. – user1610810 Apr 14 '13 at 02:05

1 Answers1

0

I just had this issue and found that getDocumentBase() is returning a huge path which is the base of the current applet, not the project.

Instead, I used getCodeBase() which returns "../projectName/bin/", so navigate back one folder in your relative paths in the code.

base = getCodeBase();
sprite = getImage(base,"../kips.PNG");

njt
  • 511
  • 4
  • 12