我如何写"如果贴纸1被放置在阵列中"那么......我的目标是将贴纸放置并作为带坐标的名称输入到数组中,然后保存到save.txt文件中。从那里,我可以点击一个加载按钮,它将加载贴纸并将其放在确切的坐标处。
if (hatSoundPlay){ //if hatSoundPlay is true
hatsound.play(); //and a hatsound will play
sticker sticker1 = new sticker();//creates a new sticker1
sticker1.arraysticker(hatPicture, "hat.png", clickX, clickY);//places sticker
image ++;//increments image by 1
arraysticker[image] = sticker1;//puts sticker 1 into the array
然后是保存代码。
else if (savePicture.isPointInElement(clickX, clickY)){
FileWriter fw = new FileWriter("save.txt");
for (int i = 0; i < arraysticker.length; i++);
if (sticker1.images[images].isShowing){
//I want the iff statement ^^ to say if sticker if placed in the array
//fw.write name and coords into save.txt
}
}
答案 0 :(得分:0)
你应该有班级Stricker
,它应该是这样的:
public class Sticker(){
private String name;
private int X;
private int Y;
//There You should place Getters and Setters to variables name,X and Y
//... and your method named 'arraysticker(Object, Image, Integer, Integer)'
}
...下一步是创建正确类型的列表ArrayList<Sticker> list = new ArrayList<>();
我会这样做:
if(hatSoundPlay){
hatsound.play();
Sticker sticker1 = new Sticker();
sticker1.arraysticker(hatPicture, "hat.png", clickX, clickY);
image ++;
list[image] = sticker1;
}
最后你的保存代码应该是:
else if (savePicture.isPointInElement(clickX, clickY)){
FileWriter fw = new FileWriter("save.txt");
for(int i = 0; i < list.length; i++){
if(list[i]!=null){ //if object exist, do...
String name;
int x,y;
name = list.getName();
x = list.getX();
y = list.getY();
fw.write(name + " " + x + " " + y + "\n"); //write all elements to file separated with space and symbol of new line
}
}
fw.close(); //necessary to close connection with file
}
要解决这个问题,我需要其他代码,因为我没有关于你的方法和其他数组的信息,如images
答案 1 :(得分:0)
至于你需要保存和恢复对象,我建议你使用像Jackson库这样的现成解决方案。这简化了逻辑,您只需使用注释即可控制保存和恢复的内容。
有很多例子。所以你得到你的类,用注释标记属性,那就是。 Jackson lib将为您做一切。看一下本教程:http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial