/* * BAGSGui.bsh * * Copyright 2005 M. Aaron Wadley * * This file is part of BAGS (Beany Adventure Game System). * BAGS (Beany Adventure Game System) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * BAGS (Beany Adventure Game System) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with BAGS (Beany Adventure Game System); if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** * Graphical user interface */ Gui(gameTitle) { inputPrompt=">"; defaultFontSize=12; fontSize=12; outputBackground=Color.black; outputForeground=Color.white; inputBackground=outputBackground; inputForeground=outputForeground; JPanel mainPanel = new JPanel(); mainPanel.setLayout (new BorderLayout()); BAGSBoxCaret caret = new BAGSBoxCaret(); caret.setBlinkRate(300); JTextField commandInput = new JTextField(20); commandInput.setCaret(caret); JTextPane displayOutput = new JTextPane(); displayOutput.setPreferredSize(new Dimension(640, 480)); displayOutput.setEditable(false); displayOutput.addFocusListener(this); JPanel statusPanel = new JPanel(); statusPanel.setPreferredSize(new Dimension(640, 20)); JScrollPane paneScrollPane = new JScrollPane(displayOutput); paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); paneScrollPane.setPreferredSize(new Dimension(640, 480)); paneScrollPane.setMinimumSize(new Dimension(10, 10)); JLabel prompt = new JLabel(inputPrompt); JPanel inputJPanel = new JPanel(); inputJPanel.setLayout(new BorderLayout()); // Add components to our layout / panels inputJPanel.add("West", prompt); inputJPanel.add("Center", commandInput); mainPanel.add("North", statusPanel); mainPanel.add("Center", new JScrollPane(displayOutput, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER)); mainPanel.add("South", inputJPanel); /** * Update the colors */ updateColors () { prompt.background = inputBackground; prompt.foreground = inputForeground; commandInput.background = inputBackground; commandInput.foreground = inputForeground; commandInput.setCaretColor(inputForeground); inputJPanel.foreground = inputForeground; inputJPanel.background = inputBackground; displayOutput.background = outputBackground; displayOutput.foreground = outputForeground; mainPanel.foreground = outputForeground; mainPanel.background = outputBackground; statusPanel.foreground = outputForeground; statusPanel.background = outputBackground; } updateBackground(newBg) { outputBackground = newBg; inputBackground = newBg; updateColors(); } updateForeground(newFg) { outputForeground = newFg; inputForeground = newFg; updateColors(); } /** * Update the colors with new colors */ updateColors(newFg, newBg) { outputForeground = newFg; outputBackground = newBg; inputForeground = newFg; inputBackground = newBg; updateColors(); } /** * Focus handler */ focusGained (e) { commandInput.requestFocusInWindow(); } /** * Focus handler */ focusLost(e) { // don't care! } /** * Add an action listener to the command text field */ addCommandActionListener(commandListener) { commandInput.addActionListener(commandListener); } addKeyListener(keyListener) { commandInput.addKeyListener(keyListener); } removeKeyListener(keyListener) { commandInput.removeKeyListener(keyListener); } // Set up options like font and color updateColors(Color.white, Color.black); theFrame = frame(mainPanel); theFrame.setTitle(gameTitle); return this; }