Hello,
I get this error
java.io.InvalidClassException: Editor; no valid constructor
,when I want to serialize my world (Editor).
I have a default constructor there, but do I get this error because of the super statement?
import greenfoot.*;
import java.util.*;
import java.awt.Color;
import java.io.Serializable;
public class Editor extends World implements Serializable
{
//stuff
//another constructor
public Editor(){
super(800, 500, 1, false);
logicHazard = true;
firstChoosen = null;
secondChoosen = null;
selected = new Part[0];
marker = new Image(1,1);
edit = new EditWindow(this);
addObject(edit, (edit.getWidth()/2)-1, getHeight()/2);
edit.prepare();
edit.loadPage(0);
saveButton = new GButton(100,40);
saveButton.setText("save");
addObject(saveButton, getWidth()-saveButton.getWidth()/2, saveButton.getHeight()/2);
//updating the world and its objects
update();
paintOrder();
Greenfoot.start();
}
public void SAVE(){
Greenfoot.setWorld(new FileLoad(this));
}
//much more stuff
}import greenfoot.*;
import java.util.*;
import java.awt.Color;
/**
* displays the saved files for loading or saving
*/
public class FileLoad extends World
{
private final boolean isLoad;
private Editor home;
private Editor[] files;
private Save save;
private Editor selected;
private GLabel[] filesDisp;
private GTextField fileNamer;
private GTickBox[] filesDispSelection;
private GTickBox ticked;
private GButton load, up, down, delete, back;
private int scrollSpeed = 5;
public FileLoad()
{
super(800, 500, 1);
initializeGUI(true);
home = null;
isLoad = true;
fileNamer = null;
ticked = null;
}
/**
* this constructor is used for saving files
*/
public FileLoad(Editor edit){
super(800, 500, 1);
if(edit==null)
throw new IllegalArgumentException("edit can't be null");
home = edit;
initializeGUI(false);
isLoad = false;
if(filesDisp!=null)
if(filesDisp.length>0)
scroll(0, filesDisp[0].getHeight());
fileNamer = new GTextField(400, 50);
fileNamer.setText(home.getName());
addObject(fileNamer, fileNamer.getWidth()/2, fileNamer.getHeight()/2+10);
if(filesDisp!=null){
GTickBox[] t = new GTickBox[filesDispSelection.length+1];
for(int i = 1; i <= filesDispSelection.length; i++)
t[i] = filesDispSelection[i-1];
t[0] = new GTickBox(50,50);
filesDispSelection = t;
if(filesDisp.length>0)
addObject(t[0], fileNamer.getX()+fileNamer.getWidth()/2+t[0].getWidth()/2,
fileNamer.getY());
}
ticked = null;
}
private void initializeGUI(boolean isload){
up = new GButton(50,getHeight()/2-25);
down = new GButton(50, up.getHeight());
up.setText(CreateACircuit.and+"");
down.setText(CreateACircuit.or+"");
addObject(up, getWidth()-up.getWidth()/2, up.getHeight()/2);
addObject(down, getWidth()-down.getWidth()/2, up.getHeight()+ down.getHeight()/2);
load = new GButton(120, 50);
if(isload)
load.setText("Load");
else
load.setText("Save");
addObject(load, getWidth()-load.getWidth()/2, getHeight()-load.getHeight()/2);
delete = new GButton(120, 50);
delete.setText("Delete");
addObject(delete, getWidth()-delete.getWidth()/2-load.getWidth(),
getHeight()-delete.getHeight()/2);
back = new GButton(80, 40);
back.setText("<-");
addObject(back, back.getWidth()/2, getHeight()-back.getHeight()/2);
loadFiles();
displayFiles();
selected = null;
if(files==null){
GLabel i = new GLabel(500, 200);
i.setText("No files found");
i.setBackground(Color.WHITE);
addObject(i, getWidth()/2, getHeight()/2);
}else if(files.length==0){
GLabel i = new GLabel(500, 150);
i.setText("No files found");
i.setBackground(Color.WHITE);
addObject(i, getWidth()/2, getHeight()/2);
}
}
public void act(){
if(isLoad){
if(filesDisp!=null){
if(filesDisp.length>0){
if(Greenfoot.isKeyDown("up")||up.isHold())
if(filesDisp[0].getY()-filesDisp[0].getHeight()/2<10)
scroll(0,-scrollSpeed);
if(Greenfoot.isKeyDown("down")||down.isHold())
if(filesDisp[filesDisp.length-1].getY()+
filesDisp[filesDisp.length-1].getHeight()/2>getHeight()-10)
scroll(0,scrollSpeed);
}
for(int i = 0; i < filesDispSelection.length; i++){
GTickBox t = filesDispSelection[i];
if(Greenfoot.mouseClicked(t)){
selected = files[i];
for(int j = 0; j < filesDispSelection.length; j++)
if(i!=j)
filesDispSelection[i].setTick(false);
}
}
if(load.isPressed()&&selected!=null){
SAVE();
Greenfoot.setWorld(selected);
}else if(delete.isPressed()&&selected!=null){
save.deleteFile(selected);
SAVE();
}
}
}else{
if(filesDisp!=null){
if(filesDisp.length>0){
if(Greenfoot.isKeyDown("up")||up.isHold())
if(filesDisp[0].getY()-filesDisp[0].getHeight()/2<10)
scroll(0,-scrollSpeed);
if(Greenfoot.isKeyDown("down")||down.isHold())
if(filesDisp[filesDisp.length-1].getY()+
filesDisp[filesDisp.length-1].getHeight()/2>getHeight()-10)
scroll(0,scrollSpeed);
}
for(int i = 0; i < filesDispSelection.length; i++){
GTickBox t = filesDispSelection[i];
if(Greenfoot.mouseClicked(t)){
ticked = t;
if(i==0)
selected = null;
else
selected = files[i];
for(int j = 0; j < filesDispSelection.length; j++)
if(i!=j)
filesDispSelection[i].setTick(false);
}
}
}
if(load.isPressed()){
if(save==null)
save = new Save();
if(ticked==null&&fileNamer.getText()!=""){
home.setName(fileNamer.getText());
save.addFile(home);
}else{
home.setName(selected.getName());
save.deleteFile(selected);
save.addFile(home);
}
SAVE();
Greenfoot.setWorld(home);
return;
}else if(delete.isPressed()&&selected!=null){
save.deleteFile(selected);
SAVE();
}
}
if(selected==null){
if(isLoad)
load.setEnabled(false);
delete.setEnabled(false);
}else{
load.setEnabled(true);
delete.setEnabled(true);
}
if(back.isPressed()){
SAVE();
if(isLoad)
Greenfoot.setWorld(new Menu());
else
Greenfoot.setWorld(home);
}
}
private void SAVE(){
if(save==null)
save = new Save();
save.save();
}
private void loadFiles(){
save = (Save) Serialization.deserialize(Save.getName());
if(save!=null)
files = save.getFiles();
else
files = null;
}
private void displayFiles(){
if(files != null){
filesDisp = new GLabel[files.length];
filesDispSelection = new GTickBox[filesDisp.length];
filesDisp[0] = new GLabel(300, 50);
filesDisp[0].setText(files[0].getName());
addObject(filesDisp[0], filesDisp[0].getWidth()/2, filesDisp[0].getHeight()/2+10);
filesDispSelection[0] = new GTickBox(50, 50);
addObject(filesDispSelection[0],
filesDisp[0].getX()+filesDisp[0].getWidth()/2+filesDispSelection[0].getWidth()/2,
filesDisp[0].getY());
for(int i = 1; i < filesDisp.length; i++){
filesDisp[i] = new GLabel(300, 50);
filesDisp[i].setText(files[i].getName());
addObject(filesDisp[i], filesDisp[i].getWidth()/2,
filesDisp[i-1].getY()+filesDisp[i].getHeight());
filesDispSelection[i] = new GTickBox(50, 50);
addObject(filesDispSelection[i],
filesDisp[i].getX()+filesDisp[i].getWidth()/2+filesDispSelection[i].getWidth()/2,
filesDisp[i].getY());
}
}else{
filesDisp = null;
filesDispSelection = null;
}
}
/**
* scrolls the world by the given distance, by moving all Actors
*/
public void scroll(int dx, int dy){
List<Superclass>things = getObjects(Superclass.class);
for(Superclass a : things)
if(a instanceof Interface){
if(a instanceof GLabel||a instanceof GTickBox)
a.setLocation(a.getX()-dx, a.getY()-dy);
}else
a.setLocation(a.getX()-dx, a.getY()-dy);
}
}