gamepadtest.java
来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 117 行
JAVA
117 行
public class GamepadTest
{
// masks for the gamepad buttons
public static final int BUTTON_1 = 0x10;
public static final int BUTTON_2 = 0x20;
// the x and y axis of the crosspad
public static final int GAMEPAD_AXIS_X = 1;
public static final int GAMEPAD_AXIS_Y = 2;
// the "centered" state of the crosspad
public static int centerX;
public static int centerY;
// the max/min "bounds" of the crosspad
public static int maxX;
public static int minX;
public static int maxY;
public static int minY;
// determines if the given button is being pressed
public native int gamepadButtonPressed(int button);
// returns the position of the sent axis
public native int gamepadAxis(int axis);
// load the shared library
static
{
System.loadLibrary("GamepadImpl");
}
// creates a new GamepadTest object and calibrates the gamepad
public GamepadTest()
{
calibrateGamepad();
}
public void calibrateGamepad()
{
System.out.println("Swirl the gamepad around a bit then hit any button");
// initialize the bounds to impossible values
maxX = 0;
maxY = 0;
minX = 100000;
minY = 100000;
int x = 0;
int y = 0;
while(0 == gamepadButtonPressed(BUTTON_1 | BUTTON_2))
{
x = gamepadAxis(GAMEPAD_AXIS_X);
y = gamepadAxis(GAMEPAD_AXIS_Y);
// update the max/min values as the user swirls the crosspad
maxX = Math.max(x, maxX);
minX = Math.min(x, minX);
maxY = Math.max(y, maxY);
minY = Math.min(y, minY);
}
// user let go of the crosspad; this must be our center position
centerX = x;
centerY = y;
System.out.println("Calbration complete!");
System.out.println("xmin: " + minX + " xcenter: " + centerX + " xmax: " + maxX);
System.out.println("ymin: " + minY + " ycenter: " + centerY + " ymax: " + maxY);
}
public static void main(String args[])
{
GamepadTest test = new GamepadTest();
int lastX = test.gamepadAxis(GAMEPAD_AXIS_X);
int lastY = test.gamepadAxis(GAMEPAD_AXIS_Y);
while(true)
{
// output any button-down states
if(test.gamepadButtonPressed(BUTTON_1) != 0)
{
System.out.println("Button 1 down!");
}
if(test.gamepadButtonPressed(BUTTON_2) != 0)
{
System.out.println("Button 2 down!");
}
// output the crosspad states
int x = test.gamepadAxis(GAMEPAD_AXIS_X);
int y = test.gamepadAxis(GAMEPAD_AXIS_Y);
if(x > centerX)
{
System.out.println("+x");
}
else if(x < centerX)
{
System.out.println("-x");
}
if(y > centerY)
{
System.out.println("+y");
}
else if(y < centerY)
{
System.out.println("-y");
}
}
}
} // GamepadTest
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?