gpt4 book ai didi

java - 字符串变量的 NullPointEException 错误(初学者)

转载 作者:行者123 更新时间:2023-12-02 07:25:45 25 4
gpt4 key购买 nike

我正在尝试模仿此处看到的类似代码 http://www.openprocessing.org/sketch/7050但我似乎对代码有点困惑。我试图获得字母的效果,本质上是绘制图片的海报图像。但我得到了一个 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();

最佳答案

您很可能被 issue 1217 击中了,如果主渲染器是 Java2D,这会阻止您使用 OpenGL PGraphics 渲染器。

该链接有一个解决方法,主要涉及将主渲染器更改为 OpenGL。

较新版本的 PGraphics 应该会为您提供更详细的异常。

关于java - 字符串变量的 NullPointEException 错误(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13609544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com