import lejos.nxt.*; import lejos.util.*; /** application class in which each button push causes an identifying string to be printed to the NXT screen @author Sharon Tuttle @version 2015-02-15 **/ public class ButtonEventPlay { /** 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 ButtonEventPlay buttonEventPlayInstance = new ButtonEventPlay(); 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 ButtonAction("Enter")); Button.ESCAPE.addButtonListener(new ButtonAction("Escape")); Button.LEFT.addButtonListener(new ButtonAction("Left")); Button.RIGHT.addButtonListener(new ButtonAction("Right")); } /** private inner class, to set up ButtonListeners (since implements ButtonListener, MUST implement methods buttonPressed and buttonReleased with the headers shown -- BUT OK for it to have OTHER methods/data in addition, here demo'd by its having a non-default constructor and a data field) **/ private class ButtonAction implements ButtonListener { // data field private String displayLabel; /** set the desired button label for this button listener @param desiredLabel the desired button display label **/ public ButtonAction(String desiredLabel) { this.displayLabel = desiredLabel; } /** when "sensitive" button is pressed, clear the screen, and print "[its label] PRESSED" on line 0 of the screen @param b the calling button **/ public void buttonPressed(Button b) { LCD.clear(); LCD.drawString(this.displayLabel + " PRESSED", 0, 0); } /** when "sensitive" button is released, print "[its label] RELEASED" on line 1 of the screen @param b the calling button **/ public void buttonReleased(Button b) { LCD.drawString(this.displayLabel + " RELEASED", 0, 1); } } }