现在的位置: 首页 > 综合 > 正文

用j2me开发GIS程序-地图绘制2

2013年12月13日 ⁄ 综合 ⁄ 共 2609字 ⁄ 字号 评论关闭
地图渲染类MapCanvas 实现地图漫游和缩放
public class MapCanvas extends Canvas
{
    private MIDlet mapMidlet;
    private int lines[][] = { { 10, 10, 10, 20, 20, 20, 20, 30 },
            { 50, 50, 70, 90, 60, 80 } };
    private int points[][] = { { 30, 30, 60, 90 } }; //测试数据
    private static int iX = 0; //移动偏移量X
    private static int iY = 0; //移动偏移量Y
    private static float fZoom = 1.0F; //缩放参数
    private static final short iStep = 10; //移动步长
    private String message = null;
    
    public MapCanvas(MIDlet midlet) {
        mapMidlet = midlet;
        message = "J2ME Map 0.1";
    }

    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 = iX + (int) (mline[j] * fZoom);
                int word2 = iY + (int) (mline[j + 1] * fZoom);
                int word3 = iX + (int) (mline[j + 2] * fZoom);
                int word4 = 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 = iX + (int) ((float) mpoint[j] * fZoom);
                int word2 = iY + (int) ((float) mpoint[j + 1] * fZoom);
                g.setColor(255, 0, 0);
                g.fillRoundRect(word1, word2, 6, 6, 3, 3);
                g.drawString("No." + j, word1, word2, 0x10 | 0x1);
            }

        }

        //操作提示信息
        g.setColor(255, 255, 255);
        g.drawString(message, getWidth() / 2, 0, 0x10 | 0x1);
    }

    /**
     * 按键事件处理
     * @param keyCode
     */
    protected void keyAction(int keyCode) {
        if (keyCode == getKeyCode(8)) {
            message = "CENTER X=" + iX + " Y=" + iY;
            iX = 0;
            iY = 0;
        } else if (keyCode == getKeyCode(2)) {
            message = "LEFT X=" + iX + " Y=" + iY;
            iX -= iStep;
        } else if (keyCode == getKeyCode(5)) {
            message = "RIGHT X=" + iX + " Y=" + iY;
            iX += iStep;
        } else if (keyCode == getKeyCode(1)) {
            message = "UP X=" + iX + " Y=" + iY;
            iY -= iStep;
        } else if (keyCode == getKeyCode(6)) {
            message = "DOWN X=" + iX + " Y=" + iY;
            iY += iStep;
        } else if (keyCode == -1) {
            message = "ZoomOut Zoom=" + fZoom;
            fZoom = (float) ((double) fZoom * 1.75D);
        } else if (keyCode == -4) {
            message = "ZoomIn Zoom=" + fZoom;
            fZoom = (float) ((double) fZoom / 1.75D);
        } else {
            message = getKeyName(keyCode);
        }
        repaint();
    }

    /**
     * 按键响应
     */
    public void keyPressed(int ikeyCode) {
        keyAction(ikeyCode);
    }

    /**
     * 重复按键响应
     */
    public void keyRepeated(int ikeyCode) {
        keyAction(ikeyCode);
    }

    /**
     * 定位设备响应
     */
    public void pointerPressed(int x, int y) {
        //未使用
    }

}

本代码在Sun模拟器和西门子S65手机上测试通过。

【上篇】
【下篇】

抱歉!评论已关闭.