/** * 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); } }