2015-10-18 48 views
-1

你能告诉我我的filewriter有什么问题吗?我想将图像的名称和图像坐标保存到文件save.txt中。有什么我失踪?我是否在正确的轨道上?这是我的第二个项目。我的第一个是创建一个可以存储图像的数组。如何将图像名称和坐标保存到save.txt文件中

else if (savePicture.isPointInElement(clickX, clickY)){ //start of save code 
    FileWriter fw = new FileWriter ("save.txt"); 
     for (int i = 0; i < arrayhat.numElements; i++){ 
      if (arrayhat.images[i].isShowing){ 
       fw.write("hat" + arrayhat.images[i].getXCenter() + " " + arrayhat.images[i].getYCenter() + " "); 
      } 
     } 
     for (int i = 0; i < arrayblunt.numElements; i++){ 
      if (arrayblunt.images[i].isShowing){ 
       fw.write("blunt" + arrayblunt.images[i].getXCenter() + " " + arrayblunt.images[i].getYCenter() + " "); 
      } 
     } 
     for (int i = 0; i < arraydealwithit.numElements; i++){ 
      if (arraydealwithit.images[i].isShowing){ 
       fw.write("dealwithit" + arraydealwithit.images[i].getXCenter() + " " + arraydealwithit.images[i].getYCenter() + " "); 
      } 
     } 
     for (int i = 0; i < arrayweed.numElements; i++){ 
      if (arrayweed.images[i].isShowing){ 
       fw.write("weed" + arrayweed.images[i].getXCenter() + " " + arrayweed.images[i].getYCenter() + " "); 
      } 
     } 
     fw.close(); 
     System.out.println("saved"); 
    } 
    else if (loadPicture.isPointInElement(clickX, clickY)){//start of load code 
     Scanner sc = new Scanner (new File("save.txt")); 
     for (int i = 0; i < arrayhat.numElements; i++){ 
      arrayhat.removeimage(arrayhat.images[i], grouphat); 
     } 
     for (int i = 0; i < arrayhat.numElements; i++){ 
      arrayblunt.removeimage(arrayblunt.images[i], groupblunt); 
     } 
     for (int i = 0; i < arrayhat.numElements; i++){ 
      arraydealwithit.removeimage(arraydealwithit.images[i], groupdealwithit); 
     } 
     for (int i = 0; i < arrayhat.numElements; i++){ 
      arrayweed.removeimage(arrayweed.images[i], groupweed); 
     } 
     while (sc.hasNext()){ 
      String word = sc.next(); 
      int x = sc.nextInt(); 
      int y = sc.nextInt(); 
      switch(word){ 
       case "hat": 
        arrayhat.addImage(x, y, grouphat); 
        break; 
       case "blunt": 
        arrayblunt.addImage(x, y, groupblunt); 
        break; 
       case "dealwithit": 
        arraydealwithit.addImage(x, y, groupdealwithit); 
        break; 
       case "weed": 
        arrayweed.addImage(x, y, groupweed); 
        break; 
      } 
     } 
     sc.close(); 
    } 

这是我的贴纸类

public class sticker { 

    public EZImage[] images; 
    public int numElements = 0; 
    public String filename; 

    void arraysticker(EZImage image, String name, int x, int y){ 
     image = EZ.addImage(name, x, y); 
    } 
    public void addImage(int X, int Y, EZGroup group) { 
     images[numElements] = EZ.addImage(filename, X, Y); 
     group.addElement(images[numElements]); 
     numElements++; 
    } 
    public sticker (int size, String file){ 
     filename = file; 
     images = new EZImage[size]; 
    } 
    public void removeimage(EZImage image, EZGroup group){ 
     if (containsElement(image)){ 
      group.removeElement(image); 
      EZ.removeEZElement(image); 
      image = null; 
     } 
    } 
    public boolean containsElement(EZImage element){ 
     for(int i = 0; i < numElements; i++){ 
      if (element == images[i]){ 
       return true; 
      } 
     } 
     return false; 
    } 
} 
+1

描述你的问题更清楚,选择一个反映你的确切问题的标题 –

+0

请告诉我们,如果条件,你的初始“else”块配对,因为此代码可能会完全跳过:导致没有文件被写入。 –

回答

1

你使用普通的Java数组。

他们需要扩大党的阶级

在所有的严重性,这个代码块与一个“其他”关键字开始,可能会跳过基于现有的逻辑:请告诉我们如果条件您最初的“其他“块与之配对。

相关问题