首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

J2ME 定位應用開發實戰:TouristUI 類與模擬器部署完整指南

     編輯:J2ME

TouristUI 核心視圖類深度解析

TouristUI 作為定位應用的主視圖組件,繼承 Canvas 並實現 CommandListener,負責即時渲染定位數據、地標編輯與指南針功能切換。

類架構與核心屬性

public class TouristUI extends Canvas implements CommandListener {
    // 狀態顯示字段
    private String providerState = "未知";
    private String proximityState = "等待中";
    private AddressInfo info;
    private QualifiedCoordinates coord;
    private float speed;
    
    // 功能命令
    private Command compassCmd = new Command("指南針", Command.OK, 1);
    private Command editorCmd = new Command("編輯器", Command.STOP, 1);
    
    // 子 UI 組件引用
    private LandmarkEditorUI editorUI;
    private CompassUI compassUI;
    
    public TouristUI(TouristData data) {
        editorUI = new LandmarkEditorUI(this, data);
        compassUI = new CompassUI(this);
        checkSupportedFeatures();
        addCommand(editorCmd);
        setCommandListener(this);
    }
}

動態功能檢測與命令管理

protected void checkSupportedFeatures() {
    if (ConfigurationProvider.getInstance().isOrientationSupported()) {
        addCommand(compassCmd);  // 支持方向感測才啟用指南針
    } else {
        removeCommand(compassCmd);
    }
}

數據更新與畫布渲染核心

public void setProviderState(String state) { providerState = state; }
public void setProximityState(String state) { proximityState = state; }

public void setInfo(AddressInfo info, QualifiedCoordinates coord, float speed) {
    this.info = info;
    this.coord = coord;
    this.speed = speed;
}

protected void paint(Graphics g) {
    Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    g.setFont(f);
    int lineHeight = f.getHeight();
    int line = 0;
    
    // 清空背景
    g.setColor(0xFFFFFF);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x0000FF);
    
    // 繪製基礎狀態
    g.drawString("提供者狀態: " + providerState, 0, lineHeight * line++, Graphics.LEFT | Graphics.TOP);
    g.drawString("接近監測: " + proximityState, 0, lineHeight * line++, Graphics.LEFT | Graphics.TOP);
    
    // 繪製座標與速度
    if (coord != null) {
        double lat = coord.getLatitude();
        double lon = coord.getLongitude();
        g.drawString("緯度,經度 (" + Utils.formatDouble(lat, 3) + ", " + Utils.formatDouble(lon, 3) + ")", 
                     0, lineHeight * line++, Graphics.TOP | Graphics.LEFT);
        g.drawString("速度: " + Utils.formatDouble(speed, 2) + " m/s", 
                     0, lineHeight * line++, Graphics.TOP | Graphics.LEFT);
    }
    
    // 繪製地址資訊
    if (info != null) {
        g.setColor(0x000000);
        drawAddressField(g, "國家", info.getField(AddressInfo.COUNTRY), lineHeight, line);
        drawAddressField(g, "省/州", info.getField(AddressInfo.STATE), lineHeight, line);
        drawAddressField(g, "城市", info.getField(AddressInfo.CITY), lineHeight, line);
        drawAddressField(g, "街道", info.getField(AddressInfo.STREET), lineHeight, line);
        drawAddressField(g, "建築物", info.getField(AddressInfo.BUILDING_NAME), lineHeight, line);
    }
}

private void drawAddressField(Graphics g, String label, String value, int lineHeight, int line) {
    if (value != null) {
        g.drawString(label + ": " + value, 0, lineHeight * line++, Graphics.TOP | Graphics.LEFT);
    }
}

命令事件處理與模擬器部署

public void commandAction(Command command, Displayable d) {
    if (command == editorCmd) {
        if (coord != null) {
            editorUI.showEditor(coord, LandmarkEditorUI.MODE_ADDNEW);
        } else {
            MessageUI.showLocationDataNotAvailable();
        }
    } else if (command == compassCmd) {
        TouristMIDlet.getDisplay().setCurrent(compassUI);
    }
}

模擬器與真機部署注意事項

  • 開發環境:基於 Eclipse IDE + Java ME SDK,參考 Nokia 官方開發者庫建立 MIDlet 專案
  • 平台限制:案例程式無法在 Series 40 平台運行,需 S60 或支援 JSR-179 的裝置
  • 真機測試連接:USB 資料線、藍牙、紅外或 OTA 空中下載任選其一
  • 裝置選擇參考:查閱 Java ME Developer's Library 獲取相容裝置清單

本文完整呈現了從核心 UI 實現到實際部署的完整鏈路,為 J2ME 定位應用開發提供了可直接參考的工程化範本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved