0

I'm trying to deep copy an array of my custom object "Space", which extends javafx Pane.

I've tried using Kryo libraries, and implementing clonable. I could use work arounds that don't actually deep copy, but that will make my code 100x more janky.

Currently I have the class Space:

public class Space extends Pane {
    private boolean available = true;
    private boolean light;
    private int x;
    private int y;
    private Peice peice;
    private Label peiceImage;
    private Rectangle border;

    public Space() {}


    public Space(int x, int y, boolean light) {
        border = new Rectangle(80,80);
        if (!light) {
            border.setFill(Color.DARKGREEN);
            border.setStroke(Color.DARKGREEN);
        } else {
            border.setFill(null);
            border.setStroke(null);
        }

        peiceImage = new Label();
        peiceImage.setTranslateX(16);
        peiceImage.setTranslateY(16);
        getChildren().addAll(border,peiceImage);

        this.x = x;
        this.y = y;
        this.peice = null;
        this.light = light;
    }
    //other unimportant methods.
}

I then try to copy in another file:

   public Space[][] copyBoard(Space[][] thisBoard) {
        Kryo kryo = new Kryo();
        Space[][] copy = new Space[8][8];
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                Space thisSpace = thisBoard[x][y];
                try {
                    kryo.setRegistrationRequired(false);
                    copy[x][y] = kryo.copy(thisSpace); //TODO This causes exception, dosent work
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
        return copy;
    }

Trying to use the kryo library I get this error:

com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): javafx.scene.layout.Region$3

But I'm not in any way commited to using kryo.

Thanks.

Elias
  • 11
  • 3
  • There are just too many properties and internal variables related to the scene structure that would be changed to an inconsistent state for this to work for general nodes. the best you can do is store the relevant info in a model and implement a way of creating the necessary nodes based on this info... – fabian May 31 '19 at 22:37

1 Answers1

-1

You can do deep copy using serialization and afterward deserialization. There are nice utils to do so on the web. For example SerializationUtils from Apache.

You can do something like the following:

Space clonedSpace = SerializationUtils.clone(space);

Docs

Dominik
  • 353
  • 1
  • 5
  • Thanks. This dosen't work (at least not implemented as simply as you put it) because it extends Pane, a javafx object. – Elias May 31 '19 at 20:10
  • I tried it anyways, but as expected got an error trying to serialize parts of javafx: `org.apache.commons.lang3.SerializationException: java.io.NotSerializableException: javafx.scene.shape.Rectangle` – Elias May 31 '19 at 20:11
  • 2
    JavaFX nodes like Pane don’t implement the Serializable interface. This way won’t work. – Miss Chanandler Bong May 31 '19 at 22:28