如何将图像名称和坐标保存到save.txt文件中

时间:2015-10-18 09:26:52

标签: java arrays filewriter

你能告诉我filewriter有什么问题吗?我想要将图像的名称和图像坐标保存到文件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 :(得分:1)

您正在使用常规Java数组。

他们需要扩展Party类

严肃地说,这段代码以“else”关键字开头,可能会根据先前的逻辑跳过:请告诉我们你的初始“其他”块与if配对的条件。