1

I am working by way through a tutorial: http://www.kilobolt.com/day-4-enter-the-robot.html and have been having a problem getting a simple image to display in the applet. I am using IntelliJ 13 Community Edition. The main for loading the images is here:

It does the image setup in the init method:

public void init() {

    setSize(800, 480);
    setBackground(Color.BLACK);
    setFocusable(true);
    addKeyListener(this);
    Frame frame = (Frame) this.getParent().getParent();
    frame.setTitle("Q-Bot Alpha");
    try {
        base = getDocumentBase();
    } catch (Exception e) {
        // TODO: handle exception
    }

    // Image Setups
    character = getImage(base, "data/character.png").toString());
}

where character is a sprite I obtained from the tutorial website. I saved it in a folder called data. The file structure can be seen here: enter image description here

When I run this I just see a black background and character.png is not displayed. However if I change the getImage line to:

character = getImage(base, new URL("http://www.kilobolt.com/uploads/1/2/5/7/12571940/character.png").toString());

and point at the URL directly it works. I suspect this must be a path issue but I have not been able to get it working.

Travis
  • 659
  • 13
  • 28
  • My guess is that the application isn't executed in `src` but in the directory above it, so you would have to point to `src/data/character.png` or move the data directory up a level. – Philipp Sep 16 '13 at 09:24
  • Thanks, but sadly it did not work either. Is there any way to confirm what directory it is used? – Travis Sep 16 '13 at 09:40
  • This question explains how to get the current working directory: http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – Philipp Sep 16 '13 at 10:31
  • See http://stackoverflow.com/a/18601928/104891 for the sample project and the proper way to load resources from the classpath instead of the working directory (that is not set to `src` in your case). – CrazyCoder Sep 17 '13 at 05:08

1 Answers1

0

I am working on the same program and had the same problem. When run, the document base is actually in KiloboltGame/bin so you need to add your data/character.png here.

Tom
  • 1