Skip to content

Instantly share code, notes, and snippets.

@koduki
Created July 27, 2008 10:54
Show Gist options
  • Save koduki/2770 to your computer and use it in GitHub Desktop.
Save koduki/2770 to your computer and use it in GitHub Desktop.
import javax.swing.*;
import wiiremotej.*;
import wiiremotej.event.*;
import javax.sound.sampled.*;
import java.io.*;
public class WiiSample1 {
private static JFrame frame;
final private static JLabel lblX = new JLabel("0");
final private static JLabel lblY = new JLabel("0");
final private static JLabel lblZ = new JLabel("0");
public static void main(String args[]) throws Exception {
frame = buildGUI();
initWiiRemote();
}
private static JFrame buildGUI() {
return new JFrame() {{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Accelerometer graph: Wii Remote");
setSize(320, 240);
add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JItem("XAcceleration", lblX));
add(new JItem("YAcceleration", lblY));
add(new JItem("ZAcceleration", lblZ));
}});
setVisible(true);
}};
}
private static void initWiiRemote() throws InterruptedException, IOException, UnsupportedAudioFileException {
// basic console logging options...
WiiRemoteJ.setConsoleLoggingAll();
// Find and connect to a Wii Remote
WiiRemote remote = WiiRemoteJ.findRemote();
remote.addWiiRemoteListener(simpleEventLisntener);
remote.setAccelerometerEnabled(true);
remote.setSpeakerEnabled(true);
remote.setIRSensorEnabled(true, WRIREvent.BASIC);
remote.setLEDIlluminated(0, true);
remote.getButtonMaps().add(
new ButtonMap(WRButtonEvent.HOME, ButtonMap.NUNCHUK,
WRNunchukExtensionEvent.C,
new int[] { java.awt.event.KeyEvent.VK_CONTROL },
java.awt.event.InputEvent.BUTTON1_MASK, 0, -1));
final WiiRemote remoteF = remote;
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
remoteF.disconnect();
}
}));
}
private static WiiRemoteListener simpleEventLisntener = new WiiRemoteAdapter() {
private boolean accelerometerSource = true; // true = wii remote,
public void disconnected() {
System.out.println("Remote disconnected... Please Wii again.");
System.exit(0);
}
public void accelerationInputReceived(WRAccelerationEvent evt) {
if (accelerometerSource) {
lblX.setText( String.valueOf( evt.getXAcceleration()) );
lblY.setText( String.valueOf( evt.getYAcceleration()) );
lblZ.setText( String.valueOf( evt.getZAcceleration()) );
}
}
public void buttonInputReceived(WRButtonEvent evt) {
if (evt.wasPressed(WRButtonEvent.TWO)) System.out.println("2");
if (evt.wasPressed(WRButtonEvent.ONE)) System.out.println("1");
if (evt.wasPressed(WRButtonEvent.B)) System.out.println("B");
if (evt.wasPressed(WRButtonEvent.A)) System.out.println("A");
if (evt.wasPressed(WRButtonEvent.MINUS)) System.out.println("Minus");
if (evt.wasPressed(WRButtonEvent.HOME)) System.out.println("Home");
if (evt.wasPressed(WRButtonEvent.LEFT)) System.out.println("Left");
if (evt.wasPressed(WRButtonEvent.RIGHT)) System.out.println("Right");
if (evt.wasPressed(WRButtonEvent.DOWN)) System.out.println("Down");
if (evt.wasPressed(WRButtonEvent.UP)) System.out.println("Up");
if (evt.wasPressed(WRButtonEvent.PLUS)) System.out.println("Plus");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment