Newer
Older
moobreeder / MooBreeder.java
/**
 * Moobreeder is a simple cow breeding game to illustrate the principles of
 * selective breeding to KS3 pupils.
 *
 * (c) Daniel Vedder 2018, Amano Christian School
 * Licensed under the terms of the MIT license.
 * https://git.synoikos.de/daniel/moobreeder
 */

/*
 * This is the main class with the GUI.
 */

//TODO Set breeding goal at beginning of game (dairy, meat, plowing)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MooBreeder extends JFrame implements MouseListener
{
	private JMenuBar menubar;
	private JMenu fileMenu, helpMenu;
	private JMenuItem save, load, reset, quit, help, about;
	private JPanel breedPanel;
	private JScrollPane herdScroller;
	private Box herdBox, motherBox, fatherBox, calf1Box, calf2Box;
	private JButton add, breed, keep, remove;

	private HelpWindow helpWindow;

	public static void main(String[] args)
	{
		Herd.getInstance();
		MooBreeder mb = new MooBreeder();
	}
	
	public MooBreeder()
	{
		this.setTitle("MooBreeder");
		this.setSize(800, 400);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		helpWindow = new HelpWindow();
		this.createMenu();
		this.createLayout();
		//TODO
		this.setVisible(true);
	}

	private void createMenu()
	{
		menubar = new JMenuBar();
		fileMenu = new JMenu("File");
		helpMenu = new JMenu("Help");
		save = new JMenuItem("Save");
		load = new JMenuItem("Load");
		reset = new JMenuItem("Reset");
		quit = new JMenuItem("Exit");
		help = new JMenuItem("Help");
		about = new JMenuItem("About");
		//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()
        {
			public void actionPerformed(ActionEvent e) {
				int restart = JOptionPane.showConfirmDialog(null, "Reset the game?", "Reset?",
															JOptionPane.OK_CANCEL_OPTION,
															JOptionPane.QUESTION_MESSAGE);
				if (restart == JOptionPane.OK_OPTION) resetGame();
			}
		});
		reset.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
		help.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
              helpWindow.setVisible(true);
          }
        });
		help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int confirm = JOptionPane.showConfirmDialog(null, "Quit MooBreeder?", "Quit?",
															JOptionPane.YES_NO_OPTION,
															JOptionPane.WARNING_MESSAGE);
				if(confirm == JOptionPane.YES_OPTION) System.exit(0);
			}
		});
		quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
		about.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "MooBreeder - an educational breeding game"+
											  "\n\n(c) 2018 Daniel Vedder"+
											  ", Amano Christian School"+
											  "\nhttps://git.synoikos.de/daniel/moobreeder",
											  "About", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		fileMenu.add(save);
		fileMenu.add(load);
		fileMenu.add(reset);
		fileMenu.add(quit);
		helpMenu.add(help);
		helpMenu.add(about);
		menubar.add(fileMenu);
		menubar.add(helpMenu);
		this.setJMenuBar(menubar);
	}

	private void createLayout()
	{
		//TODO
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
		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);
		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);
		calf2Box = new Box(BoxLayout.Y_AXIS);
		breedPanel.add(fatherBox);
		breedPanel.add(motherBox);
		breedPanel.add(calf1Box);
		breedPanel.add(calf2Box);
		this.add(breedPanel);
		updateGUI();
	}

	private void updateGUI()
	{
		updateHerdBox();
		updateParentBox();
		updateOffspringBox();
	}

	private void updateHerdBox()
	{
		//TODO
		JLabel label = new JLabel("Your current herd:");
		herdBox.add(label);
	}

	private void updateParentBox()
	{
		//TODO
		JLabel dl = new JLabel("Father");
		JLabel ml = new JLabel("Mother");
		fatherBox.add(dl);
		motherBox.add(ml);
	}


	private void updateOffspringBox()
	{
		//TODO
		JLabel l1 = new JLabel("Calf 1");
		JLabel l2 = new JLabel("Calf 2");
		calf1Box.add(l1);
		calf2Box.add(l2);
	}

	private void resetGame()
	{
		Herd.getInstance().resetHerd();
		updateGUI();
	}

	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
		cowBox.addMouseListener(this);
		return cowBox;
	}

	/*
	 * MouseListener methods
	 */

	@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)
		updateGUI();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}
	
}