程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 定位API開發指南——例子:移動應用開發的定位和定位檢測(6)

定位API開發指南——例子:移動應用開發的定位和定位檢測(6)

編輯:J2ME

4.4.8 CompassUI

1、創建CompassUI類文件。

2、引入必要的類。

package com.nokia.example.location.tourist.ui;

import Java.io.IOException;

import Javax.microedition.lcdui.Canvas;

import Javax.microedition.lcdui.Command;

import Javax.microedition.lcdui.CommandListener;

import Javax.microedition.lcdui.Displayable;

import Javax.microedition.lcdui.Graphics;

import Javax.microedition.lcdui.Image;

import Javax.microedition.location.OrIEntation;

import com.nokia.example.location.tourist.TouristMIDlet;

import com.nokia.example.location.tourist.model.ConfigurationProvider;

3、創建CompassUI類,並設置擴展Canvas。運行CommandListener和Runnable。定義這個類需要使用的常量和命令。

/**

* VIEwer class representing a compass.

*/

public class CompassUI extends Canvas implements CommandListener, Runnable

{

/** Constant value for X coordinate. Used in arrays. */

private final int X = 0;

/** Constant value for Y coordinate. Used in arrays. */

private final int Y = 1;

/** Compass center X point */

private int centerX;

/** Compass center Y point */

private int centerY;

/** Constant value representing one degree. */

private final float degree = (float) (2 * Math.PI / 360.0);

/** Current compass azimuth. */

private float azimuth = 0.0f;

/**

* Is orientation relative to the magnetic fIEld of the Earth or true north

* and gravity.

*/

private boolean isMagnetic;

/** Flag telling is the compass update thread active */

private boolean threadActive = false;

/** Sleep 1000ms during each compass update */

private final long SLEEPTIME = 1000;

/** A reference to Route UI */

private Displayable route = null;

/** A reference to Pitch and Roll UI */

private Displayable pitchrollUI = null;

/** Command that swithes current displayable to Route UI */

private Command routeCmd = new Command("Route", Command.BACK, 1);

/** Command that swithes current displayable to Pitch and Roll UI */

private Command prCmd = new Command("Pitch and Roll", Command.OK, 1);

/** Compss background image. */

private Image compassImage = null;

4、為類創建結構。

/**

* Construct instance of this displayable.

*

* @param route

* is a reference to Route UI.

*/

public CompassUI(Displayable route)

{

this.route = route;

pitchrollUI = new PitchRollUI(this);

loadCompassImage();

centerX = getWidth() / 2;

centerY = getHeight() / 2;

addCommand(routeCmd);

addCommand(prCmd);

setCommandListener(this);

}

5、為Compass創建方法,加載背景圖像。

/**

* Load compass backgound image from JAR file.

*/

private void loadCompassImage()

{

String imageName = "/compass_small.gif";

if (getWidth() > 160)

{

imageName = "/compass_large.gif";

}

try

{

compassImage = Image.createImage(getClass().getResourceAsStream(

imageName));

}

catch (IOException e)

{

e.printStackTrace();

}

}

6、創建以下兩個方法,根據Canvas的可見性,VM選擇調用。

/**

* VM calls this method immediately prior to this Canvas being made visible

* on the display.

*/

protected void showNotify()

{

// Actives compass update thread.

threadActive = true;

new Thread(this).start();

}

/**

* VM calls this method shortly after the Canvas has been removed from the

* display.

*/

protected void hideNotify()

{

// Stops the thread.

threadActive = false;

}

7、創建方法,引導canvas。

/**

* Renders the canvas.

*

* @param g -

* the Graphics object to be used for rendering the Canvas

*/

protected void paint(Graphics g)

{

// clean up canvas

g.setColor(255, 255, 255);

g.fillRect(0, 0, getWidth(), getHeight());

int spikeLen = 5;

int len = (compassImage.getWidth() / 2) - spikeLen;

// draw compass background

g.drawImage(compassImage, (getWidth() - compassImage.getWidth()) / 2,

centerY, Graphics.LEFT | Graphics.VCENTER);

// draw compass arrow

g.setColor(0, 0, 255);

drawArrow(g, degree * azimuth, len, spikeLen);

// draw orIEntation type

g.setColor(0, 0, 255);

String otext = "True North";

if (!isMagnetic)

{

otext = "Magnetic fIEld";

}

g.drawString("OrIEntation: " + otext, 0, getHeight(), Graphics.BOTTOM

| Graphics.LEFT);

}

8、創建方法,描述compass arrow。

/**

* Draw a compass arrow rotated to a certain angle.

*

* @param g

* is a reference to Graphics object.

* @param angle

* in degrees [0.0,360.0)

* @param len

* is arrows length.

* @param spikeLen

* is length of arrows spike.

*/

private void drawArrow(Graphics g, float angle, int len, int spikeLen)

{

int a[] = rotate(angle, 0, -(len - spikeLen));

int b[] = rotate(angle, -spikeLen, -(len - spikeLen));

int c[] = rotate(angle, 0, -len);

int d[] = rotate(angle, spikeLen, -(len - spikeLen));

int e[] = rotate(angle + (degree * 180.0), 0, -len);

// use red foreground color

g.setColor(255, 0, 0);

g.drawLine(centerX, centerY, centerX + a[X], centerY + a[Y]);

g.drawLine(centerX + b[X], centerY + b[Y], centerX + d[X], centerY

+ d[Y]);

g.drawLine(centerX + b[X], centerY + b[Y], centerX + c[X], centerY

+ c[Y]);

g.drawLine(centerX + c[X], centerY + c[Y], centerX + d[X], centerY

+ d[Y]);

// use black foreground color

g.setColor(0, 0, 0);

g.drawLine(centerX, centerY, centerX + e[X], centerY + e[Y]);

}

9、創建方法,旋轉箭頭方向。

/**

* Rotate point (x,y) by degrees that angle parameter defines. The new

* coordinate calculation is performed with a 2x2 rotate matrix.

*

* @param angle

* to be rotated

* @param x

* coordinate

* @param y

* coordinate

* @return new coordinate pair in int array format [x,y]

*/

private int[] rotate(double angle, int x, int y)

{

int rotated[] = new int[2];

rotated[X] = (int) (Math.cos(angle) * x + Math.sin(angle) * y);

rotated[Y] = (int) (-Math.sin(angle) * x + Math.cos(angle) * y);

return rotated;

}

10、通過Runnable創建一個運行的方法。目的是更新角度、高度和長度,覆蓋canvas。isOrIEntationMagnetic方法將返回一個布爾值,用來說明OrIEntation方法是否與地球的地磁連接或者使用的是真實的北極點。

/**

* run method from Runnable interface. Updates azimuth, pitch and roll

* values and repaints the canvas.

*

* If OrIEntation is supported on the terminal, compass sensor is either 2D

* or 3D. If the terminals compass sensor providers only compass azimuth,

* pitch and roll values are Float.NaN.

*

* @see HideNotify() method.

*/

public void run()

{

// Keep the thread running until another displayable is set visible.

// See also hideNotify() method.

while (threadActive)

{

Orientation orIEntation = ConfigurationProvider.getInstance()

.getOrIEntation();

if (orIEntation != null)

{

isMagnetic = orientation.isOrIEntationMagnetic();

azimuth = orIEntation.getCompassAzimuth();

}

repaint();

try

{

// Pause this thread for a secord before next update.

Thread.sleep(SLEEPTIME);

}

catch (InterruptedException e)

{

}

}

}

11、創建方法,檢測命令按鍵的輸入。

/**

* Event indicating when a command button is pressed.

*

* @see Javax.microedition.lcdui.CommandListener#commandAction

(Javax.microedition.lcdui.Command,

* Javax.microedition.lcdui.Displayable)

*/

public void commandAction(Command command, Displayable d)

{

if (command == routeCmd)

{

TouristMIDlet.getDisplay().setCurrent(route);

}

else if (command == prCmd)

{

TouristMIDlet.getDisplay().setCurrent(pitchrollUI);

}

}

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved