Loading font from data folder in a second window

The following code create two windows when my sketch is run:

Main file:

PWindow pwindow;
PFont font;

void setup() {
  size(800, 800);
  pwindow = new PWindow();
}

Second file:

class PWindow extends PApplet {
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

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

  void setup() {
    font = loadFont("AppleSymbols-24.vlw");
    textFont(font, 24);
    text("test", 100, 100);
  }
}

Everything works, however, processing can’t seem to locate the font file in the data folder when I use loadFont in my second file (RuntimeException: Could not load font AppleSymbols-24.vlw. Make sure that the font has been copied to the data folder of your sketch.). But when I put loadFont in my main file’s setup, it is able to load with no error. How can I get processing to locate the font in the data folder when I attempt to load it from a file that’s not my main file?

2 Likes

Hello,

This worked:

PWindow pwindow;
PFont font;

String s1;

void setup() {
  size(800, 800);
  pwindow = new PWindow();
  println("W1: ", sketchPath());
  s1 = sketchPath();
  println("S1: ", s1);
}

class PWindow extends PApplet {
  PWindow() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

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

  void setup() {
    println();
    println("W2: ", sketchPath());
    println("S1: ", s1);
    font = loadFont(s1 + "/data/Bauhaus93-48.vlw");
    textFont(font, 48);
    text(":)", 100, 100);
  }
}

I used println() statements to show the sketchPath() as seen by each window to better understand what was happening:

:)

2 Likes
3 Likes

Thank you! That makes sense

1 Like