I am trying to have a class with an Image (javaFX) property saved in a binary file but when the Image is different than null I get an IOException.
Is it possible to make this work either by using an Image object or in some other way?
Here is a Demo class showing the problem.
import java.io.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.image.Image;
public class Demo extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
TheObject obj1 = new TheObject(1, new Image("image1.jpg"));
TheObject obj2 = new TheObject(2, new Image("image2.jpg"));
TheObject obj3 = new TheObject(3, new Image("image3.jpg"));
ObjectOutputStream outputStream;
try {
outputStream = new ObjectOutputStream(new FileOutputStream("file"));
outputStream.writeObject(obj1);
outputStream.writeObject(obj2);
outputStream.writeObject(obj3);
}
catch(IOException e) {
System.out.println("IOException");
System.exit(0);
}
}
public static class TheObject {
private int id;
private Image img;
TheObject(int id, Image img) {
this.id = id;
this.img = img;
}
public int getID() { return id; }
public Image getImg() { return img; }
public void setID(int id) { this.id = id; }
public void setImg(Image img) { this.img = img; }
}
}