Skip to content

Instantly share code, notes, and snippets.

@koduki
Created October 15, 2008 15:16
Show Gist options
  • Save koduki/16928 to your computer and use it in GitHub Desktop.
Save koduki/16928 to your computer and use it in GitHub Desktop.
import java.awt.*;
import javax.swing.*;
import wiiremotej.*;
import wiiremotej.event.*;
/**
* WiiRemoteJ Nunchuk Excample.
* this source base WiiRemoteJExample(WRLmpl.java)
*/
public class WRLImplNunchuk extends WiiRemoteAdapter {
private static boolean accelerometerSource = false; // true = wii remote,
// false = nunchuk
private static boolean lastSource = true;
private WiiRemote remote;
private static JFrame graphFrame;
private static JPanel graph;
private static int t = 0;
private static int x = 0;
private static int y = 0;
private static int z = 0;
private static int x2 = 0;
private static int y2 = 0;
private static int z2 = 0;
private static int lastX = 0;
private static int lastY = 0;
private static int lastZ = 0;
private static int lastX2 = 0;
private static int lastY2 = 0;
private static int lastZ2 = 0;
public static void main(String args[]) {
// basic console logging options...
WiiRemoteJ.setConsoleLoggingAll();
// WiiRemoteJ.setConsoleLoggingOff();
try {
graphFrame = new JFrame();
graphFrame.setTitle("Accelerometer graph: Wii Remote");
graphFrame.setSize(800, 600);
graphFrame.setResizable(false);
t = 801;
graph = new JPanel() {
private void drawLine(Graphics g, Color color, int t, int last, int v){
g.setColor(color);
g.drawLine(t, last, t, v);
}
public void paintComponent(Graphics graphics) {
if (t >= 800 || accelerometerSource != lastSource) {
t = 0;
lastSource = accelerometerSource;
graphics.clearRect(0, 0, 800, 600);
graphics.fillRect(0, 0, 800, 600);
graphics.setColor(Color.WHITE);
graphics.drawLine(0, 300, 800, 300);
}
drawLine(graphics, Color.RED, t, lastX, x);
drawLine(graphics, Color.GREEN, t, lastY, y);
drawLine(graphics, Color.BLUE, t, lastZ, z);
drawLine(graphics, Color.YELLOW, t, lastX2, x2);
drawLine(graphics, Color.WHITE, t, lastY2, y2);
drawLine(graphics, Color.PINK, t, lastZ2, z2);
}
};
graphFrame.add(graph);
graphFrame.setVisible(true);
// Find and connect to a Wii Remote
WiiRemote remote = WiiRemoteJ.findRemote();
remote.addWiiRemoteListener(new WRLImplNunchuk(remote));
remote.setAccelerometerEnabled(true);
// remote.setIRSensorEnabled(true, WRIREvent.BASIC); // Nunchukと同時には使えない
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();
}
}));
} catch (Exception e) {
e.printStackTrace();
}
}
public WRLImplNunchuk(WiiRemote remote) {
this.remote = remote;
}
public void disconnected() {
System.out.println("Remote disconnected... Please Wii again.");
System.exit(0);
}
public void accelerationInputReceived(WRAccelerationEvent evt) {
lastX = x;
lastY = y;
lastZ = z;
x = (int) (evt.getXAcceleration() / 5 * 300) + 300;
y = (int) (evt.getYAcceleration() / 5 * 300) + 300;
z = (int) (evt.getZAcceleration() / 5 * 300) + 300;
t++;
// graph.repaint();
}
public void extensionInputReceived(WRExtensionEvent evt) {
if (evt instanceof WRNunchukExtensionEvent) {
WRNunchukExtensionEvent NEvt = (WRNunchukExtensionEvent) evt;
if (!accelerometerSource) {
WRAccelerationEvent AEvt = NEvt.getAcceleration();
lastX2 = x2;
lastY2 = y2;
lastZ2 = z2;
x2 = (int) (AEvt.getXAcceleration() / 5 * 300) + 300;
y2 = (int) (AEvt.getYAcceleration() / 5 * 300) + 300;
z2 = (int) (AEvt.getZAcceleration() / 5 * 300) + 300;
t++;
graph.repaint();
}
AnalogStickData stick = NEvt.getAnalogStickData();
System.out.println("stick angle : " + stick.getAngle());
System.out.println("stick x : " + stick.getX());
System.out.println("stick y : " + stick.getY());
if (NEvt.wasReleased(WRNunchukExtensionEvent.C)) System.out.println("C");
if (NEvt.wasPressed(WRNunchukExtensionEvent.Z)) System.out.println("Z");
if (NEvt.wasPressed(WRNunchukExtensionEvent.Z)) System.out.println("Z");
}
}
public void extensionConnected(WiiRemoteExtension extension) {
System.out.println("Extension connected!");
try {
remote.setExtensionEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void extensionPartiallyInserted() {
System.out.println("Extension partially inserted. Push it in more next time, jerk!");
}
public void extensionUnknown() {
System.out.println("Extension unknown. Did you try to plug in a toaster or something?");
}
public void extensionDisconnected(WiiRemoteExtension extension) {
System.out.println("Extension disconnected. Why'd you unplug it, retard?");
}
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