I'm having trouble with font loading. Fonts are specified in a separate file
(I simplified the program since getting the list of fonts works fine, please
excuse the poor string manipulation especially) and I'm attempting to load
them as follows:
1. Load from the data directory
2. Load from the system fonts
3. Use the first system fonts
But java seems to freeze at the end of setup when it gets the
NullPointerException since "Futura Book" is not in the data directory, however
that should have been caught by the try/catch block.
I'm using Processing 1.0.1 on a Windows XP machine.
Quote:************************* Console output ***************************************
# of sysFonts 445
Attempting to load 'Futura Book' from data directory
Font 'Futura Book' not loaded from data directory
java.lang.NullPointerException
at java.io.DataInputStream.readInt(DataInputStream.java:370)
at processing.core.PFont.<init>(PFont.java:128)
at processing.core.PApplet.loadFont(PApplet.java:3500)
at net.jpcutler.markertags.FontFailureProblem.setup(FontFailureProblem.java:30)
at processing.core.PApplet.handleDraw(PApplet.java:1383)
at processing.core.PApplet.run(PApplet.java:1311)
at java.lang.Thread.run(Thread.java:619)
Attempting to load 'LucidaSans-16.vlw' from data directory
Code:import java.util.HashMap;
import processing.core.PApplet;
import processing.core.PFont;
public class FontFailureProblem extends PApplet{
HashMap pfonts;
String[] fontNames = {"Futura Book","LucidaSans-16.vlw"};
String[] sysFonts;
int j = 0;
public void setup() {
size(100,100);
strokeWeight(2);
sysFonts = PFont.list();
pfonts = new HashMap();
println("# of sysFonts " + sysFonts.length);
// load the required fonts.
for (int i=0; i<fontNames.length; i++) {
PFont tmpPFont = null;
boolean fontLoaded = false;
// Also load the required pfonts
try {
println("Attempting to load '" + fontNames[i] + "' from data directory");
// Try to get the font from the data directory
tmpPFont = loadFont(fontNames[i]);
fontLoaded = true;
} catch (Exception e) {
println("Font '" + fontNames[i] + "' not loaded from data directory");
}
if (!fontLoaded) {
// Try to get the font from the system fonts
try {
tmpPFont = createFont(fontNames[i],12);
fontLoaded = true;
} catch (Exception e) {
println("Font not found in system fonts");
}
}
if (!fontLoaded) {
println("Unable to load font '"+fontNames[i]+"' from the system or the bin/data directory using '"+sysFonts[0]+"'");
tmpPFont = createFont(sysFonts[1],12);
}
pfonts.put(fontNames[i], tmpPFont);
}
println("end of setup");
}
public void draw() {
println("begin draw");
background(255);
stroke(0);
line(0,j,this.width,j);
if (j > this.height) { j=0; } else {j++;}
}
}