?? wiiremotesampleapplication.java
字號(hào):
//===========================================================================//= Example based on WiiRemoteJ.jar example "WRLImpl.java", by =//= Michael Diamond (version 1/05/07) posted on june 26/2007 at =//= http://www.wiili.org/forum/wiiremotej-v10-released-t2357.html =//= Modified by Oscar Xavier Chavarro - October 9 / 2007. =//===========================================================================// Java basic classesimport java.io.DataOutputStream;import java.io.FileOutputStream;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.File;// Java GUI classesimport java.util.ArrayList;import java.awt.Color;import java.awt.Graphics;import java.awt.FlowLayout;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JFrame;// Java multimedia classesimport javax.sound.sampled.AudioSystem;import javax.sound.sampled.AudioInputStream;// WiiRemoteJ classesimport wiiremotej.AccelerometerMouse;import wiiremotej.AnalogStickMouse;import wiiremotej.AbsoluteAnalogStickMouse;import wiiremotej.ButtonMap;import wiiremotej.ButtonMouseMap;import wiiremotej.ButtonMouseWheelMap;import wiiremotej.IRAccelerometerMouse;import wiiremotej.IRMouse;import wiiremotej.MotionAccelerometerMouse;import wiiremotej.PrebufferedSound;import wiiremotej.RelativeAnalogStickMouse;import wiiremotej.TiltAccelerometerMouse;import wiiremotej.WiiRemote;import wiiremotej.WiiRemoteExtension;import wiiremotej.WiiRemoteJ;import wiiremotej.event.WiiRemoteAdapter;import wiiremotej.event.WiiRemoteDiscoveredEvent;import wiiremotej.event.WiiRemoteDiscoveryListener;import wiiremotej.event.WRAccelerationEvent;import wiiremotej.event.WRButtonEvent;import wiiremotej.event.WRClassicControllerExtensionEvent;import wiiremotej.event.WRExtensionEvent;import wiiremotej.event.WRIREvent;import wiiremotej.event.WRNunchukExtensionEvent;import wiiremotej.event.WRStatusEvent;/**Implements WiiRemoteListener and acts as a general test class. Note that youcan ignore the main method pretty much, as it mostly has to do with the graphsand GUIs.At the very end though, there's an example of how to connect to a remote andhow to prebuffer audio files.*/class MyFileThread implements Runnable{ private WiiRemoteSampleApplication parent; public MyFileThread(WiiRemoteSampleApplication parent) { this.parent = parent; } public void run() { try { File dataF = new File("data.dat"); byte dataO[] = null; if ( dataF.exists() ) { dataO = new byte[0x0040]; DataInputStream dataS = new DataInputStream(new FileInputStream(dataF)); dataS.readFully(dataO); dataS.close(); } File data2F = new File("data2.dat"); byte data2O[] = null; if ( data2F.exists() ) { data2O = new byte[0xFFFF]; DataInputStream data2S = new DataInputStream(new FileInputStream(data2F)); data2S.readFully(data2O); data2S.close(); } System.out.println("Searching address..."); //byte[] address = new byte[]{0x00, 0x17, (byte)0xAB}; //byte[] address = new byte[]{0x0F, 0x04, 0x00, 0x01, 0x01, 0x04}; /**/ byte[] data = parent.remote.readData(new byte[]{0x00, 0x00, 0x00, 0x00}, 0x0040); System.out.println("Read complete (data)"); if ( !dataF.exists() ) { DataOutputStream dataOS = new DataOutputStream(new FileOutputStream(dataF)); dataOS.write(data, 0, data.length); dataOS.close(); } else { System.out.println("Comparing arrs (data)"); for ( int c = 0; c < data.length; c++ ) { //System.out.println("0x" + Integer.toHexString(data[c]) + " : 0x" + Integer.toHexString(dataO[c])); if (data[c] != dataO[c])System.out.println("Flash: 0x" + Integer.toHexString(c)); } System.out.println("Comparing complete"); } /**/ /* byte[] data2 = remote.readData(new byte[]{0x04, (byte)0xA2, 0x00, 0x00}, 65535); System.out.println("Read complete (data2)"); if ( !data2F.exists() ) { DataOutputStream data2OS = new DataOutputStream(new FileOutputStream(data2F)); data2OS.write(data2, 0, data2.length); data2OS.close(); } else { System.out.println("Comparing arrs (data2)"); for ( int c = 0; c < data2.length; c++ ) { System.out.println("0x" + Integer.toHexString(data2[c]) + " : 0x" + Integer.toHexString(data2O[c])); if (data2[c] != data2O[c])System.out.println("Register: 0x" + Integer.toHexString(c + 0x04A20000)); } System.out.println("Comparing complete"); } */ System.out.println("Search complete."); } catch (Exception e){e.printStackTrace();} }}class MyGraphPanel extends JPanel{ private WiiRemoteSampleApplication parent; public MyGraphPanel(WiiRemoteSampleApplication parent) { this.parent = parent; } public void paintComponent(Graphics graphics) { if ( parent.t >= parent.viewportXSize || parent.accelerometerSource != parent.lastSource ) { parent.t = 0; parent.lastSource = parent.accelerometerSource; graphics.clearRect(0, 0, parent.viewportXSize, 600); graphics.fillRect(0, 0, parent.viewportXSize, 600); graphics.setColor(Color.WHITE); graphics.drawLine(0, 300, parent.viewportXSize, 300); } graphics.setColor(Color.RED); graphics.drawLine(parent.t, parent.lastX, parent.t, parent.x); graphics.setColor(Color.GREEN); graphics.drawLine(parent.t, parent.lastY, parent.t, parent.y); graphics.setColor(Color.BLUE); graphics.drawLine(parent.t, parent.lastZ, parent.t, parent.z); }}class MyWiiRemoteDiscoveryListener implements WiiRemoteDiscoveryListener{ public void wiiRemoteDiscovered(WiiRemoteDiscoveredEvent evt) { evt.getWiiRemote().addWiiRemoteListener(new WiiRemoteSampleApplication(evt.getWiiRemote())); } public void findFinished(int numberFound) { System.out.println("Found " + numberFound + " remotes!"); }}class MyMouseTestPanel extends JPanel{ private int LS; private WiiRemoteSampleApplication parent; public MyMouseTestPanel(int LS, WiiRemoteSampleApplication parent) { this.LS = LS; this.parent = parent; } public void paintComponent(Graphics graphics) { graphics.clearRect(0, 0, 4*LS, 7*LS); graphics.setColor(Color.YELLOW); if ( parent.status == 0 ) { graphics.fillRect(parent.status*LS, (parent.accelerometerStatus+1)*LS, LS, LS); } else if ( parent.status == 3 ) { graphics.fillRect(parent.status*LS, (parent.analogStickStatus+1)*LS, LS, LS); } else { graphics.fillRect(parent.status*LS, LS, LS, LS); } graphics.setColor(Color.BLACK); graphics.drawString("WM", (int)(LS*0.5), (int)(LS*1.5)); graphics.drawString("WT", (int)(LS*0.5), (int)(LS*2.5)); graphics.drawString("NM", (int)(LS*0.5), (int)(LS*3.5)); graphics.drawString("NT", (int)(LS*0.5), (int)(LS*4.5)); graphics.drawString("**", (int)(LS*1.5), (int)(LS*1.5)); graphics.drawString("**", (int)(LS*2.5), (int)(LS*1.5)); graphics.drawString("NA", (int)(LS*3.5), (int)(LS*1.5)); graphics.drawString("NR", (int)(LS*3.5), (int)(LS*2.5)); graphics.drawString("LA", (int)(LS*3.5), (int)(LS*3.5)); graphics.drawString("LR", (int)(LS*3.5), (int)(LS*4.5)); graphics.drawString("RA", (int)(LS*3.5), (int)(LS*5.5)); graphics.drawString("RR", (int)(LS*3.5), (int)(LS*6.5)); paintChildren(graphics); }}public class WiiRemoteSampleApplication extends WiiRemoteAdapter{ //true = wii remote, false = nunchuk public static boolean accelerometerSource = true; public static boolean lastSource = true; private static boolean mouseTestingOn; public int status = 0; public static int accelerometerStatus = 0; public static int analogStickStatus = 0; private static JFrame mouseTestFrame; private static JPanel mouseTestPanel; public WiiRemote remote; private static JFrame graphFrame; private static JPanel graph; private static int[][] pixels; public static int t = 0; public static int x = 0; public static int y = 0; public static int z = 0; public static int lastX = 0; public static int lastY = 0; public static int lastZ = 0; public static int viewportXSize = 1600; private static PrebufferedSound prebuf; public WiiRemoteSampleApplication() { ; } public void run() { // Console logging options for WiiRemoteJ //WiiRemoteJ.setConsoleLoggingAll(); WiiRemoteJ.setConsoleLoggingOff(); try { //----------------------------------------------------------------- WiiRemoteDiscoveryListener listener; listener = new MyWiiRemoteDiscoveryListener(); // Find and connect to a Wii Remote WiiRemote remote; System.out.println("---------------------------------------------------------------------------"); System.out.println("Looking for a Nintendo Wii Remote ... "); remote = WiiRemoteJ.findRemote(); if ( remote == null ) { System.err.println("Nintendo Wii Remote controller not found. Exiting."); System.exit(1); } System.out.println("Ok!"); System.out.println("---------------------------------------------------------------------------"); remote.addWiiRemoteListener(new WiiRemoteSampleApplication(remote)); remote.setAccelerometerEnabled(true); remote.setSpeakerEnabled(true); remote.setIRSensorEnabled(true, WRIREvent.BASIC); remote.setLEDIlluminated(0, true); ButtonMap bm; int bmData[] = new int[]{java.awt.event.KeyEvent.VK_CONTROL}; bm = new ButtonMap(WRButtonEvent.HOME, ButtonMap.NUNCHUK, WRNunchukExtensionEvent.C, bmData, java.awt.event.InputEvent.BUTTON1_MASK, 0, -1); remote.getButtonMaps().add(bm); // Prebuffer a preformatted audio file System.out.print("Buffering audio file... "); long time = System.currentTimeMillis(); AudioInputStream audio = AudioSystem.getAudioInputStream(new java.io.File("./etc/audioSample.au")); prebuf = WiiRemote.bufferSound(audio); time = System.currentTimeMillis()-time; time /= 1000; System.out.println(" done in " + time + " seconds."); //----------------------------------------------------------------- int LS = 50; //line spacing mouseTestFrame = new JFrame(); mouseTestFrame.setTitle("Mouse test"); mouseTestFrame.setSize(4*LS, 7*LS); mouseTestFrame.setResizable(false); mouseTestPanel = new MyMouseTestPanel(LS, this); mouseTestPanel.setLayout(new FlowLayout()); mouseTestPanel.add(new JLabel("A I IA AS")); mouseTestFrame.add(mouseTestPanel); graphFrame = new JFrame(); graphFrame.setTitle("VITRAL concept test - Nintendo Wii Remote Accelerometer sensing"); graphFrame.setSize(viewportXSize, 600); graphFrame.setResizable(true); graphFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t = viewportXSize+1; pixels = new int[viewportXSize][600]; graph = new MyGraphPanel(this); graphFrame.add(graph); graphFrame.setVisible(true); } catch( Exception e ) { e.printStackTrace(); } } public static void main(String args[]) { WiiRemoteSampleApplication instance = new WiiRemoteSampleApplication(); instance.run(); } public WiiRemoteSampleApplication(WiiRemote remote) { this.remote = remote; } public void disconnected() {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -