File not found using surface.setIcon(icon) in public class test extends PApplet

hello,everyone. I can’t find the file when using surface.setIcon(icon) in public class test extends PApplet. I am using the processing4 version. My pictures are saved in data. The file structure is as follows:
MySketch/
test.pde
data/
logo.png
I tried to use the following code to verify whether my image can be called.

PImage img;

public void setup() {
  size(400, 400);
  img = loadImage("logo.png");  // 假设 logo.png 在 data 文件夹中
  if (img != null) {
    image(img, 100, 100);
  } else {
    println("图片加载失败!");
  }
}

This was successful.
When I use the following code, the problem of The file “logo.png” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable appears.

// OpenBioView.java
import processing.core.PApplet;

public class test extends PApplet {
  PImage img;

  public void setup() {
    size(400, 400);
    img = loadImage("logo.png");  // 加载图片
    if (img != null) {
      image(img, 100, 100);  // 显示图片
    } else {
      println("图片加载失败!");
    }
  }

  public void draw() {
    // 绘制图像等内容
  }

  public static void main(String[] args) {
    PApplet.main("test");  
  }
}

Does anyone know why this is? I hope someone can help me, thank you very much! :face_with_head_bandage:

1 Like

Thanks for your reply! I did the following in setup, and I only used the size() method in setting. I also tried to set the Icon in setting, but unfortunately it failed.
This is the code in setup():

 PImage icon = loadImage("logo.png");
  surface.setIcon(icon);
public void settings() {size(2500,1200, JAVA2D);}

Your answer is the reason why I failed in the setting.

1 Like

Your question is marked ‘homework’ so according to forum rules we cannot give you a “full code” solution.

The following is the approach that I would use:

  1. Delete “public”; not necessary.
  2. Change name of test class to class Test extends PApplet.
  3. Inside of class Test create a Test() function.
  4. Inside of Test() add the following:
  PApplet.runSketch(new String[]{this.getClass().getName()}, this);
  surface.setTitle("PApplet");
  String imgPath = "Users/yourName/Documents/Processing/sketchFolderName/data/logo.png";
  img = loadImage(imgPath);  // 加载图片
  if (img == null) {
    println("图片加载失败!");
  }
  1. Add a settings() function to your Test class:
 void settings() {
    size(400, 400, P2D);
  }
  1. Inside of draw() call the image: image(img,0,0)
  2. Delete the ‘main’ function and replace it with a separate setup() function outside of the Test class.
  3. Inside of the separate setup() call the new Test() like this: Test test = new Test();
  4. Inside of the separate setup() use surface.setVisible(false); if you don’t want to see the default Processing window.

I know it’s a lot, but I hope it helps you.

Addendum:
The question title suggests that you are interested in surface.setIcon(). If that is the case, and you are using the Windows operating system the following reference may be helpful:

1 Like

ok, I will try it! thanks for your reply! :smiley:

Hello @cola ,

This example shows the Java code that the Processing IDE generates:

//This works:

//PImage img;

//public void setup() {
//  size(400, 400);
//  img = loadImage("logo.png");  // 假设 logo.png 在 data 文件夹中
//  if (img != null) {
//    image(img, 100, 100);
//  } else {
//    println("图片加载失败!");
//  }
//}


//This is what Processing generates (for above) in the %temp%/processing folder:

/* autogenerated by Processing revision 1295 on 2025-01-19 */
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class test extends PApplet {

  PImage img;

  public void setup() {
    /* size commented out by preprocessor */    ;
    
    //img = loadImage("logo.png");  // \u5047\u8bbe logo.png \u5728 data \u6587\u4ef6\u5939\u4e2d
    
    // The only thing that needed changing was point to an absolute location:
    img = loadImage("d:/users/glv/Documents/P4/test/data/logo.png");
    
    if (img != null) {
      image(img, 100, 100);
    } else {
      println("\u56fe\u7247\u52a0\u8f7d\u5931\u8d25\uff01");
    }
  }

  public void settings() {
    size(400, 400);
  }

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "test" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

I can run the code above in the Processing IDE as long as the sketch name is test which is referenced in the code.

Note in code provided:

    // The only thing that needed changing was point to an absolute location:
    img = loadImage("d:/users/glv/Documents/P4/test/data/logo.png");

What IDE are you using for your OpenBioView.java ?

I also tried sketchPath() and dataPath():

Note:
They work if used in the Processing code (top of example) but not in your code example (bottom of example).
Try adding it to see the different paths!

:)

Your sketch can be made to work if you save it with the same name as your class, ie ‘test’ and use the full url for logo.png, ie something like this ‘/Users/yourName/Documents/Processing/test/logo.png’ You also need to add a settings() function to get the correct window size:

  public void settings(){
    size(400,400);
  }