简体   繁体   English

序列化一个圆[] []?

[英]Serialize a Circle[][]?

I have a pixel editor app and I need to save to a file the pixelArray created. 我有一个像素编辑器应用程序,我需要将创建的pixelArray保存到文件中。 Serializing the pixelArray of Circle objects seems like the practical solution so I create the following class: 序列化Circle对象的pixelArray似乎是实际的解决方案,因此我创建了以下类:

public class PixelArray implements Serializable{
    static Circle[][] pixelArray;
    public static  Circle[][] pixArray (int row, int col){
        pixelArray = new Circle[row][col];
        return pixelArray;
    }
}

The SaveFile class includes a JFileChooser and saveFile method for to Serialize the pixelArray as follows: SaveFile类包含一个JFileChooser和saveFile方法,用于对pixelArray进行序列化,如下所示:

public class SaveFile { 公共类SaveFile {

public static void write(){
    String fileName;
    fileName = "PixelArray1.pix";
    JFileChooser fileDialog = new JFileChooser( "C:\\Program Files (x86)\\Visual Art\\Playlist\\");
    File selectedFile = new File(fileName);
    fileDialog.setSelectedFile(selectedFile);
    fileDialog.setDialogTitle("Save Pixel File");
    fileDialog.setSelectedFile(null);
    fileDialog.setSize(400, 400);
    fileDialog.setVisible(true);
    int option = fileDialog.showSaveDialog(null);
    if(option != JFileChooser.APPROVE_OPTION)
        return;                                         //user canceled of clicked the dialog's close box
    selectedFile = fileDialog.getSelectedFile();
    if(selectedFile.exists()){                          //ask the user to replace this file
        int response = JOptionPane.showConfirmDialog(null,"The file \""+ selectedFile.getName() +
        "\" already exists.\nDo you want to replace it?",
        "Confirm Save",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.WARNING_MESSAGE );
        if(response != JOptionPane.YES_OPTION) return;  //user does not want ot replace the fle
    }
    saveFile(pixelArray, "PixelArray1.pix");                 //Serialize
}

public static void saveFile(Circle[][] pixelArray, String fileName){      //Serialize
    try {
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(pixelArray);
        out.flush();
        out.close();
    }
    catch (IOException e){
        System.out.println(e);
    }
}

Try as I may as a newbie, I can't get past the exception: 作为新手,请尝试尝试以下操作:

java.io.NotSerializableException: javafx.scene.shape.Circle java.io.NotSerializableException:javafx.scene.shape.Circle

Serializing a Circle[][] shouldn't be that hard; 序列化一个Circle [] []并不难。 what am I missing, please? 我想念什么?

Probably you should read documentation of the specific exception, it states: 可能您应该阅读有关特定异常的文档,其中指出:

Thrown when an instance is required to have a Serializable interface. 当需要实例具有可Serializable接口时抛出。 The serialization runtime or the class of the instance can throw this exception. 序列化运行时或实例的类可以引发此异常。 The argument should be the name of the class. 参数应为类的名称。

This because Java serialization requires to tag classes which are supposed to be serializable with the Serializable interface: 这是因为Java序列化要求标记应该通过Serializable接口可序列Serializable

class Circle implements Serializable
{
  ...
}

If the class you are working on is not modifiable, then you must provide your own bridge class, eg: 如果您正在处理的类是不可修改的,那么您必须提供自己的桥类,例如:

class SerializableCircle implements Serializable
{
  // create a serializable circle from a Circle
  SerializableCircle(Circle circle) {
    ..
  }

  // create a Circle back from an unserialized circle
  Circle unserialize()
  {
    ..
  }
};

and then serialize and unserialize this. 然后进行序列化和反序列化。

Since javafx.scene.shape.Circle is not serializable, you need to serialize something that is actually serializable. 由于javafx.scene.shape.Circle无法序列化,因此您需要序列化实际上可序列化的内容。

To accomplish this, you could write a wrapper class who implements Serializable that has all the data needed to recreate a circle object when it is deserialized. 为此,您可以编写一个包装类,该类实现Serializable,该类具有反序列化时重新创建圆形对象所需的所有数据。

Here's something that should help with making a serializable class: http://www.javapractices.com/topic/TopicAction.do?Id=45 这应该有助于制作可序列化的类: http : //www.javapractices.com/topic/TopicAction.do? Id= 45

Now, I believe from the Answers given, it would be more practical to drop Serialization and figure out how to convert my Circle[][] to a Color[]. 现在,我相信从给出的答案中 ,删除序列化并弄清楚如何将Circle [] []转换为Color []将更为实用。 Then I would need to read/write Color[] to a file to Save/Open and send via Wi-Fi to microcontroller. 然后,我需要将Color []读/写到文件以进行保存/打开,然后通过Wi-Fi发送到微控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM