Matthew Flynn
Matthew Flynn

Reputation: 191

Display image as background processing

I am trying to use an image as the background in java processing, but this code gives me the error: "The file "cave_entrance.jpg" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable."

This is the code I have:

public class Main extends PApplet{

    public void setup(){
        size(900,700);
        background(0);
    }// setup

    public void draw(){
        PImage background = loadImage("cave_entrance.jpg");
        background(background);
    }// draw

}// Main

I have the file cave_entrance.jpg in

Project
    data
        cave_entrance.jpg

Upvotes: 0

Views: 1038

Answers (2)

Kevin Workman
Kevin Workman

Reputation: 42176

From the Processing documentation for the dataPath() function:

This function almost certainly does not do the thing you want it to. The data path is handled differently on each platform, and should not be considered a location to write files. It should also not be assumed that this location can be read from or listed. This function is used internally as a possible location for reading files. It's still "public" as a holdover from earlier code.

Libraries should use createInput() to get an InputStream or createOutput() to get an OutputStream. sketchPath() can be used to get a location relative to the sketch. Again, do not use this to get relative locations of files. You'll be disappointed when your app runs on different platforms.

It also sounds strange that a path includes the bin directory. I would expect any path in your sketch to be relative to the bin directory, so you shouldn't need to use an absolute path to get into that directory.

Since you aren't using the Processing editor, you have to add your data directory to your source path. If you're in eclipse, you can do this just by copying your data directory into the src directory. This is a much better solution than manually copying files into your bin directory, since eclipse can (and will) delete those files without warning.

Upvotes: 1

Grzegorz Górkiewicz
Grzegorz Górkiewicz

Reputation: 4586

You can check where exatcly to place this file by calling:

println(dataPath(""));

Upvotes: 0

Related Questions