地图渲染类MapCanvas,该类完成地图绘制和操作响应
public class MapCanvas extends Canvas {
private MIDlet mapMidlet;
private int lines[][] = { { -10, -10, -10, 0, 20, 20, 20, 30 },
{ 50, 50, 70, 90, 60, 80 } };
protected static int[][] points = { { 30, 30 }, { 60, 90 } }; // 测试数据
private static int iX = 0; // 移动偏移量X
private static int iY = 0; // 移动偏移量Y
private static float fZoom = 0.001F; // 缩放参数 测试取得
private static final short iStep = 20; // 移动步长 测试取得
private static int iWidth; //手机显示屏宽
private static int iHeight; //手机显示屏高
private static int iW2; //手机显示屏中心点X
private static int iH2; //手机显示屏中心点Y
private String message = null;
public MapCanvas(MIDlet midlet) {
mapMidlet = midlet;
message = "J2ME Map 0.1";
}
public void init() {
iWidth = getWidth();
iHeight = getHeight();
iW2 = iWidth / 2;
iH2 = iHeight / 2;
// 启用新线程,从Jar包中读取自定义数据
(new Loader()).start();
}
/**
* 绘制地图
* 实际应用中,可考虑使用空间索引过滤当前视图,加快绘图速度
*
* @param g
*/
protected void paint(Graphics g) {
// 清空屏幕
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
// 画线
for (int l = 0; l < lines.length; l++) {
int mline[] = lines[l];
for (int j = 0; j < mline.length - 2; j += 2) {
int word1 = iW2 + iX + (int) (mline[j] * fZoom);
int word2 = iH2 + iY + (int) (mline[j + 1] * fZoom);
int word3 = iW2 + iX + (int) (mline[j + 2] * fZoom);
int word4 = iH2 + iY + (int) (mline[j + 3] * fZoom);
g.setColor(255, 255, 255);
g.drawLine(word1, word2, word3, word4);
}
}
// 画点(矩形)和文字
for (int l = 0; l < points.length; l++) {
int mpoint[] = points[l];
for (int j = 0; j < mpoint.length; j += 2) {
int word1 = iW2 + iX + (int) ((float) mpoint[j] * fZoom);
int word2 = iH2 + iY + (int) ((float) mpoint[j + 1] * fZoom);
g.setColor(255, 0, 0);
g.fillRoundRect(word1, word2, 6, 6, 3, 3);
//放大到一定比例显示标注
if (fZoom > 3) {
g.setColor(33, 33, 255);
g.drawString("P" + l, word1, word2, 0x10 | 0x1);
}
}
}
// 操作提示信息
g.setColor(255, 255, 255);
g.drawString(message, getWidth() / 2, 0, 0x10 | 0x1);
}
/**
* 按键事件处理
*
* @param keyCode
*/
protected void keyAction(int keyCode) {
try {
if (keyCode == getKeyCode(8)) {
iX = 0;
iY = 0;
message = "CENTER X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(2)) {
iX -= iStep;
message = "LEFT X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(5)) {
iX += iStep;
message = "RIGHT X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(1)) {
iY -= iStep;
message = "UP X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(6)) {
iY += iStep;
message = "DOWN X=" + iX + " Y=" + iY;
} else if (keyCode == -1) {
fZoom = (float) ((double) fZoom * 1.75D);
message = "ZoomOut Zoom=" + fZoom;
} else if (keyCode == -4) {
fZoom = (float) ((double) fZoom / 1.75D);
message = "ZoomIn Zoom=" + fZoom;
} else {
message = getKeyName(keyCode);
}
repaint();
} catch (Exception exp) {
exp.printStackTrace();
}
}
/**
* 按键响应
*/
public void keyPressed(int ikeyCode) {
keyAction(ikeyCode);
}
/**
* 重复按键响应
*/
public void keyRepeated(int ikeyCode) {
keyAction(ikeyCode);
}
/**
* 定位设备响应
*/
public void pointerPressed(int x, int y) {
// 未使用
}
}
本段代码在Sun模拟器和西门子S65手机上测试通过。
public class MapCanvas extends Canvas {
private MIDlet mapMidlet;
private int lines[][] = { { -10, -10, -10, 0, 20, 20, 20, 30 },
{ 50, 50, 70, 90, 60, 80 } };
protected static int[][] points = { { 30, 30 }, { 60, 90 } }; // 测试数据
private static int iX = 0; // 移动偏移量X
private static int iY = 0; // 移动偏移量Y
private static float fZoom = 0.001F; // 缩放参数 测试取得
private static final short iStep = 20; // 移动步长 测试取得
private static int iWidth; //手机显示屏宽
private static int iHeight; //手机显示屏高
private static int iW2; //手机显示屏中心点X
private static int iH2; //手机显示屏中心点Y
private String message = null;
public MapCanvas(MIDlet midlet) {
mapMidlet = midlet;
message = "J2ME Map 0.1";
}
public void init() {
iWidth = getWidth();
iHeight = getHeight();
iW2 = iWidth / 2;
iH2 = iHeight / 2;
// 启用新线程,从Jar包中读取自定义数据
(new Loader()).start();
}
/**
* 绘制地图
* 实际应用中,可考虑使用空间索引过滤当前视图,加快绘图速度
*
* @param g
*/
protected void paint(Graphics g) {
// 清空屏幕
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
// 画线
for (int l = 0; l < lines.length; l++) {
int mline[] = lines[l];
for (int j = 0; j < mline.length - 2; j += 2) {
int word1 = iW2 + iX + (int) (mline[j] * fZoom);
int word2 = iH2 + iY + (int) (mline[j + 1] * fZoom);
int word3 = iW2 + iX + (int) (mline[j + 2] * fZoom);
int word4 = iH2 + iY + (int) (mline[j + 3] * fZoom);
g.setColor(255, 255, 255);
g.drawLine(word1, word2, word3, word4);
}
}
// 画点(矩形)和文字
for (int l = 0; l < points.length; l++) {
int mpoint[] = points[l];
for (int j = 0; j < mpoint.length; j += 2) {
int word1 = iW2 + iX + (int) ((float) mpoint[j] * fZoom);
int word2 = iH2 + iY + (int) ((float) mpoint[j + 1] * fZoom);
g.setColor(255, 0, 0);
g.fillRoundRect(word1, word2, 6, 6, 3, 3);
//放大到一定比例显示标注
if (fZoom > 3) {
g.setColor(33, 33, 255);
g.drawString("P" + l, word1, word2, 0x10 | 0x1);
}
}
}
// 操作提示信息
g.setColor(255, 255, 255);
g.drawString(message, getWidth() / 2, 0, 0x10 | 0x1);
}
/**
* 按键事件处理
*
* @param keyCode
*/
protected void keyAction(int keyCode) {
try {
if (keyCode == getKeyCode(8)) {
iX = 0;
iY = 0;
message = "CENTER X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(2)) {
iX -= iStep;
message = "LEFT X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(5)) {
iX += iStep;
message = "RIGHT X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(1)) {
iY -= iStep;
message = "UP X=" + iX + " Y=" + iY;
} else if (keyCode == getKeyCode(6)) {
iY += iStep;
message = "DOWN X=" + iX + " Y=" + iY;
} else if (keyCode == -1) {
fZoom = (float) ((double) fZoom * 1.75D);
message = "ZoomOut Zoom=" + fZoom;
} else if (keyCode == -4) {
fZoom = (float) ((double) fZoom / 1.75D);
message = "ZoomIn Zoom=" + fZoom;
} else {
message = getKeyName(keyCode);
}
repaint();
} catch (Exception exp) {
exp.printStackTrace();
}
}
/**
* 按键响应
*/
public void keyPressed(int ikeyCode) {
keyAction(ikeyCode);
}
/**
* 重复按键响应
*/
public void keyRepeated(int ikeyCode) {
keyAction(ikeyCode);
}
/**
* 定位设备响应
*/
public void pointerPressed(int x, int y) {
// 未使用
}
}
本段代码在Sun模拟器和西门子S65手机上测试通过。