作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Processing 中制作一个程序(一种与 Java 非常相似的语言)它将我使用 python 排序的数据放入文本文件中,然后添加到处理草图中并将它们显示在可滚动的气泡中。我希望每次释放鼠标按钮时数组索引都会增加一,以便可以通过单击并释放来查看所有歌曲(即我排序的数据)。问题是处理不允许我使用已将文件加载到 draw()
中的数组。方法即使我的 setup()
和draw()
方法是公开的。它给我的错误是 NullPointerException
第 82 行:
text(songsort[a1],close,15,99,99);
当它可以在 setup()
中读取文件时,它还说我的文件丢失且无法读取。 。我非常感谢您的帮助。
这是我的代码:
import de.looksgood.ani.*;
import de.looksgood.ani.easing.*;
PFont f;
int x=50;
int i=50;
int y=50;
int far=999;
int mid=666;
int close=333;
int end=1333;
int y1=113;
int far2=999;
int mid2=666;
int close2=333;
int end2=1333;
int y2=339;
int far3=999;
int mid3=666;
int close3=333;
int end3=1333;
int y3=567;
int a1=1;
int a2=2;
int a3=3;
int a4=4;
int b1=1;
int b2=2;
int b3=3;
int b4=4;
int c1=1;
int c2=2;
int c3=3;
int c4=4;
public void setup(){
size(1333,680);
smooth();
f = createFont("Georgia", 32);
textFont(f);
textAlign(CENTER, CENTER);
String[] songsort= loadStrings("songsort.txt");
String[] artistsort= loadStrings("yearsort.txt");
String[] yearsort= loadStrings("artistsort.txt");
println("There are "+songsort.length+artistsort.length+yearsort.length+" lines");
Ani.init(this);
Ani.setDefaultEasing(Ani.QUART_IN_OUT);
}
public void draw(){
background(169);
fill(123,43,23);
stroke(0);
rect(x,1,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by year",x,15,100,height/3);
fill(123,43,23);
stroke(0);
ellipse(far,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(mid,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(close,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(end,y1,99,99);
textFont(f);
textSize(16);
fill(255);
text(songsort[1],close,15,99,99); ///////////// Error here! /////////////
if((mouseX<1333) && (mouseX>0) && (mouseY>0) && (mouseY<226) && (mousePressed==true)){
x=x-5;
end=end-5;
far=far-5;
mid=mid-5;
close=close-5;
}
fill(43,123,23);
stroke(255);
rect(i,226.66,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by song name",i,226.66,100,height/3);
fill(43,123,23);
stroke(255);
ellipse(far2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(mid2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(close2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(end2,y2,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>226.66) && (mouseY<453.34) && (mousePressed==true)){
i=i-5;
end2=end2-5;
far2=far2-5;
mid2=mid2-5;
close2=close2-5;
}
fill(240,179,93);
stroke(147);
rect(y,453.34,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by artist",y,453.34,100,height/3);
fill(240,179,93);
stroke(147);
ellipse(far3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(mid3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(close3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(end3,y3,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>453.34) && (mouseY<680) && (mousePressed==true)){
y=y-5;
end3=end3-5;
far3=far3-5;
mid3=mid3-5;
close3=close3-5;
}
}
void mouseReleased(){
Ani.to(this, 1.0, "close", 133);
Ani.to(this, 1.0, "mid", 466);
Ani.to(this, 1.0, "far", 799);
Ani.to(this, 1.0, "end", 1133);
Ani.to(this, 1.0, "close2", 133);
Ani.to(this, 1.0, "mid2", 466);
Ani.to(this, 1.0, "far2", 799);
Ani.to(this, 1.0, "end2", 1133);
Ani.to(this, 1.0, "close3", 133);
Ani.to(this, 1.0, "mid3", 466);
Ani.to(this, 1.0, "far3", 799);
Ani.to(this, 1.0, "end3", 1133);
}
谢谢!
最佳答案
您的数组在函数 setup() 中被声明为局部变量,因此它们在该函数之外不可见。为了使它们在类的其他函数中可见,您应该将它们声明为此类的字段。
public class YourClass {
private String[] songsort;
private String[] artistsort;
private String[] yearsort;
public void setup() {
...
songsort = loadStrings("songsort.txt");
artistsort = loadStrings("yearsort.txt");
yearsort = loadStrings("artistsort.txt");
...
}
}
关于java - 如何在draw()方法中使用数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224215/
我是一名优秀的程序员,十分优秀!