Parent and offspring boxes now shown.
1 parent df2cdc3 commit b1745f2dbbe50c01de85e6bf9d35c94be4389148
@Daniel Vedder Daniel Vedder authored on 28 Sep 2018
Showing 2 changed files
View
10
Herd.java
public void removeCow(int id)
{
Cow c = getCow(id);
if (c != null) herd.remove(c);
}
 
public Cow[] getParents()
{
return parents;
}
 
public boolean isParent(int id)
{
offspring[0] = new Cow(parents[0], parents[1]);
if (noffs == 2) offspring[1] = new Cow(parents[0], parents[1]);
}
 
public Cow[] getCalves()
{
return offspring;
}
 
public boolean isCalf(int id)
{
if ((offspring[0] != null && offspring[0].getID() == id) ||
(offspring[1] != null && offspring[1].getID() == id))
View
89
MooBreeder.java
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class MooBreeder extends JFrame implements MouseListener
{
private JMenuBar menubar;
helpMenu = new JMenu("Help");
save = new JMenuItem("Save");
load = new JMenuItem("Load");
reset = new JMenuItem("Reset");
quit = new JMenuItem("Exit");
quit = new JMenuItem("Quit");
help = new JMenuItem("Help");
about = new JMenuItem("About");
//TODO save,load,help
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
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());
JLabel herdlabel = new JLabel("Your current herd:", JLabel.CENTER);
herdBorderPanel.add(herdlabel, BorderLayout.NORTH);
herdBox = new Box(BoxLayout.Y_AXIS);
herdScroller = new JScrollPane(herdBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
herdScroller.setWheelScrollingEnabled(true);
this.add(herdScroller);
herdBorderPanel.add(herdScroller, BorderLayout.CENTER);
herdBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.SOUTH);
herdBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.EAST);
herdBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.WEST);
this.add(herdBorderPanel);
//Create the right half of the window with the breeding interface
breedPanel = new JPanel(new GridLayout(2,2));
fatherBox = new Box(BoxLayout.Y_AXIS);
motherBox = new Box(BoxLayout.Y_AXIS);
calf1Box = new Box(BoxLayout.Y_AXIS);
{
updateHerdBox();
updateParentBox();
updateOffspringBox();
this.repaint();
}
 
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));
}
 
private void updateParentBox()
{
//TODO
JLabel dl = new JLabel("Father");
JLabel ml = new JLabel("Mother");
fatherBox.removeAll();
motherBox.removeAll();
//Label boxes
JLabel dl = new JLabel("Father:");
JLabel ml = new JLabel("Mother:");
fatherBox.add(dl);
motherBox.add(ml);
//Include information boxes
Cow[] parents = Herd.getInstance().getParents();
if (parents[0] != null) {
Box db = getCowBox(parents[0]);
fatherBox.add(Box.createVerticalStrut(10));
fatherBox.add(db);
}
if (parents[1] != null) {
Box mb = getCowBox(parents[1]);
motherBox.add(Box.createVerticalStrut(10));
motherBox.add(mb);
}
fatherBox.validate();
motherBox.validate();
}
 
 
private void updateOffspringBox()
{
//TODO
JLabel l1 = new JLabel("Calf 1");
JLabel l2 = new JLabel("Calf 2");
calf1Box.removeAll();
calf2Box.removeAll();
JLabel l1 = new JLabel("Calf 1:");
JLabel l2 = new JLabel("Calf 2:");
calf1Box.add(l1);
calf2Box.add(l2);
//Include information boxes
Cow[] calves = Herd.getInstance().getCalves();
if (calves[0] != null) {
Box db = getCowBox(calves[0]);
fatherBox.add(Box.createVerticalStrut(10));
fatherBox.add(db);
}
if (calves[1] != null) {
Box mb = getCowBox(calves[1]);
motherBox.add(Box.createVerticalStrut(10));
motherBox.add(mb);
}
calf1Box.validate();
calf2Box.validate();
}
 
private void resetGame()
{
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(Box.createGlue());
cowBox.add(name);
cowBox.add(Box.createVerticalStrut(3));
cowBox.add(Box.createGlue());
//cowBox.add(Box.createGlue());
cowBox.add(genealogy);
//TODO insert remaining labels
cowBox.add(Box.createGlue());
//cowBox.add(Box.createGlue());
cowBox.add(Box.createVerticalStrut(5));
cowBox.setBorder(new BevelBorder(BevelBorder.RAISED));
cowBox.addMouseListener(this);
return cowBox;
}
 
JPopupMenu contextMenu = new JPopupMenu();
JMenuItem item1 = new JMenuItem();
JMenuItem item2 = new JMenuItem();
//Change item labels and actions depending on context
//FIXME Problem with animals occurring both in the herd list and the parent slots
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mouseMainAction(selection); //always the same
}
});
if (Herd.getInstance().isParent(selection)) {
item1.setText("Remove parent");
item1.setText("Remove as parent");
}
else if (Herd.getInstance().isCalf(selection)) {
item1.setText("Add calf to herd");
item2.setText("Discard calf");
});
}
else {
item1.setText("Select for breeding");
item2.setText("Delete from herd");
item2.setText("Remove from herd");
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String question = "Delete "+Herd.getInstance().
String question = "Remove "+Herd.getInstance().
getCow(selection).getName()+" from herd?";
int confirm = JOptionPane.showConfirmDialog(null, question, "Delete cow?",
int confirm = JOptionPane.showConfirmDialog(null, question, "Remove animal?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(confirm == JOptionPane.YES_OPTION)
Herd.getInstance().removeCow(selection);