package sample;
import box.Box;
import bt.BTClient;
import bt.ConnectionLostException;
import javax.microedition.io.StreamConnection;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class ClientCanvas extends Canvas implements Runnable {
private final static int deviceNum = 2;
private BTSampleMIDlet midlet;
private int exitCount;
private String[] remoteDevices;// doChoice()之前使用,之后作废
private String[] devicesName;// doChoice()完成初始化后使用
private Box[] boxs;
private int[][] boxsCoor;
private int[][] dnsCoor;// devices name Coordinate
private int fontHeight;
private int[] fontWidth;
private boolean isBuilding;
private boolean isChoice;
private boolean isConnecting;
private boolean running;
private boolean btOver;// bluetooth connection is over;
private boolean hasExce;// has a Exception
private boolean isQuit;
private int srcWidth;
private int srcHeight;
private int[] colorArray;
private int useKey;// 仅限当前线程进行使用,防线程无序导致发送的key与运行的key前后不一致
private int localKey;// 当前线程与系统线程同时使用
private int remoteKey;
private int activeDevice;
private String exceptionStr;
public ClientCanvas(BTSampleMIDlet mid) {
setFullScreenMode(true);
midlet = mid;
exitCount = 0;
boxs = new Box[deviceNum];
boxs[1] = new Box(100, 100);// Server, Remote
boxs[0] = new Box(100, 200);// Client, Local
boxsCoor = new int[2][2];
boxsCoor[0][0] = 100;
boxsCoor[0][1] = 200;
boxsCoor[1][0] = 100;
boxsCoor[1][1] = 100;
dnsCoor = new int[2][2];
dnsCoor[0][0] = boxsCoor[0][0];
dnsCoor[0][1] = boxsCoor[0][1] - (boxs[0].getHeightBox() >> 1);
dnsCoor[1][0] = boxsCoor[1][0];
dnsCoor[1][1] = boxsCoor[1][1] - (boxs[1].getHeightBox() >> 1);
devicesName = new String[deviceNum];
devicesName[0] = BTClient.getLocalDeviceName();
devicesName[1] = "";
fontHeight = 0;
fontWidth = new int[deviceNum];
isBuilding = isChoice = isConnecting = running = btOver = hasExce = isQuit = false;
srcWidth = 240;
srcHeight = 310;
useKey = localKey = remoteKey = 0;
activeDevice = -1;
exceptionStr = "";
}
private void initColorArray() {
int len = srcWidth * fontHeight;
colorArray = new int[len];
for (int i = 0; i < len; i++) {
colorArray[i] = 0x7f0000ff;
}
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(0);
g.fillRect(0, 0, 240, 310);
// 内容显示
if (isBuilding == true) {
showWaiting(g);
} else if (isChoice == true) {
drawChoiceFrame(g);
showDevicesName(g);
} else if (isConnecting == true) {
showConnectDeviceInfo(g);
entryGame(g);
} else if (running == true) {
drawBoxs(g);
showCoordinates(g);
testQuit(g);
} else if (btOver == true) {
drawBoxs(g);
showCoordinates(g);
drawBtOver(g);
}
showExceptionInfo(g);
}
private void showWaiting(Graphics g) {
g.setColor(0xffffff);
g.drawString("Waiting for connecting", 120, 155, Graphics.HCENTER
| Graphics.BOTTOM);
}
private void drawChoiceFrame(Graphics g) {
// 考虑到activeDevice初始值为-1
if (activeDevice >= 0) {
g.setColor(0x0000ff);
g.fillRect(0, activeDevice * 20, 240, 20);
}
}
private void showDevicesName(Graphics g) {
g.setColor(0xffffff);
int len = remoteDevices.length;
for (int i = 0; i < len; i++) {
g.drawString(remoteDevices[i], 120, 20 * (i + 1), Graphics.HCENTER
| Graphics.BOTTOM);
}
}
private void showConnectDeviceInfo(Graphics g) {
g.setColor(0xffffff);
g.drawString("Connection is OK!", 120, 20, Graphics.HCENTER
| Graphics.TOP);
g.drawString("Remote Device Name:", 120, 40, Graphics.HCENTER
| Graphics.TOP);
g.drawString(devicesName[1], 120, 60, Graphics.HCENTER | Graphics.TOP);
}
private void entryGame(Graphics g) {
g.setColor(0xffffff);
g.drawString("OK", 0, srcHeight, Graphics.BOTTOM | Graphics.LEFT);
}
private void drawBoxs(Graphics g) {
g.setColor(0xffff00);
int len = deviceNum;
for (int i = 0; i < len; i++) {
// 先画方块,再画设备名
Box b = boxs[i];
g.fillRect(b.getDrawX(), b.getDrawY(), b.getWidthBox(), b
.getHeightBox());
g.drawLine(0, b.getCenterY(), srcWidth, b.getCenterY());
g.drawLine(b.getCenterX(), 0, b.getCenterX(), srcHeight);
g.drawString(devicesName[i], dnsCoor[i][0], dnsCoor[i][1],
Graphics.HCENTER | Graphics.TOP);
}
}
private void showCoordinates(Graphics g) {
// 画出Local Box坐标信息
g.drawRGB(colorArray, 0, srcWidth, 0, 0, srcWidth, fontHeight, true);
g.drawString("X = " + boxsCoor[0][0], 0, 0, Graphics.TOP
| Graphics.LEFT);
g.drawString(devicesName[0], 120, 0, Graphics.HCENTER | Graphics.TOP);
g.drawString("Y = " + boxsCoor[0][1], srcWidth, 0, Graphics.TOP
| Graphics.RIGHT);
// 画出Remote Box坐标信息
g.drawRGB(colorArray, 0, srcWidth, 0, srcHeight - fontHeight, srcWidth,
fontHeight, true);
g.drawString("X = " + boxsCoor[1][0], 0, srcHeight, Graphics.BOTTOM
| Graphics.LEFT);
g.drawString(devicesName[1], 120, srcHeight, Graphics.HCENTER
| Graphics.BOTTOM);
g.drawString("Y = " + boxsCoor[1][1], srcWidth, srcHeight,
Graphics.BOTTOM | Graphics.RIGHT);
}
private void drawBtOver(Graphics g) {
g.setColor(0xffffff);
g.drawString("The boxs are coincident!!", 120, 155, Graphics.BOTTOM
| Graphics.HCENTER);
g.drawString("Please Pressed the OK key!", 120, 175, Graphics.BOTTOM
| Graphics.HCENTER);
}
/**
*如果有按左软键,将显示退出询问,连续两次按左软键后将执行退出命令。
*/
private void testQuit(Graphics g) {
int key = useKey;
if (key == -6) {
g.drawString("Quit this app?", srcWidth >> 1, srcHeight - 50,
Graphics.HCENTER | Graphics.TOP);
g.drawString("YES", 0, srcHeight, Graphics.BOTTOM | Graphics.LEFT);
g.drawString("NO", srcWidth, srcHeight, Graphics.BOTTOM
| Graphics.RIGHT);
}
}
private void showExceptionInfo(Graphics g) {
if (hasExce) {
g.setColor(0);
g.fillRect(0, 0, srcWidth, srcHeight);
g.setColor(0xffffff);
g
.drawString(exceptionStr, 120, 155, Graphics.BOTTOM
| Graphics.TOP);
g.drawString("OK", 0, srcHeight, Graphics.BOTTOM | Graphics.LEFT);
}
}
public void run() {
isBuilding = true;
repaint();
BTClient btClient = new BTClient();
btClient.clientStartInquiry();
remoteDevices = btClient.getDevicesVec();
isBuilding = false;
isChoice = true;
loop1: while (isChoice) {
useKey = localKey;
setActiveDev();
doChoice();
if (isChoice == false) {
isConnecting = true;
useKey = 0;
localKey = 0;
break loop1;
}
repaint();
useKey = 0;
localKey = 0;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("InterruptedException while(isChoice)");
}
}
// isChoice = false;
btClient.clientSearchService(activeDevice);
String connURL = btClient.getConnURL();
System.out.println("connURL: " + connURL);
StreamConnection conn = null;
conn = btClient.getStreamConnection(connURL);
DataInputStream dis = null;
dis = btClient.getDIS(conn);
DataOutputStream dos = null;
dos = btClient.getDOS(conn);
// 向服务端发送本地设备名字
btClient.sendLocalDeviceName(dos, devicesName[0]);
initFontPara();
initColorArray();
loop2: while (isConnecting) {
useKey = localKey;
makeSure();
if (isConnecting == false) {
useKey = 0;
localKey = 0;
running = true;
break loop2;
}
repaint();
useKey = 0;
localKey = 0;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out
.println("InterruptedException
蓝牙演示实例——MoveBoxs
需积分: 4 67 浏览量
2009-07-24
16:41:37
上传
评论
收藏 43KB RAR 举报
Sodino
- 粉丝: 1439
- 资源: 34
最新资源
- MyBatis 动态 SQL:灵活而强大的查询构建器.pdf
- com.accordion.prettyo.apk
- 毕业设计:基于SSM的mysql-ssm软件bug管理系统(源码 + 数据库 + 说明文档)
- MTSQL8.0.35windows(64bit)-mysql-installer-community-8.0.35.0
- 人工智能引领音乐创作新时代之Suno AI
- Public-bicycle-usage-forecast-master.zip
- 通道处理过程模拟:从理论到实践.pdf
- 数据库第七次作业E-R图第一题
- 大厂面试真题Java语法基础面试专题及答案
- IMG20240428211124.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈