/** * Title: Ball World

* Description:

* Copyright: Copyright (c) DXN, SBW

* Company: Rice University

* @author DXN * @version 1.0 */ package ballworld; import java.awt.*; import javax.swing.*; import java.awt.event.*; import command.*; /** * A subclass of JFrame containing the various GUI components specified in the program behavior specification. * Provides a Container area where the drawing object is to be drawn. */ public class BallGUI2 extends JFrame { private JPanel _controlPnl = new JPanel(); private ILambda _paintCmd = NoOpLambda.Singleton; private JPanel _canvasPnl = new JPanel() { public void paint(Graphics g) { super.paint(g); _paintCmd.apply(g); } }; private JButton _clearAllBtn = new JButton("ClearAll"); private JTextField _inputTF = new JTextField("Straight"); private JPanel _inputPnl = new JPanel(); private JButton _makeSwitcherBtn = new JButton("Make Switcher"); private JButton _switchBtn = new JButton("Switch!"); private JPanel _switcherPnl = new JPanel(); private JPanel _cbPnl = new JPanel(); private JComboBox _list1DL = new JComboBox(); private JComboBox _list2DL = new JComboBox(); private JButton _combineBtn = new JButton("Combine!"); private JButton _addBtn = new JButton("Add to lists"); private JButton _makeSelectedBtn = new JButton("Make Selected Ball"); /** * Initializes and lays out the GUI components. */ public BallGUI2(final ILambda makeBallCmd, final ILambda clearBallsCmd, ILambda paintCmd, final ILambda makeSwitcherCmd, final ILambda switchCmd, final ILambda addCmd, final ILambda combineCmd) { _paintCmd = paintCmd; try { init(); } catch(Exception e) { e.printStackTrace(); } setSize (new Dimension(600, 450)); _clearAllBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { clearBallsCmd.apply(null); } }); _makeSwitcherBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { makeSwitcherCmd.apply(null); } }); _switchBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { switchCmd.apply(_list1DL.getSelectedItem()); } }); _addBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { Object o =addCmd.apply(_inputTF.getText()); if(null==o) return; _list1DL.insertItemAt(o,0); _list2DL.insertItemAt(o,0); } }); _combineBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { Object o =combineCmd.apply(new Object[]{_list1DL.getSelectedItem(),_list2DL.getSelectedItem()}); if(null==o) return; _list1DL.insertItemAt(o,0); _list2DL.insertItemAt(o,0); } }); _makeSelectedBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { makeBallCmd.apply(_list1DL.getSelectedItem()); } }); } private void init() throws Exception { this.setTitle("Ball World"); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(_controlPnl, BorderLayout.NORTH); //_canvasPnl.setBackground(Color.BLACK); this.getContentPane().add(_canvasPnl, BorderLayout.CENTER); _inputPnl.setLayout(new GridLayout(0,1)); _controlPnl.add(_inputPnl); _inputPnl.add(_inputTF); _inputTF.setToolTipText("Name of strategy,XXX, from ballworld.XXXStrategy."); _inputPnl.add(_addBtn); _addBtn.setToolTipText("Add strategy to both droplists. The rest of the of classname is added automatically."); _cbPnl.setLayout(new GridLayout(0,1)); _controlPnl.add(_cbPnl); _cbPnl.add(_makeSelectedBtn); _makeSelectedBtn.setToolTipText("Instantiate a Ball with the strategy selected in the upper droplist."); _cbPnl.add(_list1DL); _cbPnl.add(_list2DL); _cbPnl.add(_combineBtn); _combineBtn.setToolTipText("Combine the selected strategies from both droplists into a single strategy that is added to both droplists."); _switcherPnl.setLayout(new GridLayout(0,1)); _controlPnl.add(_switcherPnl); _switcherPnl.add(_makeSwitcherBtn); _makeSwitcherBtn.setToolTipText("Instantiate a ball that can switch strategies."); _switcherPnl.add(_switchBtn); _switchBtn.setToolTipText("Switch the strategy on all switcher balls to the currently selected strategy in the upper droplist."); _controlPnl.add(_clearAllBtn); _clearAllBtn.setToolTipText("Clear all the balls from the screen."); } //Overridden so we can exit on System Close protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if(e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } /** * Accessor method for the panel upon which the balls are to be drawn */ public Container getContainer() { return _canvasPnl; } }