我创建了一个Light类,它接收三个Vector3fs
,即位置,颜色和衰减。我已经能够创建一个方法将Light保存到配置中:
lightName: (0.0, 1000.0, -7000.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0)
现在我需要一种方法,可以使用保存的信息加载和返回灯光。到目前为止,我有:
public Light getLight(String name) {
String line;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "");
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
任何建议都将不胜感激!谢谢!
---- UPDATE ------
感谢Johnny的回应,我能够弄明白。这是完全有效的代码:
public Light getLight(String name) {
String line;
float x = 0, y = 0, z = 0, r = 0, g = 0, b = 0, x1 = 1, y1 = 0, z1 = 0;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
line = line.replace(name + ": ", "").replace("(", "").replace(")", "");
Scanner parser = new Scanner(line);
parser.useDelimiter(", ");
x = parser.nextFloat();
y = parser.nextFloat();
z = parser.nextFloat();
r = parser.nextFloat();
g = parser.nextFloat();
b = parser.nextFloat();
x1 = parser.nextFloat();
y1 = parser.nextFloat();
z1 = parser.nextFloat();
parser.close();
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
您不需要替换line
中的任何内容,因为您需要的只是数字。但是,您需要解析line
中的数字。
您应该循环或全部解析数字,将每个数字分配给变量,然后创建Light
。
类似于以下内容应该有效:
public Light getLight(String name) {
String line;
double x, y, z, r, g, b, x1, y1, z1;
try {
while((line = bufferedReader.readLine()) != null) {
if(line.startsWith(name)) {
Scanner parser = new Scanner(line);
x = parser.nextDouble();
y = parser.nextDouble();
// continue assigning variables
// break out of while loop, only interested in one line
break;
}
}
return new Light(new Vector3f(x , y, z), new Vector3f(r, g, b), new Vector3f(x1, y1, z1));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}