Guest User

Untitled

a guest
Feb 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. import wiiremotej.*;
  5. import wiiremotej.event.*;
  6.  
  7. /**
  8. * WiiRemoteJ Nunchuk Excample.
  9. * this source base WiiRemoteJExample(WRLmpl.java)
  10. */
  11.  
  12. public class WRLImplNunchuk extends WiiRemoteAdapter {
  13. private static boolean accelerometerSource = false; // true = wii remote,
  14. // false = nunchuk
  15. private static boolean lastSource = true;
  16.  
  17. private WiiRemote remote;
  18. private static JFrame graphFrame;
  19. private static JPanel graph;
  20. private static int t = 0;
  21. private static int x = 0;
  22. private static int y = 0;
  23. private static int z = 0;
  24.  
  25. private static int x2 = 0;
  26. private static int y2 = 0;
  27. private static int z2 = 0;
  28.  
  29. private static int lastX = 0;
  30. private static int lastY = 0;
  31. private static int lastZ = 0;
  32.  
  33. private static int lastX2 = 0;
  34. private static int lastY2 = 0;
  35. private static int lastZ2 = 0;
  36.  
  37. public static void main(String args[]) {
  38. // basic console logging options...
  39. WiiRemoteJ.setConsoleLoggingAll();
  40. // WiiRemoteJ.setConsoleLoggingOff();
  41.  
  42. try {
  43. graphFrame = new JFrame();
  44. graphFrame.setTitle("Accelerometer graph: Wii Remote");
  45. graphFrame.setSize(800, 600);
  46. graphFrame.setResizable(false);
  47.  
  48. t = 801;
  49. graph = new JPanel() {
  50. private void drawLine(Graphics g, Color color, int t, int last, int v){
  51. g.setColor(color);
  52. g.drawLine(t, last, t, v);
  53. }
  54. public void paintComponent(Graphics graphics) {
  55. if (t >= 800 || accelerometerSource != lastSource) {
  56. t = 0;
  57. lastSource = accelerometerSource;
  58. graphics.clearRect(0, 0, 800, 600);
  59. graphics.fillRect(0, 0, 800, 600);
  60. graphics.setColor(Color.WHITE);
  61. graphics.drawLine(0, 300, 800, 300);
  62. }
  63.  
  64. drawLine(graphics, Color.RED, t, lastX, x);
  65. drawLine(graphics, Color.GREEN, t, lastY, y);
  66. drawLine(graphics, Color.BLUE, t, lastZ, z);
  67.  
  68. drawLine(graphics, Color.YELLOW, t, lastX2, x2);
  69. drawLine(graphics, Color.WHITE, t, lastY2, y2);
  70. drawLine(graphics, Color.PINK, t, lastZ2, z2);
  71. }
  72. };
  73. graphFrame.add(graph);
  74. graphFrame.setVisible(true);
  75.  
  76. // Find and connect to a Wii Remote
  77. WiiRemote remote = WiiRemoteJ.findRemote();
  78. remote.addWiiRemoteListener(new WRLImplNunchuk(remote));
  79. remote.setAccelerometerEnabled(true);
  80. // remote.setIRSensorEnabled(true, WRIREvent.BASIC); // Nunchukと同時には使えない
  81.  
  82. remote.getButtonMaps().add(
  83. new ButtonMap(WRButtonEvent.HOME, ButtonMap.NUNCHUK,
  84. WRNunchukExtensionEvent.C,
  85. new int[] { java.awt.event.KeyEvent.VK_CONTROL },
  86. java.awt.event.InputEvent.BUTTON1_MASK, 0, -1));
  87.  
  88. final WiiRemote remoteF = remote;
  89. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  90. public void run() {
  91. remoteF.disconnect();
  92. }
  93. }));
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99. public WRLImplNunchuk(WiiRemote remote) {
  100. this.remote = remote;
  101. }
  102.  
  103. public void disconnected() {
  104. System.out.println("Remote disconnected... Please Wii again.");
  105. System.exit(0);
  106. }
  107.  
  108. public void accelerationInputReceived(WRAccelerationEvent evt) {
  109.  
  110. lastX = x;
  111. lastY = y;
  112. lastZ = z;
  113.  
  114. x = (int) (evt.getXAcceleration() / 5 * 300) + 300;
  115. y = (int) (evt.getYAcceleration() / 5 * 300) + 300;
  116. z = (int) (evt.getZAcceleration() / 5 * 300) + 300;
  117.  
  118. t++;
  119.  
  120. // graph.repaint();
  121.  
  122. }
  123.  
  124. public void extensionInputReceived(WRExtensionEvent evt) {
  125.  
  126. if (evt instanceof WRNunchukExtensionEvent) {
  127. WRNunchukExtensionEvent NEvt = (WRNunchukExtensionEvent) evt;
  128.  
  129. if (!accelerometerSource) {
  130. WRAccelerationEvent AEvt = NEvt.getAcceleration();
  131. lastX2 = x2;
  132. lastY2 = y2;
  133. lastZ2 = z2;
  134.  
  135. x2 = (int) (AEvt.getXAcceleration() / 5 * 300) + 300;
  136. y2 = (int) (AEvt.getYAcceleration() / 5 * 300) + 300;
  137. z2 = (int) (AEvt.getZAcceleration() / 5 * 300) + 300;
  138.  
  139. t++;
  140.  
  141. graph.repaint();
  142. }
  143.  
  144. AnalogStickData stick = NEvt.getAnalogStickData();
  145. System.out.println("stick angle : " + stick.getAngle());
  146. System.out.println("stick x : " + stick.getX());
  147. System.out.println("stick y : " + stick.getY());
  148.  
  149. if (NEvt.wasReleased(WRNunchukExtensionEvent.C)) System.out.println("C");
  150. if (NEvt.wasPressed(WRNunchukExtensionEvent.Z)) System.out.println("Z");
  151. if (NEvt.wasPressed(WRNunchukExtensionEvent.Z)) System.out.println("Z");
  152. }
  153. }
  154.  
  155. public void extensionConnected(WiiRemoteExtension extension) {
  156. System.out.println("Extension connected!");
  157. try {
  158. remote.setExtensionEnabled(true);
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. }
  162. }
  163.  
  164. public void extensionPartiallyInserted() {
  165. System.out.println("Extension partially inserted. Push it in more next time, jerk!");
  166. }
  167.  
  168. public void extensionUnknown() {
  169. System.out.println("Extension unknown. Did you try to plug in a toaster or something?");
  170. }
  171.  
  172. public void extensionDisconnected(WiiRemoteExtension extension) {
  173. System.out.println("Extension disconnected. Why'd you unplug it, retard?");
  174. }
  175.  
  176. public void buttonInputReceived(WRButtonEvent evt) {
  177. if (evt.wasPressed(WRButtonEvent.TWO)) System.out.println("2");
  178. if (evt.wasPressed(WRButtonEvent.ONE)) System.out.println("1");
  179. if (evt.wasPressed(WRButtonEvent.B)) System.out.println("B");
  180. if (evt.wasPressed(WRButtonEvent.A)) System.out.println("A");
  181. if (evt.wasPressed(WRButtonEvent.MINUS)) System.out.println("Minus");
  182. if (evt.wasPressed(WRButtonEvent.HOME))System.out.println("Home");
  183. if (evt.wasPressed(WRButtonEvent.LEFT))System.out.println("Left");
  184. if (evt.wasPressed(WRButtonEvent.RIGHT))System.out.println("Right");
  185. if (evt.wasPressed(WRButtonEvent.DOWN))System.out.println("Down");
  186. if (evt.wasPressed(WRButtonEvent.UP))System.out.println("Up");
  187. if (evt.wasPressed(WRButtonEvent.PLUS))System.out.println("Plus");
  188. }
  189.  
  190. }
Add Comment
Please, Sign In to add comment