📄 controllrobot.java
字号:
/*************************************************************
*
* The Mobile Robotics Project
* - mobilerobotics.sourceforge.net
*
* This file was created 2005-01-07
* More information in MobileRoboticsMidlet.java
*
*************************************************************/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.lang.Thread;
public class ControllRobot extends GameCanvas implements Runnable {
private boolean doPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int width; // To hold screen width
private int height; // To hold screen height
private int direction = 8; // The direction the robot goes in (for display)..
private int ydirection = 0; // The last sent Y command (forward/backward/stop)
private int xdirection = 0; // The last send X command (left/right/stop)
private Bluetooth bluetooth; // Bluetooth class
private MobileRoboticsMidlet mobileRoboticsMidlet; // MobileRoboticsMidlet class
private int mode; // Mode (pointer or regular)
private boolean pointer; // Do we have a pointer?
private boolean havePoints; // Do we have points?
private int boxy;
private int left; // The left (X) for our "is running, stopped"
private int top;
private int[][] points; // our points
private int nrpoints; // the nr of points..
private int[][] dpoints; // A duplicate of points for when running the path*
private int dnrpoints; // A duplocate of nr of points for when running the path*
// *: When we run the path we created we must be able
// to see how many points we have passed, and if there are any left.
// in short, we must have one path to compare with and one to
// store were we already been
/*
* Bluetooth commands and their function (int direction)
*
* Car is running..
*
* 0) Power On
* 1) Power Off
* 2) Forward
* 3) Backward
* 4) Left
* 5) Right
* 6) Stop
*
* For local use only:
* 7) Switch (Switch modes (key to pointer))
* 8) Killed (It's powered off)
*/
// Constructor and initialization
public ControllRobot(MobileRoboticsMidlet mobileMidlet, Bluetooth blue) {
super(true);
width = getWidth();
height = getHeight();
bluetooth = blue;
mobileRoboticsMidlet = mobileMidlet;
}
// Automatically start thread for game loop
public void start() {
System.err.println(" Inited: ControllRobot");
// Do we have pointers?
pointer = hasPointerEvents();
// Loop
doPlay = true;
// Software mode
mode = 1;
// Start
Thread t = new Thread(this);
t.start();
// See bluetooth commands.
direction = 8;
// If the display is to small we might have to change our values for
// left and top margins due to otherwise overlapping images.
if((width / 2 - 80) < 15)
{
left = 15;
}else{
left = width / 2 - 80;
}
if((height / 2 - 50) < 70)
{
top = 65;
}else{
top = height / 2 - 50;
}
// Boxes configureation
boxy = height - 50;
// We don't have any points stored
havePoints = false;
// Pointer array
points = new int[10][2];
// We don't have any points yet
nrpoints = 0;
}
public boolean running()
{
return doPlay;
}
public void stop()
{
doPlay = false;
}
public void run() {
Graphics g = getGraphics();
while (doPlay == true) {
// Two is for status/debug screen, one is for regular controllRobot
if(mode == 1)
{
input();
drawScreen(g);
}else {
if(pointer)
{
drawPointerScreen(g);
}else{
drawAboutScreen(g);
}
}
}
}
protected void keyPressed( int keyCode ) {
// Press 0 to toggle modes
if(keyCode == 48)
{
if(mode == 1)
{
bluetooth.command(6);
direction = 7;
mode = 2;
}else{
if(havePoints == true)
{
clearPath();
}
bluetooth.command(6);
direction = 6;
mode = 1;
}
}
}
public void pointerPressed(int x, int y)
{
// If we are in regular mode there is no reason for pointerevents, there for return.
if(mode == 1)
return;
// If statement is true we have pressed the pointer in the draw area and not on a button
if(y < boxy)
{
// We don't want anyone drawing lines that are going back and forth,
// our robot should just go forward in a special pattern. Therefor we don't
// count any pointerPress that is behind the previous dot.
if((nrpoints > 0 ) && (y > points[nrpoints-1][1]))
return;
// You should be able to press a point outside of the drawing area.
if(x < width / 2 - 89 || x > (width / 2 - 89 + 177) || y < 41 || y > (41 + boxy - 42))
return;
if(havePoints == false)
{
havePoints = true;
}
// Draw a line
System.err.println("draw a line");
points[nrpoints][0] = x;
points[nrpoints][1] = y;
nrpoints++;
// Right button
}else if((y > boxy) && (x > (width / 2)))
{
System.err.println("Run path");
runPath();
// Left button
}else if((y > boxy) && (x < (width / 2)))
{
// Start clearing all path variables
clearPath();
}
}
private void clearPath()
{
System.err.println("Clear paths");
for (int i=0; i < nrpoints; i++)
{
points[i][0] = 0;
points[i][1] = 0;
}
points[nrpoints][0] = 0;
points[nrpoints][1] = 0;
havePoints = false;
nrpoints = 0;
}
public void runPath()
{
}
public void resetDirection()
{
direction = 6;
ydirection = 6;
xdirection = 6;
}
// Method to Handle User Inputs
private void input() {
int keyStates = getKeyStates();
// Left
if ((keyStates & LEFT_PRESSED) != 0)
{
if(xdirection == 4)
{
// Left
direction = 4;
bluetooth.command(4);
}else if(xdirection == 5)
{
// We are turning right so let's go to neutral.
bluetooth.command(4);
// Depending on the ydirection we set the wheel to a certain angle.
if(ydirection == 2 || ydirection == 3)
{
direction = ydirection;
}else{
direction = 6;
}
xdirection = 6;
// If it's in neutral send left
}else if(xdirection == 6)
{
bluetooth.command(4);
xdirection = 4;
direction = 4;
}else{ // Other
direction = 4;
bluetooth.command(4);
xdirection = 4;
}
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
{
if(xdirection == 5)
{
// Right
direction = 5;
bluetooth.command(5);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -