diff --git a/HELP b/HELP index 5ab6642..c5cf48c 100755 --- a/HELP +++ b/HELP @@ -1,4 +1,5 @@ MOOBREEDER HELP +=============== MooBreeder is a simple cowbreeding game designed for educational use at Key Stage 3. It demonstrates the basic principles of selective breeding. diff --git a/HELP b/HELP index 5ab6642..c5cf48c 100755 --- a/HELP +++ b/HELP @@ -1,4 +1,5 @@ MOOBREEDER HELP +=============== MooBreeder is a simple cowbreeding game designed for educational use at Key Stage 3. It demonstrates the basic principles of selective breeding. diff --git a/Herd.java b/Herd.java index 4f96b8f..15da9a5 100755 --- a/Herd.java +++ b/Herd.java @@ -25,7 +25,6 @@ private ArrayList herd; private Cow[] parents; private Cow[] offspring; - private int selection; private Herd() { @@ -33,8 +32,8 @@ parents = new Cow[] {null, null}; offspring = new Cow[] {null, null}; for (int i = 0; i < initialSize; i++) { - String sex = "male"; - if ((i % 2) == 0) sex = "female"; + String sex = "female"; + if ((i % 2) == 0) sex = "male"; herd.add(new Cow(sex)); } } @@ -48,6 +47,16 @@ return singleton; } + public int size() + { + return herd.size(); + } + + public Cow getNthCow(int n) + { + return herd.get(n); + } + public Cow getCow(int id) { for (int c = 0; c < herd.size(); c++) { @@ -63,14 +72,12 @@ if (c != null) herd.remove(c); } - public void select(int id) + public boolean isParent(int id) { - selection = id; - } - - public int selected() - { - return selection; + if ((parents[0] != null && parents[0].getID() == id) || + (parents[1] != null && parents[1].getID() == id)) + return true; + else return false; } public void setParent(int id) @@ -115,7 +122,15 @@ if (noffs == 2) offspring[1] = new Cow(parents[0], parents[1]); } - public void keepChild(int id) + public boolean isCalf(int id) + { + if ((offspring[0] != null && offspring[0].getID() == id) || + (offspring[1] != null && offspring[1].getID() == id)) + return true; + else return false; + } + + public void keepCalf(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; @@ -126,7 +141,7 @@ } } - public void discardChild(int id) + public void discardCalf(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; diff --git a/HELP b/HELP index 5ab6642..c5cf48c 100755 --- a/HELP +++ b/HELP @@ -1,4 +1,5 @@ MOOBREEDER HELP +=============== MooBreeder is a simple cowbreeding game designed for educational use at Key Stage 3. It demonstrates the basic principles of selective breeding. diff --git a/Herd.java b/Herd.java index 4f96b8f..15da9a5 100755 --- a/Herd.java +++ b/Herd.java @@ -25,7 +25,6 @@ private ArrayList herd; private Cow[] parents; private Cow[] offspring; - private int selection; private Herd() { @@ -33,8 +32,8 @@ parents = new Cow[] {null, null}; offspring = new Cow[] {null, null}; for (int i = 0; i < initialSize; i++) { - String sex = "male"; - if ((i % 2) == 0) sex = "female"; + String sex = "female"; + if ((i % 2) == 0) sex = "male"; herd.add(new Cow(sex)); } } @@ -48,6 +47,16 @@ return singleton; } + public int size() + { + return herd.size(); + } + + public Cow getNthCow(int n) + { + return herd.get(n); + } + public Cow getCow(int id) { for (int c = 0; c < herd.size(); c++) { @@ -63,14 +72,12 @@ if (c != null) herd.remove(c); } - public void select(int id) + public boolean isParent(int id) { - selection = id; - } - - public int selected() - { - return selection; + if ((parents[0] != null && parents[0].getID() == id) || + (parents[1] != null && parents[1].getID() == id)) + return true; + else return false; } public void setParent(int id) @@ -115,7 +122,15 @@ if (noffs == 2) offspring[1] = new Cow(parents[0], parents[1]); } - public void keepChild(int id) + public boolean isCalf(int id) + { + if ((offspring[0] != null && offspring[0].getID() == id) || + (offspring[1] != null && offspring[1].getID() == id)) + return true; + else return false; + } + + public void keepCalf(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; @@ -126,7 +141,7 @@ } } - public void discardChild(int id) + public void discardCalf(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; diff --git a/MooBreeder.java b/MooBreeder.java index 051a417..ad35f6b 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -61,8 +61,7 @@ //TODO save,load,help save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK)); load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK)); - reset.addActionListener(new ActionListener() - { + reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int restart = JOptionPane.showConfirmDialog(null, "Reset the game?", "Reset?", JOptionPane.OK_CANCEL_OPTION, @@ -71,10 +70,8 @@ } }); reset.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); - help.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent e) - { + help.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { helpWindow.setVisible(true); } }); @@ -140,8 +137,16 @@ private void updateHerdBox() { //TODO + herdBox.removeAll(); JLabel label = new JLabel("Your current herd:"); herdBox.add(label); + herdBox.add(Box.createVerticalStrut(3)); + for (int c = 0; c < Herd.getInstance().size(); c++) { + Box cowBox = getCowBox(Herd.getInstance().getNthCow(c)); + herdBox.add(cowBox); + herdBox.add(Box.createVerticalStrut(1)); + } + herdBox.validate(); } private void updateParentBox() @@ -171,10 +176,20 @@ private Box getCowBox(Cow c) { - //TODO Box cowBox = new Box(BoxLayout.Y_AXIS); cowBox.setName(String.valueOf(c.getID())); - //TODO display cow name, traits and rating + JLabel name = new JLabel(c.getName()); + JLabel genealogy = new JLabel("Generation "+c.getGeneration()+ + " Father "+c.getParents()[0]+ + " Mother "+c.getParents()[1]); + //TODO display cow traits and rating + cowBox.add(Box.createGlue()); + cowBox.add(name); + cowBox.add(Box.createVerticalStrut(3)); + cowBox.add(Box.createGlue()); + cowBox.add(genealogy); + //TODO insert remaining labels + cowBox.add(Box.createGlue()); cowBox.addMouseListener(this); return cowBox; } @@ -186,19 +201,83 @@ @Override public void mouseClicked(MouseEvent me) { - //TODO - int prevSel = Herd.getInstance().selected(); - int nextSel = Integer.parseInt(me.getComponent().getName()); - Herd.getInstance().select(nextSel); - - - //getClickCount() - - //TODO double-click: set/remove as parent, add to herd - //TODO left-click: context menu (as double-click, remove) + System.out.println("Recognised a mouse event."); //DEBUG + int selectionID = Integer.parseInt(me.getComponent().getName()); + //Respond to the mouse click with an action or a context menu + if (me.getButton() == MouseEvent.BUTTON3) { + mouseSideAction(selectionID, me); + } + else if (me.getButton() == MouseEvent.BUTTON1 && + me.getClickCount() == 2) { + mouseMainAction(selectionID); + } updateGUI(); } + /* + * The action launched by a double left click or the first context menu: + * set/remove a parent or add a calf to the herd. + */ + private void mouseMainAction(int selection) + { + System.out.println("Carrying out mouse main action on animal #"+selection); //DEBUG + if (Herd.getInstance().isParent(selection)) + Herd.getInstance().removeParent(selection); + else if (Herd.getInstance().isCalf(selection)) + Herd.getInstance().keepCalf(selection); + else Herd.getInstance().setParent(selection); + } + + /* + * The context menu launched by a right click. Offers the main mouse action + * (see above), or the option to discard/delete a calf or adult animal. + */ + private void mouseSideAction(int selection, MouseEvent me) + { + System.out.println("Carrying out mouse side action on animal #"+selection); //DEBUG + //Initialise menu + JPopupMenu contextMenu = new JPopupMenu(); + JMenuItem item1 = new JMenuItem(); + JMenuItem item2 = new JMenuItem(); + //Change item labels and actions depending on context + item1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + mouseMainAction(selection); //always the same + } + }); + if (Herd.getInstance().isParent(selection)) { + item1.setText("Remove parent"); + } + else if (Herd.getInstance().isCalf(selection)) { + item1.setText("Add calf to herd"); + item2.setText("Discard calf"); + item2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Herd.getInstance().discardCalf(selection); + } + }); + } + else { + item1.setText("Select for breeding"); + item2.setText("Delete from herd"); + item2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String question = "Delete "+Herd.getInstance(). + getCow(selection).getName()+" from herd?"; + int confirm = JOptionPane.showConfirmDialog(null, question, "Delete cow?", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) + Herd.getInstance().removeCow(selection); + } + }); + } + //Add menu items and show menu + contextMenu.add(item1); + if (item2.getText() != "") contextMenu.add(item2); + contextMenu.show(me.getComponent(), me.getX(), me.getY()); + } + @Override public void mouseEntered(MouseEvent arg0) {}