package class8_1_RemoteControl_lambda_start; import class8_1_RemoteControl_lambda_start.other.Command; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.Color; import java.awt.Container; /** * This class controls the operation of an automobile * @author hornick, yoder */ public class Car extends JFrame { // Currently used only for positioning the windows. static int instanceCount = 0; JLabel status = new JLabel(); /** * @param title name of car */ public Car(String title) { setTitle(title); setSize(200, 80); setLocation(100, 100+80*instanceCount++); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Retrieves the window's contentPane and sets the background color Container contentPane = getContentPane(); JPanel jp = new JPanel(); contentPane.add(jp); jp.setBackground(Color.LIGHT_GRAY); status.setText("engine off"); jp.add(status); contentPane.validate(); contentPane.repaint(); } /** * Simulate the command that starts the engine */ public void startEngine() { status.setText("engine on"); } /** * Returns an instance of an anonymous inner class * for starting this car. * @return command to start */ public Command getStartCommand() { // Exercise: Rewrite this command as a lambda expression. return new Command() { @Override public boolean execute() { startEngine(); return true; } }; } /** * Simulate the command that shuts the engine off */ public void stopEngine() { status.setText("engine off"); } /** * @return a command to stop this car */ public Command getStopCommand() { return new Command() { @Override public boolean execute() { stopEngine(); return true; } }; } // Any number of other commands may be implemented here }