Page 161 - Hacking Roomba
P. 161
142 Part II — Fun Things to Do
MyGUI Processing Library
Besides keyboard control, it would be nice to also have GUI buttons that the user could click
with the mouse. Using the standard Java GUI buttons is not easy in Processing. It can be done
but you have to figure out how to step around the built-in Processing event loop and AWT-
based graphics. There have been a few simplified GUI toolkits created for use in Processing.
The one used in RoombaView is called MyGUI, written by Markavian and available from the
Processing library page at http://processing.org/reference/libraries/. MyGUI
uses Processing for all of its drawing and event handling.
MyGUI registers with Processing at a level not available to sketches. Because of this, MyGUI
controls always draw on top of any sketch graphics.
Listing 7-4 shows MyGUI in use and Figure 7-4 shows the result for the movement buttons.
Listing 7-4: Making Graphical Buttons with MyGUI
void makeMoveButtons(int posx, int posy) {
MyGUIGroup buttonGroup = new MyGUIGroup(this, posx, posy);
buttonGroup.setStyle(new MyGUIStyle(this, cBut));
PImage forward = loadImage(“but_forward.tif”);
PImage backward = loadImage(“but_backward.tif”);
PImage spinleft = loadImage(“but_spinleft.tif”);
PImage spinright = loadImage(“but_spinright.tif”);
PImage stopit = loadImage(“but_stop.tif”);
PImage turnFL = loadImage(“but_turnleft.tif”);
PImage turnFR = loadImage(“but_turnright.tif”);
butForward = new MyGUIButton(this, 0,-45, forward);
butBackward = new MyGUIButton(this, 0, 45, backward);
butSpinLeft = new MyGUIButton(this,-45, 0, spinleft);
butSpinRight = new MyGUIButton(this, 45, 0, spinright);
butStop = new MyGUIButton(this, 0, 0, stopit);
butTurnLeft = new MyGUIButton(this,-45,-45, turnFL);
butTurnRight = new MyGUIButton(this, 45,-45, turnFR);
sliderSpeed = new MyGUIPinSlider(this, 0,80, 100,20, 0,500);
sliderSpeed.setValue(200);
sliderSpeed.setActionCommand(“speed-update”);
gui.add(buttonGroup);
buttonGroup.add(butForward );
buttonGroup.add(butBackward);
buttonGroup.add(butSpinLeft);
buttonGroup.add(butSpinRight);
buttonGroup.add(butStop);
buttonGroup.add(butTurnLeft);
buttonGroup.add(butTurnRight);