Editable pictures, how to save/load?
Greetings all!
I try to save/load pictures what users could add. To give you a picture: A user could click the mouse, a tree will appear. A user could click with the right mousebutton, and a hero will appear. A user could click middle mouse button, and a dragon will appear, and a user could hold the mouse, and a wall will appear.
All these objects are JPanels, with event listeners.
(spelElementen is a arraylist of JPanels (tree, hero, dragon and wall)
Code:
public void bestandschrijven()
{
dialoogvenster =
new FileDialog( venster, "Bewaar een bestand", FileDialog.SAVE);
dialoogvenster.setVisible( true );
String bestandsnaam = dialoogvenster.getFile();
if (bestandsnaam != null)
{
String padEnBestand = dialoogvenster.getDirectory() + bestandsnaam;
try
{
ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream( padEnBestand ));
uit.writeObject( spelElementen );
spelElementen.clear();
repaint();
uit.close();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(
null,
"Er is een fout opgetreden bij het schrijven.",
"Bewaren van bestand is mislukt",
JOptionPane.WARNING_MESSAGE);
}
}
}
public void bestandlezen()
{
dialoogvenster =
new FileDialog( venster, "Open een bestand", FileDialog.LOAD );
dialoogvenster.setVisible( true );
String bestandsnaam = dialoogvenster.getFile();
if (bestandsnaam != null)
{
String padEnBestand = dialoogvenster.getDirectory() + bestandsnaam;
try
{
ObjectInputStream in = new ObjectInputStream( new FileInputStream( padEnBestand ) );
spelElementen.clear();
removeMouseListener(this);
removeMouseMotionListener(this);
spelElementen = (ArrayList) in.readObject();
repaint();
addMouseListener(this);
addMouseMotionListener(this);
in.close();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(
null,
"Er is een fout opgetreden bij het lezen.",
"Inlezen van het bestand is mislukt",
JOptionPane.WARNING_MESSAGE);
}
catch ( ClassNotFoundException ex )
{
JOptionPane.showMessageDialog(
null,
"Er is een fout opgetreden bij het lezen.",
"Inlezen van het bestand is mislukt",
JOptionPane.WARNING_MESSAGE);
}
}
}