package class8_1_RemoteControl_lambda_start; import class8_1_RemoteControl_lambda_start.other.Command; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; /** * The remote control unit which controls the world * @author hornick, yoder * */ public class ControlPanelUI extends JFrame { private ControlPanelListener eventListener; // UI event listener private Robot robot; private Car car; private Car car2; private JTextField result; private Map map; private Deque stack = new LinkedList(); public ControlPanelUI( Car car, Car car2, Robot robot ) { this.robot = robot; this.car = car; this.car2 = car2; // create the containing window and set its size etc setTitle("X10 Controller"); setSize(200, 300); setLocation(400, 400); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); eventListener = new ControlPanelListener(); // Retrieves the window's contentPane and sets the background color Container contentPane = getContentPane(); contentPane.setBackground(Color.BLACK); JPanel jp = new JPanel(); FlowLayout fl = new FlowLayout(); jp.setLayout(fl); contentPane.add(jp); // create the command buttons ArrayList commands = new ArrayList(); map = new TreeMap<>(); map.put("Tidy", robot.getTidyCommand()); map.put("Scrub", robot.getScrubCommand()); map.put("Start Car 1", car.getStartCommand()); map.put("Stop Car 1", car.getStopCommand()); map.put("Start Car 2", car2.getStartCommand()); map.put("Stop Car 2",car2.getStopCommand()); // What is the Big-O runtime of the iterator used for this loop? for( Map.Entry entry: map.entrySet()) { JButton cb = new JButton(entry.getKey()); cb.setPreferredSize(new Dimension(150, 25)); cb.setActionCommand(entry.getKey()); jp.add(cb); cb.addActionListener(eventListener); //map.put(entry.getKey(), entry.getValue()); } result = new JTextField(""); result.setPreferredSize(new Dimension(200, 25)); jp.add(result); contentPane.validate(); contentPane.repaint(); } /** * handle the invocation of various commands * @param action The action command / text on the button that was pressed */ private void handleCommand( String action ) { // What is the Big-O runtime of this // method-call? Command command = map.get(action); if(null == command) { System.out.println("Errro!!!!"); } boolean status; status = command.execute(); if(status) { stack.push(command); } if(status) { result.setText("command OK"); } else { result.setText("command failed"); } } /** * This is a nested inner class that handles events * for the ControlPanel * @author hornick */ private class ControlPanelListener implements ActionListener { int myThing; /** * Constructor; does nothing useful */ private ControlPanelListener() { } /** * ActionListener event handler for this class * @param event - the ActionEvent object that caused the event */ public void actionPerformed(ActionEvent event) { String action = event.getActionCommand(); handleCommand(action); } } // end of ControlPanel inner class }