Error: A null PFont was passed to textFont()
in
Core Library Questions
•
1 year ago
Hello members.
I'm new in Processing and with Tutorials, Onlinesketches etc. I've created a sketch which shows rss feed lines, if you clap your hands or do a loud sound. Since I've pasted the code of a drop down menu in it, I get the error: A null PFont was passed to textFont().
The code includes two Fonts "Arial-BoldMT-22.vlw" and "ArialMT-22.vlw" as well as two png-Pictures "logo" and "logoklein". You can replace them with whatever you want, just to see how my code works.
The Code is splitted in three menus.
Menu #1:
I'm new in Processing and with Tutorials, Onlinesketches etc. I've created a sketch which shows rss feed lines, if you clap your hands or do a loud sound. Since I've pasted the code of a drop down menu in it, I get the error: A null PFont was passed to textFont().
The code includes two Fonts "Arial-BoldMT-22.vlw" and "ArialMT-22.vlw" as well as two png-Pictures "logo" and "logoklein". You can replace them with whatever you want, just to see how my code works.
The Code is splitted in three menus.
Menu #1:
- /**
* Clap to load another RSS Feed, click on the news to move them,
* news fade out after the 20th.
*
* (c) 2012 M. Maier
*/
String s = "";
import ddf.minim.*;
Minim minim;
AudioInput input;
String[] fontList ;
String[] titles;
PFont myfont;
PVector [] positions;
PFont [] myFonts;
int [] myFontSize;
int Anzahl;
boolean fullList = false;
int[] BlockgroesseX;
int[] BlockgroesseY;
PFont fornt, fett;
String [] loadfeeds = {
"http://www.faz.net/aktuell/?rssview=1",
"http://rss.bild.de/bild.xml",
"http://rss.feedsportal.com/795/f/449002/index.rss",
"http://www.welt.de/?service=Rss"
};
int p; //descides a feed in Array "loadfeeds"
XMLElement[] titleXMLElements;
int CurrentTitle = 0;
float klatschen;
PImage Logoklein;
PImage Logo;
boolean startbildschirm;
void setup() {
startbildschirm = true;
size(screen.width, screen.height);
fontList = PFont.list();
minim = new Minim (this);
input = minim.getLineIn (Minim.STEREO, 512);
background(0);
//setzt den Hintergrund im Present-Mode auf Schwarz
frame.setBackground(new java.awt.Color(0, 0, 0));
Logoklein = loadImage("logoklein.png");
Logo = loadImage("logo.png");
setupMainMenu(); // erstelle Drop-Down Menü
fornt = loadFont("ArialMT-22.vlw");
fett = loadFont("Arial-BoldMT-22.vlw");
}
//-----------------------------------------------------------------------
// Main Menu
//-----------------------------------------------------------------------
void setupMainMenu() {
SMenu m=new SMenu(this); // create new main menu (at 0,0)
m.SMSubM("File"); // new submenu
m.SMSubM("Import");
m.SMItem("FAZ", new actn() {
public void a() {
p = 0;
}
}
);
m.SMItem("BILD", new actn() {
public void a() {
p = 1;
}
}
);
m.SMItem("Süddeutsche", new actn() {
public void a() {
p = 2;
}
}
);
m.SMItem("Weltonline", new actn() {
public void a() {
p = 3;
}
}
);
m.SMEnd();
m.SMItem("Close", new actn() {
public void a() {
exit();
}
}
); // overwrite act.a() with own proc
m.SMEnd();
m.SMSubM("Save");
m.SMItem("png", new actn() {
public void a() {
s="png clicked";
}
}
);
m.SMItem("tiff", new actn() {
public void a() {
s="tiff clicked";
}
}
);
m.SMItem("targa", new actn() {
public void a() {
s="targa clicked";
}
}
);
m.SMItem("jpeg", new actn() {
public void a() {
s="jpeg clicked";
}
}
);
m.SMEnd();
m.SMSubM("Help");
m.SMItem("About", new actn() {
public void a() {
About();
}
}
);
m.SMEnd(); // end all with SMEnd()
}
void About() {
fill(50, 200);
rect(0, 0, width, height);
fill(24);
rect(width/2-300, 160, 600, 340);
image(Logoklein, 620, 210, Logoklein.width, Logoklein.height);
strokeWeight(1);
stroke(80);
fill(255, 100);
textFont(fornt);
textSize(12);
text("M. Maier \n2.Semester \nInteraktive Medien \nProfessor Erich Schöls", width/2-260, 320);
fill(255);
text("Snapfeed visualisiert Nachrichten durch Interaktion mit dem Mikrofon.", width/2-260, 430);
ellipseMode(CENTER);
ellipse(width/2+300, 160, 30, 30);
textFont(fett);
if (mouseX > width/2+285 && mouseX < width/2+315 && mouseY > 145 && mouseY < 175) {
fill(120);
}
}
void draw() {
if (startbildschirm == true) {
smooth();
fill(0);
rect(0, 0, width, height);
smooth();
imageMode(CENTER);
image(Logo, width/2, height/2);
cursor(HAND);
}
else {
//-----------------------------------------------------------------------
// LOAD RSS FEED
//-----------------------------------------------------------------------
String url = loadfeeds[p];
println("Loading feed: "+url);
XMLElement rss = new XMLElement(this, url);
titleXMLElements = rss.getChildren("channel/item/title");
Anzahl = titleXMLElements.length;
// Arrays für jede Art von Werten
titles = new String[Anzahl];
positions =new PVector[Anzahl];
myFonts =new PFont[Anzahl];
myFontSize = new int[Anzahl];
BlockgroesseX = new int[Anzahl];
BlockgroesseY = new int[Anzahl];
for (int i = 0; i < titleXMLElements.length; i++) {
String title = titleXMLElements[i].getContent();
titles[i]=(new String(title));
}
// ********** MENÜ BEFEHLE ******************** //
stroke(g.fillColor);
if (s.length()>30) { // draw arrow
pushMatrix();
translate(20, 20);
rotate(PI/4);
line(15, 0, 135, 0);
triangle(0, 0, 15, -3, 15, 3);
popMatrix();
}
background(0);
cursor(ARROW);
smooth();
image(Logoklein, width-80, 60);
klatschen = input.mix.level() * width;
if (klatschen > 100) {
//immer wenn geklatscht wird fülle die werte abhängig vom Zähler CurrentTitle
positions [CurrentTitle] = (new PVector(random(width/2), random(70, height))); // Positionen
PFont font = createFont(fontList[(int)random(0, fontList.length)], 10); // Fonts
myFonts[CurrentTitle] = font;
myFontSize[CurrentTitle] = (int)random(14, 200); // Font Size
CurrentTitle += 1;
// wenn CurrentTitle die Maximalanzahl erreicht hat setze CurrentTitle auf 0
// ab jetzt werden schon vorhanden Werte in den Listen neu überschrieben
// alle anderen bleiben in der Liste bis sie überschrieben werden
if (CurrentTitle >= Anzahl) {
CurrentTitle = 0;
}
}
// solange die Liste noch nicht die maximalanzahl erreicht hat zähle hoch
if (CurrentTitle < Anzahl && fullList == false)
{
for (int j = 0; j < CurrentTitle; j++) {
int TheTextSize = myFontSize[j];
textFont(myFonts[j], TheTextSize);
String newString = titles[j];
/*
String[] m1 = match(titles[j], "Altmaier");
if (m1 != null) {
fill(farben[1]);
}
else {
fill(255, 85);
}
*/
PVector newPosition = positions[j];
text(newString, newPosition.x, newPosition.y);
// ermittle die Groesse der Textbloecke
BlockgroesseX [j] = (int)textWidth(newString);
BlockgroesseY [j] = TheTextSize;
// wenn die Maus über einem der Blöcke ist übergebe ihm den Mauswert als Koordinate
if (mousePressed) {
if (mouseX > (newPosition.x) && mouseX < (newPosition.x+BlockgroesseX[j]) && mouseY > (newPosition.y-BlockgroesseY[j]) && mouseY < (newPosition.y) )
{
println("über String Nummer " + j);
newPosition.x -= (pmouseX-mouseX);
newPosition.y -= (pmouseY-mouseY);
}
}
}
}
else // wenn sie die Maximalanzahl erreicht hat zeichne das vorhandene und ändere immer nur das was CurrentTitle entspricht (von 0 bis 20)
{
for (int j = 0; j < Anzahl-1; j++) {
int TheTextSize = myFontSize[j];
textFont(myFonts[j], TheTextSize);
String newString = titles[j];
PVector newPosition = positions[j];
text(newString, newPosition.x, newPosition.y);
// wenn die Maus über einem der Blöcke ist übergebe ihm den Mauswert als koordinate
if (mousePressed) {
if (mouseX > (newPosition.x) && mouseX < (newPosition.x+BlockgroesseX[j]/2) && mouseY > (newPosition.y-BlockgroesseY[j]) && mouseY < (newPosition.y) )
{
newPosition.x -= (pmouseX-mouseX);
newPosition.y -= (pmouseY-mouseY);
}
}
}
}
if (CurrentTitle == Anzahl-1)
{
fullList = true;
}
}
/* void keyPressed() {
if (key=='s') saveFrame("##.png"); //save .png bei keyPressed oder saveFrame("##.tif") oder saveFrame("##.tga")
}*/
}
void mousePressed() {
if (startbildschirm == true) {
startbildschirm = false;
fill(255, 255, 255, random(20, 255));
}
// deaktiviert ihn, wenn man mit Maus klickt
}
Menu #2 (has to be named SMenu.java):
- import java.util.ArrayList;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import processing.core.*;
public class SMenu extends ArrayList {
static PApplet app; static SMenu actm=null; static PFont font; static actn action=null;
String nam; actn act=null; SMenu m,par; Rectangle r,rm; int vis=-1;
public SMenu(PApplet ap) {this(ap,"Main",0,0);}
public SMenu(PApplet ap,int mx, int my) {this(ap,"Popup",mx,my);}
SMenu(PApplet ap,String s) {this(ap,s,0,0);}
SMenu(PApplet ap,String s,int mx,int my) {
par=actm; nam=s;
if (ap!=null) {
app=ap; actm=this; font=app.createFont("Arial",12);
app.registerDraw(this); app.registerMouseEvent(this);
if (nam.equals("Main")) app.mouseY=-1; //to avoid appearing main menu at startup
rm=new Rectangle(mx,my,0,0);
} else {
app.pushStyle(); app.textFont(font);
if (par.nam.equals("Main")) {
r=new Rectangle(par.rm.x+par.rm.width,par.rm.y,textW(s),textH(s));
rm=new Rectangle(r.x,par.rm.y+textH(s),0,0);
par.rm.width+=r.width; par.rm.height=textH(s);
} else {
r=new Rectangle(par.rm.x,par.rm.y+par.rm.height,textW(s),textH(s));
rm=new Rectangle(r.x+r.width,r.y,0,0);
par.rm.height+=textH(s); par.rm.width=app.max(par.rm.width,textW(s));
}
app.popStyle();
}
}
public void SMSubM(String s) {
actm.add(actm=new SMenu(null,s));
}
public void SMItem(String s,actn ac) {
actm.add(m=new SMenu(null,s)); m.act=ac;
}
public void SMSep() {
actm.add(new SMenu(null,null));
}
public void SMEnd() {
actm=actm.par;
if (actm==null) if (nam.equals("Main")) adjustH(); else adjustV();
}
void adjustH(){
trimToSize();
for (int i=0; i<size(); i++) {
m=(SMenu)get(i);
m.adjustV();
}
}
void adjustV(){
trimToSize();
for (int i=0; i<size(); i++) {
m=(SMenu)get(i);
m.r.x=m.par.rm.x;
m.r.width=m.par.rm.width; m.rm.x=m.r.x+m.r.width;
m.adjustV();
}
}
public void mouseEvent(MouseEvent event){
if ((vis>=0)&&(event.getID()==MouseEvent.MOUSE_RELEASED)) {
if (action!=null) {action.a(); action=null;};
vis=-1;
if (nam.equals("Popup")) {
app.unregisterDraw(this); app.unregisterMouseEvent(this);
}
}
}
public void draw() {
app.pushStyle(); app.textFont(font);app.fill(0);
for (int i=0; i<size(); i++) {
m=(SMenu)get(i);
if (m.r.contains(app.mouseX,app.mouseY)) {vis=i; m.vis=999;}
}
if (vis>=0){
action=null;
app.fill(220,229,234);app.stroke(200);app.rect(rm.x,rm.y,rm.width,rm.height);
for (int i=0; i<size(); i++) {
m=(SMenu)get(i);
if (vis==i){app.fill(210,225,230);app.stroke(180);app.rect(m.r.x,m.r.y,m.r.width,m.r.height);}
if ((m.act!=null)||(m.size()>0)) app.fill(0); else app.fill(120);
if (m.nam!=null) app.text(m.nam,m.r.x+10,m.r.y+app.textAscent());
if ((m.par.par!=null)&&(m.size()>0))
app.triangle(m.r.x+m.r.width-4,m.r.y+m.r.height/2-3,
m.r.x+m.r.width-4,m.r.y+m.r.height/2+3,
m.r.x+m.r.width-1,m.r.y+m.r.height/2);
if (vis==i) m.draw();
if (m.r.contains(app.mouseX,app.mouseY)) action=m.act;
}
}
app.popStyle();
}
int textW(String s) {
if (s==null) return(0); else
return((int)app.textWidth(s)+20);
}
int textH(String s) {
if (s==null) return(4); else
return((int)(app.textAscent()+app.textDescent()));
}
}
Menu #3 (has to be named actn.java):
Can please someone tell me, what creates the error, and maybe fix it, too?
Another problem I have is, when you go in the Dropdown menu to "Help" --> "About", my credit note is shown for only 1 sec. and disappears...
What also would be cool, if the code could be more structured in menus for example. It's hard for me to keep track of the codes.
- public abstract class actn {
public abstract void a();
}
Can please someone tell me, what creates the error, and maybe fix it, too?
Another problem I have is, when you go in the Dropdown menu to "Help" --> "About", my credit note is shown for only 1 sec. and disappears...
What also would be cool, if the code could be more structured in menus for example. It's hard for me to keep track of the codes.
1