2012-11-28 79 views
0

我想模仿一個類似的代碼在這裏看到http://www.openprocessing.org/sketch/7050但我似乎已經有點與代碼混淆了。我試圖得到這些字母的效果,實際上是畫出了這幅畫的海報圖像。但我得到一個NullPointException,並假設是與我如何初始化字符串變量,但我似乎無法找到我做錯了什麼。NullPointException與字符串變量錯誤(初學者)

的錯誤

Exception in thread "Animation Thread" java.lang.NullPointerException 
    at processing.opengl.PGL.getString(PGL.java:1029) 
    at processing.opengl.PGraphicsOpenGL.getGLParameters(PGraphicsOpenGL.java:6076) 
    at processing.opengl.PGraphicsOpenGL.beginDraw(PGraphicsOpenGL.java:1547) 
    at MLKpractice.letterfit(MLKpractice.java:147) 
    at MLKpractice.draw(MLKpractice.java:98) 
    at processing.core.PApplet.handleDraw(PApplet.java:2120) 
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:197) 
    at processing.core.PApplet.run(PApplet.java:1998) 
    at java.lang.Thread.run(Thread.java:680) 

該計劃說,錯誤是在線127

lettersquare.beginDraw(); 

但我相信錯誤是上述地方

PFont font; 
String fontpath = "ArialMT-200.vlw"; 
int fontstart = 300; 
int fontend = 8; 
float fontsize = fontstart; 
float fontsizedecrease = 0.97; 
PImage bg; 
PImage australia; 
PImage austria; 
String country1 = "australia.jpg"; 
String country2 = "austria.jpg"; 
String letters = "Australia"; 
char[] chars = new char[52]; 
int nchars = 0; 
int iterations = 500; 
int c = 0; 
PGraphics letter,lettersquare,drawing; 

void setup(){ 
    //initialize the sketch 
    size(900,600); 
    //background(255); 
    //initialize the font 
    //font = loadFont(fontpath); 
    ///* 
    for(int i=0;i<letters.length();i++){ 
    boolean found = false; 
    char lc = letters.charAt(i); 
    for(int j=0;j<nchars;j++){ 
     if(chars[j]==lc){ 
     found = true; 
     break; 
     } 
    } 
    if(!found) chars[nchars++] = lc; 
    } 
    chars = (char[]) subset(chars,0,nchars); 
    font = createFont("Arial",200,true,chars); 
    //*/ 
    textAlign(CENTER,CENTER); 
    //load the image that will be filled with letters 
    australia = loadImage(country1); 
    austria = loadImage(country2); 

    bg = loadImage("background.jpg"); 
    //posterize the image 
    australia.filter(THRESHOLD,0.4); 
    australia.filter(BLUR,3); 
    australia.filter(THRESHOLD,0.6); 
    //initialize the drawing buffers 
    letter = createGraphics((int)fontsize,(int)fontsize,JAVA2D); 
    lettersquare = createGraphics((int)fontsize,(int)fontsize,P2D); 
    drawing = createGraphics(width,height,JAVA2D); 
    drawing.beginDraw(); 
    drawing.background(255); 
    // THIS STUPID THING NEEDS TO GO HERE!!!! 
    drawing.image(bg,0,0); 
    drawing.endDraw(); 
} 

void draw(){ 

    if(floor(fontsize)>fontend&&c<letters.length()-1){ 
    if(!letterfit()){ 
     fontsize *= fontsizedecrease; 
    }else{ 
     c++; 
     if(c==11){ 
     fontsize *= 0.75; 
     } 
    } 

    tint(255); 
    image(drawing,0,0); 


    if (keyCode == LEFT) { 
    image(austria,0,0); 
    } 
    // if (keyCode == RIGHT) { 
    // frog1.frogx = frog1.frogx + 1; 
    // } 


    if(mousePressed){ 
     tint(255,100); 
     image(australia,0,0); 
    } 
    }else{ 
    tint(255); 
    image(drawing,0,0); 
    println(c+" "+letters.length()); 
    /* 
    save("mlk-"+hour()+""+minute()+""+second()+".tif"); 
    exit(); 
    */ 
    noLoop(); 
    } 
} 
boolean letterfit(){ 
    letter.beginDraw(); 
    letter.background(255,0); 
    letter.fill(0); 
    letter.textAlign(CENTER,CENTER); 
    letter.translate(fontsize/2,fontsize/2); 
    letter.rotate(random(TWO_PI)); 
    letter.scale(fontsize/fontstart); 
    letter.textFont(font); 
    letter.smooth(); 
    letter.text(letters.charAt(c),0,0); 
    letter.endDraw(); 
    lettersquare.beginDraw(); 
+0

在你調用letterfit()之前你有沒有調用過setup()? –

+0

@JoachimIsaksson是的,我不打電話給信函,直到後來 –

+0

我會在letterfit()的開頭打印出字母的值。它可能是0.不知何故! – emrys57

回答

2

你最有可能得到點擊issue 1217,這可以防止您使用OpenGL PGraphics渲染器,如果主rende rer是Java2D。

該鏈接有一個解決方法,基本上涉及將主渲染器更改爲OpenGL。

更新版本的PGraphics會給你一個更詳細的例外。

+0

啊我現在可以看到,這是全部因爲處理2.0b1?我只是擔心,因爲我使用這個作業和學校計算機運行在以前的版本,我的原代碼工作。 –

+0

@ChristineAustin據我所知,它只與2.x版本有關,1.x應該可以工作。 –

+0

好的,tysm爲您提供幫助!我想我會玩它保存並在1.x版本上運行,因爲代碼在那裏工作。我本人從來沒有注意到這一點!天才 :] –