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 * FAIRER test? apples to apples! ButtonEventPlay3 does 4 *named* to compare to ButtonEventPlay2's 4 *unnamed* * AND -- anonymous wins: 9432 for ButtonEventPlay3 vs. 9356 for ButtonEventPlay2; BUT: shared non-anonymous listener better still (9100); what should be the conclusion here? [note: ButtonEventPlay4, which is 3 w/NO comments or blank lines, still 9432; ...what if all indentation gone? still 9432; [ButtonEventPlay5] PRETTY good evidence for the hypothesis that compilation into bytecode removes all comments indentation whitespace, so those do NOT hurt you here! I think;] @author Sharon Tuttle @version 2015-02-15 **/ public class ButtonEventPlay3 { /** 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 ButtonEventPlay3 buttonEventPlayInstance = new ButtonEventPlay3(); 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 EnterAction()); Button.ESCAPE.addButtonListener(new EscapeAction()); Button.LEFT.addButtonListener(new LeftAction()); Button.RIGHT.addButtonListener(new RightAction()); } /** private inner class for Enter button listener **/ private class EnterAction implements ButtonListener { /** when Enter pressed, clear the screen, and print "Enter PRESSED" on line 0 of the screen @param b the calling button **/ public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Enter PRESSED", 0, 0); } /** when Enter button is released, print "Enter RELEASED" on line 1 of the screen @param the calling button **/ public void buttonReleased(Button b) { LCD.drawString("Enter RELEASED", 0, 1); } } /** private inner class for Escape button listener **/ private class EscapeAction implements ButtonListener { /** when Escape pressed, clear the screen, and print "Escape PRESSED" on line 0 of the screen @param b the calling button **/ public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Escape PRESSED", 0, 0); } /** when Escape button is released, print "Escape RELEASED" on line 1 of the screen @param the calling button **/ public void buttonReleased(Button b) { LCD.drawString("Escape RELEASED", 0, 1); } } /** private inner class for Left button listener **/ private class LeftAction implements ButtonListener { /** when Left pressed, clear the screen, and print "Left PRESSED" on line 0 of the screen @param b the calling button **/ public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Left PRESSED", 0, 0); } /** when Left button is released, print "Left RELEASED" on line 1 of the screen @param the calling button **/ public void buttonReleased(Button b) { LCD.drawString("Left RELEASED", 0, 1); } } /** private inner class for Right button listener **/ private class RightAction implements ButtonListener { /** when Right pressed, clear the screen, and print "Right PRESSED" on line 0 of the screen @param b the calling button **/ public void buttonPressed(Button b) { LCD.clear(); LCD.drawString("Right PRESSED", 0, 0); } /** when Right button is released, print "Right RELEASED" on line 1 of the screen @param the calling button **/ public void buttonReleased(Button b) { LCD.drawString("Right RELEASED", 0, 1); } } }