diff --git a/HelpWindow.java b/HelpWindow.java new file mode 100755 index 0000000..2f8213d --- /dev/null +++ b/HelpWindow.java @@ -0,0 +1,111 @@ +/** + * 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 a small window displaying the help text and license. + * (Adapted from Ecologia.) + */ + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import java.io.*; + + +public class HelpWindow extends JFrame +{ + JTextArea text; + JScrollPane scroller; + Box main_panel, button_panel; + JButton help, license; + + public HelpWindow() + { + this.setTitle("Help"); + this.setSize(580, 450); + this.setLocation(300, 150); + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + createGUI(); + loadDocFile("HELP"); + } + + /** + * Add the text area which will display the text and the buttons to choose + * which text to display. + */ + public void createGUI() + { + //Add the text area + main_panel = new Box(BoxLayout.Y_AXIS); + text = new JTextArea(); + text.setEditable(false); + text.setLineWrap(true); + text.setWrapStyleWord(true); + scroller = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); + main_panel.add(scroller); + main_panel.add(Box.createVerticalStrut(5)); + //Add the buttons + help = new JButton("Help"); + license = new JButton("License"); + help.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("HELP"); + } + }); + license.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("LICENSE"); + } + }); + button_panel = new Box(BoxLayout.X_AXIS); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(help); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(license); + button_panel.add(Box.createVerticalStrut(3)); + main_panel.add(button_panel); + this.add(main_panel, BorderLayout.CENTER); + //Add some fillers for the optics + this.add(new JPanel(), BorderLayout.NORTH); + this.add(new JPanel(), BorderLayout.EAST); + this.add(new JPanel(), BorderLayout.WEST); + this.add(new JPanel(), BorderLayout.SOUTH); + } + + /** + * Load a documentation file. + * @param String fileName + */ + public void loadDocFile(String filename) + { + String helptext = ""; + try { + InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(filename)); + BufferedReader helpfile_reader = new BufferedReader(isr); + String line = helpfile_reader.readLine(); + while (line != null) { + helptext = helptext+line+"\n"; + line = helpfile_reader.readLine(); + } + helpfile_reader.close(); + } + catch (IOException ioe) { + helptext = "Error loading file!"; + System.out.println("ERROR HelpWindow: could not load file '"+filename+"'!"); + } + text.setText(helptext); + text.setCaretPosition(0); + } +} diff --git a/HelpWindow.java b/HelpWindow.java new file mode 100755 index 0000000..2f8213d --- /dev/null +++ b/HelpWindow.java @@ -0,0 +1,111 @@ +/** + * 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 a small window displaying the help text and license. + * (Adapted from Ecologia.) + */ + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import java.io.*; + + +public class HelpWindow extends JFrame +{ + JTextArea text; + JScrollPane scroller; + Box main_panel, button_panel; + JButton help, license; + + public HelpWindow() + { + this.setTitle("Help"); + this.setSize(580, 450); + this.setLocation(300, 150); + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + createGUI(); + loadDocFile("HELP"); + } + + /** + * Add the text area which will display the text and the buttons to choose + * which text to display. + */ + public void createGUI() + { + //Add the text area + main_panel = new Box(BoxLayout.Y_AXIS); + text = new JTextArea(); + text.setEditable(false); + text.setLineWrap(true); + text.setWrapStyleWord(true); + scroller = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); + main_panel.add(scroller); + main_panel.add(Box.createVerticalStrut(5)); + //Add the buttons + help = new JButton("Help"); + license = new JButton("License"); + help.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("HELP"); + } + }); + license.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("LICENSE"); + } + }); + button_panel = new Box(BoxLayout.X_AXIS); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(help); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(license); + button_panel.add(Box.createVerticalStrut(3)); + main_panel.add(button_panel); + this.add(main_panel, BorderLayout.CENTER); + //Add some fillers for the optics + this.add(new JPanel(), BorderLayout.NORTH); + this.add(new JPanel(), BorderLayout.EAST); + this.add(new JPanel(), BorderLayout.WEST); + this.add(new JPanel(), BorderLayout.SOUTH); + } + + /** + * Load a documentation file. + * @param String fileName + */ + public void loadDocFile(String filename) + { + String helptext = ""; + try { + InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(filename)); + BufferedReader helpfile_reader = new BufferedReader(isr); + String line = helpfile_reader.readLine(); + while (line != null) { + helptext = helptext+line+"\n"; + line = helpfile_reader.readLine(); + } + helpfile_reader.close(); + } + catch (IOException ioe) { + helptext = "Error loading file!"; + System.out.println("ERROR HelpWindow: could not load file '"+filename+"'!"); + } + text.setText(helptext); + text.setCaretPosition(0); + } +} diff --git a/MooBreeder.java b/MooBreeder.java index 16dd73d..051a417 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -11,6 +11,8 @@ * 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.*; @@ -19,12 +21,14 @@ { private JMenuBar menubar; private JMenu fileMenu, helpMenu; - private JMenuItem reset, quit, help, about; + 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(); @@ -36,6 +40,7 @@ this.setTitle("MooBreeder"); this.setSize(800, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); + helpWindow = new HelpWindow(); this.createMenu(); this.createLayout(); //TODO @@ -47,11 +52,15 @@ 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 + //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) { @@ -61,6 +70,15 @@ 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?", @@ -79,6 +97,8 @@ "About", JOptionPane.INFORMATION_MESSAGE); } }); + fileMenu.add(save); + fileMenu.add(load); fileMenu.add(reset); fileMenu.add(quit); helpMenu.add(help); @@ -153,6 +173,8 @@ { //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; } @@ -165,8 +187,16 @@ public void mouseClicked(MouseEvent me) { //TODO - //getSource() + 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 diff --git a/HelpWindow.java b/HelpWindow.java new file mode 100755 index 0000000..2f8213d --- /dev/null +++ b/HelpWindow.java @@ -0,0 +1,111 @@ +/** + * 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 a small window displaying the help text and license. + * (Adapted from Ecologia.) + */ + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import java.io.*; + + +public class HelpWindow extends JFrame +{ + JTextArea text; + JScrollPane scroller; + Box main_panel, button_panel; + JButton help, license; + + public HelpWindow() + { + this.setTitle("Help"); + this.setSize(580, 450); + this.setLocation(300, 150); + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + createGUI(); + loadDocFile("HELP"); + } + + /** + * Add the text area which will display the text and the buttons to choose + * which text to display. + */ + public void createGUI() + { + //Add the text area + main_panel = new Box(BoxLayout.Y_AXIS); + text = new JTextArea(); + text.setEditable(false); + text.setLineWrap(true); + text.setWrapStyleWord(true); + scroller = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); + main_panel.add(scroller); + main_panel.add(Box.createVerticalStrut(5)); + //Add the buttons + help = new JButton("Help"); + license = new JButton("License"); + help.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("HELP"); + } + }); + license.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + loadDocFile("LICENSE"); + } + }); + button_panel = new Box(BoxLayout.X_AXIS); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(help); + button_panel.add(Box.createVerticalStrut(3)); + button_panel.add(license); + button_panel.add(Box.createVerticalStrut(3)); + main_panel.add(button_panel); + this.add(main_panel, BorderLayout.CENTER); + //Add some fillers for the optics + this.add(new JPanel(), BorderLayout.NORTH); + this.add(new JPanel(), BorderLayout.EAST); + this.add(new JPanel(), BorderLayout.WEST); + this.add(new JPanel(), BorderLayout.SOUTH); + } + + /** + * Load a documentation file. + * @param String fileName + */ + public void loadDocFile(String filename) + { + String helptext = ""; + try { + InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(filename)); + BufferedReader helpfile_reader = new BufferedReader(isr); + String line = helpfile_reader.readLine(); + while (line != null) { + helptext = helptext+line+"\n"; + line = helpfile_reader.readLine(); + } + helpfile_reader.close(); + } + catch (IOException ioe) { + helptext = "Error loading file!"; + System.out.println("ERROR HelpWindow: could not load file '"+filename+"'!"); + } + text.setText(helptext); + text.setCaretPosition(0); + } +} diff --git a/MooBreeder.java b/MooBreeder.java index 16dd73d..051a417 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -11,6 +11,8 @@ * 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.*; @@ -19,12 +21,14 @@ { private JMenuBar menubar; private JMenu fileMenu, helpMenu; - private JMenuItem reset, quit, help, about; + 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(); @@ -36,6 +40,7 @@ this.setTitle("MooBreeder"); this.setSize(800, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); + helpWindow = new HelpWindow(); this.createMenu(); this.createLayout(); //TODO @@ -47,11 +52,15 @@ 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 + //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) { @@ -61,6 +70,15 @@ 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?", @@ -79,6 +97,8 @@ "About", JOptionPane.INFORMATION_MESSAGE); } }); + fileMenu.add(save); + fileMenu.add(load); fileMenu.add(reset); fileMenu.add(quit); helpMenu.add(help); @@ -153,6 +173,8 @@ { //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; } @@ -165,8 +187,16 @@ public void mouseClicked(MouseEvent me) { //TODO - //getSource() + 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 diff --git a/run.sh b/run.sh index 95a12a0..2035cc0 100755 --- a/run.sh +++ b/run.sh @@ -2,6 +2,6 @@ # Compile, package and run MooBreeder javac *.java -jar cfe MooBreeder.jar MooBreeder *.class +jar cfe MooBreeder.jar MooBreeder LICENSE HELP *.class rm *.class java -jar MooBreeder.jar