2015-10-18 68 views
0

我会如何写“如果sticker1放置在数组中”然后...我的目标是将贴纸放置并输入到数组中作为名称,并将坐标保存到save.txt文件中。从那里,我可以点击一个加载按钮,它会加载贴纸并将其放置在确切的坐标处。如何将图像的名称及其坐标写入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

你应该有一流的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 
} 

为了解决这个问题,我需要的代码休息,因为我没有关于你的方法和otherr阵列像images

0

至于你需要保存和恢复对象的信息,我建议你使用杰克逊库等一些现成的解决方案。这简化了逻辑,您可以通过使用注释来控制要保存和恢复的内容。

有很多例子。所以你得到你的课,用注解标记属性,那就是。杰克逊lib将为你做所有的事情。看看这个教程:http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial

相关问题