import lejos.nxt.*; import lejos.util.*; /** application class in which each button push causes an identifying string to be printed to the NXT screen experiment: would anonymous classes result in "smaller" executable? * on 2015-02-15 test -- comparing to named SHARED-event-listener in ButtonEventPlay.java -- NO: 9356 on brick for this, 9100 on brick for ButtonEventPlay.java @author Sharon Tuttle @version 2015-02-15 **/ public class ButtonEventPlay2 { /** set up buttons to print an identifying string to the NXT screen when they are pressed, and run for about 30 seconds @param args not used **/ public static void main(String[] args) { System.out.println("Button Play"); System.out.println("RUNS 30 SEC"); System.out.println("click buttons!"); // creating an instance of this class, from which to // call the desired non-static method // from this static main method ButtonEventPlay2 buttonEventPlayInstance = new ButtonEventPlay2(); buttonEventPlayInstance.setUp(); // uh oh, how make this QUIT effectively? ...trying just // ending program after about 30 more seconds // (don't want a button push to end program THIS time!) Delay.msDelay(30000); } /** set up the buttons so they can identify themselves to the screen when pressed and released **/ private void setUp() { // set a button listener for each button // on the front of the NXT brick Button.ENTER.addButtonListener( new ButtonListener() { public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Enter PRESSED", 0, 0); } public void buttonReleased(Button b) { LCD.drawString("Enter RELEASED", 0, 1); } }); Button.ESCAPE.addButtonListener( new ButtonListener() { public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Escape PRESSED", 0, 0); } public void buttonReleased(Button b) { LCD.drawString("Escape RELEASED", 0, 1); } }); Button.LEFT.addButtonListener( new ButtonListener() { public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Left PRESSED", 0, 0); } public void buttonReleased(Button b) { LCD.drawString("Left RELEASED", 0, 1); } }); Button.RIGHT.addButtonListener( new ButtonListener() { public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Right PRESSED", 0, 0); } public void buttonReleased(Button b) { LCD.drawString("Right RELEASED", 0, 1); } }); } }