diff --git a/Cow.java b/Cow.java index 658d665..8ac2e88 100755 --- a/Cow.java +++ b/Cow.java @@ -13,8 +13,9 @@ import java.util.HashMap; import java.util.Iterator; +import java.io.Serializable; -public class Cow +public class Cow implements Serializable { private static int idCounter = 1; private static int maxGen = 1; diff --git a/Cow.java b/Cow.java index 658d665..8ac2e88 100755 --- a/Cow.java +++ b/Cow.java @@ -13,8 +13,9 @@ import java.util.HashMap; import java.util.Iterator; +import java.io.Serializable; -public class Cow +public class Cow implements Serializable { private static int idCounter = 1; private static int maxGen = 1; diff --git a/Herd.java b/Herd.java index f0f91b6..c42b206 100755 --- a/Herd.java +++ b/Herd.java @@ -15,7 +15,7 @@ import java.util.Random; import java.io.*; -public class Herd +public class Herd implements Serializable { public static int initialSize = 4; private static Herd singleton; @@ -69,7 +69,10 @@ public void removeCow(int id) { Cow c = getCow(id); - if (c != null) herd.remove(c); + if (c != null) { + if (isParent(id)) removeParent(id); + herd.remove(c); + } } public Cow[] getParents() @@ -158,8 +161,6 @@ else if (offspring[1] != null && offspring[1].getID() == id) o = 1; if (o >= 0) offspring[o] = null; } - - //TODO Test the serialization code! public static void saveHerd(String filename) { @@ -177,7 +178,7 @@ public static void loadHerd(String filename) { - singleton = null; + resetHerd(); try { FileInputStream fileIn = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fileIn); diff --git a/Cow.java b/Cow.java index 658d665..8ac2e88 100755 --- a/Cow.java +++ b/Cow.java @@ -13,8 +13,9 @@ import java.util.HashMap; import java.util.Iterator; +import java.io.Serializable; -public class Cow +public class Cow implements Serializable { private static int idCounter = 1; private static int maxGen = 1; diff --git a/Herd.java b/Herd.java index f0f91b6..c42b206 100755 --- a/Herd.java +++ b/Herd.java @@ -15,7 +15,7 @@ import java.util.Random; import java.io.*; -public class Herd +public class Herd implements Serializable { public static int initialSize = 4; private static Herd singleton; @@ -69,7 +69,10 @@ public void removeCow(int id) { Cow c = getCow(id); - if (c != null) herd.remove(c); + if (c != null) { + if (isParent(id)) removeParent(id); + herd.remove(c); + } } public Cow[] getParents() @@ -158,8 +161,6 @@ else if (offspring[1] != null && offspring[1].getID() == id) o = 1; if (o >= 0) offspring[o] = null; } - - //TODO Test the serialization code! public static void saveHerd(String filename) { @@ -177,7 +178,7 @@ public static void loadHerd(String filename) { - singleton = null; + resetHerd(); try { FileInputStream fileIn = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fileIn); diff --git a/MooBreeder.java b/MooBreeder.java index 16beba3..7dc9d2d 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -17,7 +17,9 @@ import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; +import javax.swing.filechooser.*; import java.util.HashMap; +import java.io.File; public class MooBreeder extends JFrame implements MouseListener { @@ -29,7 +31,8 @@ private JPanel breedPanel; private JScrollPane herdScroller; private Box herdBox, motherBox, fatherBox, calf1Box, calf2Box; - private JButton add, breed, keep, remove; + private JButton breed; + private JFileChooser fileChooser; //TODO Insert the "breed" button! @@ -44,10 +47,13 @@ public MooBreeder() { this.setTitle("MooBreeder"); - this.setSize(800, 400); + this.setSize(800, 400); //FIXME Adjust width and spacing this.setDefaultCloseOperation(EXIT_ON_CLOSE); goal = "dairy"; //FIXME helpWindow = new HelpWindow(); + fileChooser = new JFileChooser(System.getProperty("user.dir")); + FileFilter datFilter = new FileNameExtensionFilter("MooBreeder (.dat) files", "dat"); + fileChooser.setFileFilter(datFilter); this.createMenu(); this.createLayout(); //TODO @@ -65,8 +71,35 @@ quit = new JMenuItem("Quit"); help = new JMenuItem("Help"); about = new JMenuItem("About"); - //TODO save,load,help + save.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int saveConfirm = fileChooser.showSaveDialog(null); + if (saveConfirm == JFileChooser.APPROVE_OPTION) { + File saveFile = fileChooser.getSelectedFile(); + if (!saveFile.getName().endsWith(".dat")) + saveFile = new File(saveFile.getAbsolutePath()+".dat"); + Herd.saveHerd(saveFile.getAbsolutePath()); + JOptionPane.showMessageDialog(null, "Current herd saved to "+saveFile.getName(), + "Game saved", JOptionPane.INFORMATION_MESSAGE); + } + } + }); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK)); + load.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int loadConfirm = fileChooser.showOpenDialog(null); + if (loadConfirm == JFileChooser.APPROVE_OPTION) { + File loadFile = fileChooser.getSelectedFile(); + int userConfirm = JOptionPane.showConfirmDialog(null, "Load herd from "+loadFile.getName()+"?", + "Confirm", JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE); + if (userConfirm == JOptionPane.OK_OPTION) { + Herd.loadHerd(loadFile.getAbsolutePath()); + updateGUI(); + } + } + } + }); load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK)); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -114,7 +147,6 @@ private void createLayout() { - //TODO this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS)); //Create the left half of the window with the herd box JPanel herdBorderPanel = new JPanel(new BorderLayout()); @@ -129,6 +161,7 @@ herdBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.WEST); this.add(herdBorderPanel); //Create the right half of the window with the breeding interface + //TODO - breed button JPanel breedBorderPanel = new JPanel(new BorderLayout()); breedPanel = new JPanel(new GridLayout(2,2)); fatherBox = new Box(BoxLayout.Y_AXIS); @@ -212,7 +245,7 @@ private void resetGame() { Herd.getInstance().resetHerd(); - updateGUI(); + updateGUI(); //FIXME ScrollPane disappears? - only until window is resized } private Box getCowBox(Cow c) @@ -254,6 +287,8 @@ /* * MouseListener methods + * + * FIXME Updating the GUI does not yet work as and when intended */ @Override