📄 locationbasedservice.java
字号:
/*
*
* Copyright © 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
package com.sun.perseus.demo.locationBasedService;
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import org.w3c.dom.Document;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGPoint;
import org.w3c.dom.svg.SVGRect;
import org.w3c.dom.svg.SVGSVGElement;
import org.w3c.dom.svg.SVGRGBColor;
import com.sun.perseus.demo.PlaySVGImageDemo;
/**
* An example of zooming and panning. A sample itinerary is used in combination
* with a map of downtown San Francisco to show how a route can be traced from a
* starting point to an ending point. This also demonstrates how to
* insert/remove the traced route into/from the map content so that the route
* can also be scaled with the map.
*/
public final class LocationBasedService extends PlaySVGImageDemo implements Runnable {
/**
* The image that holds the animated SVG content.
*/
private static final String SVG_IMAGE = "/svg/locationBasedService.svg";
/**
* The image that holds the splash screen image.
*/
private static final String SPLASH_IMAGE = "/images/LocationBasedHelp.png";
Object[][] points =
new Object[][] {
{ new float[] { 14f, 60f }, "Washington and Battery" },
{ new float[] { 14f, 146f }, "Left on Sacramento" },
{ new float[] { 153f, 146f }, "Right on Drumm" },
{ new float[] { 153f, 204f }, "Left on Market" },
{ new float[] { 229f, 151f }, "Right on Stewart" },
{ new float[] { 278f, 219f }, "First left" },
{ new float[] { 312f, 194f }, "Left on Embarcadero" },
{ new float[] { 275f, 141f }, "You made it!" }
};
/**
* Location markers
*/
SVGElement[] markers = new SVGElement[points.length];
/**
* Marker connectors
*/
SVGElement[] connectors = new SVGElement[points.length - 1];
/**
* Animation steps
*/
Runnable initialDisplay = new InitialDisplay();
Runnable locationBasedServiceAnim = new LocationBasedServiceAnim();
Runnable finalDisplay = new FinalDisplay();
/**
* Message element
*/
SVGElement message;
/**
* Message location
*/
float messageX;
/**
* Message location
*/
float messageY;
/**
* Message background
*/
SVGElement messageBackground;
/**
* Color constant for markers and connectors
*/
// EXTENSION
// final String MARKER_COLOR = "red";
// REPLACE WITH
SVGRGBColor MARKER_COLOR = null;
SVGRGBColor MARKER_BLACK = null;
SVGRGBColor MARKER_WHITE = null;
/**
* Current Animation
*/
Runnable step = initialDisplay;
/**
* Initial viewbox
*/
SVGRect initialVB;
/**
* Initial screen bbox
*/
SVGRect initialBBox;
/**
* Controls whether the animation is paused or not
*/
boolean paused = true;
/**
* Default constructor
*/
public LocationBasedService() {
super(SVG_IMAGE, SPLASH_IMAGE, false);
}
/**
* By default, start the demo in the playing state.
*/
public void startApp() {
super.startApp();
init(svgImage.getDocument());
}
/**
* @param doc the DocumentNode holding the expected SVG content.
*/
public void init(final Document doc) {
svg = (SVGSVGElement)doc.getDocumentElement();
// EXTENSION
MARKER_COLOR = svg.createSVGRGBColor(255,0,0);
MARKER_BLACK = svg.createSVGRGBColor(0,0,0);
MARKER_WHITE = svg.createSVGRGBColor(255,255,255);
final SVGElement inserts = (SVGElement)doc.getElementById("inserts");
//
// Add markers for each of the locationBasedService points
//
final SVGElement markersGroup = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "g");
markersGroup.setTrait("visibility", "visible");
for (int i = 0; i < points.length; i++) {
Object[] point = points[i];
float[] coord = (float[])point[0];
SVGElement marker = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "ellipse");
marker.setFloatTrait("cx", coord[0]);
marker.setFloatTrait("cy", coord[1]);
marker.setFloatTrait("rx", 5);
marker.setFloatTrait("ry", 5);
// EXTENSION
// marker.setTrait("fill", MARKER_COLOR);
// REPLACE WITH
marker.setRGBColorTrait("fill", MARKER_COLOR);
markersGroup.appendChild(marker);
markers[i] = marker;
}
//
// Add connector lines for each of the locationBasedService points
// except the last.
//
final SVGElement connectorsGroup = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "g");
connectorsGroup.setTrait("fill", "none");
// EXTENSION
// connectorsGroup.setTrait("stroke", MARKER_COLOR);
// REPLACE WITH
connectorsGroup.setRGBColorTrait("stroke", MARKER_COLOR);
connectorsGroup.setFloatTrait("stroke-width", 2.5f);
for (int i = 0; i < (points.length - 1); i++) {
connectors[i] = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "line");
connectors[i].setTrait("visibility", "hidden");
connectors[i].setFloatTrait("x1", markers[i].getFloatTrait("cx"));
connectors[i].setFloatTrait("y1", markers[i].getFloatTrait("cy"));
connectors[i].setFloatTrait("x2", markers[i].getFloatTrait("cx"));
connectors[i].setFloatTrait("y2", markers[i].getFloatTrait("cy"));
connectorsGroup.appendChild(connectors[i]);
}
//
// Message
//
message = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "text");
message.setTrait("text-anchor", "middle");
// EXTENSION
// message.setTrait("fill", "black");
// REPLACE WITH
message.setRGBColorTrait("fill", MARKER_BLACK);
// EXTENSION
// message.setTrait("fill-opacity", "0.5");
// message.setTrait("font-family", "SunSansDemiEmbeded");
messageBackground = (SVGElement)doc.createElementNS(SVG_NAMESPACE_URI, "rect");
// EXTENSION
// messageBackground.setTrait("fill", "white");
// REPLACE WITH
messageBackground.setRGBColorTrait("fill", MARKER_WHITE);
// EXTENSION
// messageBackground.setFloatTrait("fill-opacity", 0.75f);
//
// Remember the initial viewbox & screenBBox
//
initialVB = svg.getRectTrait("viewBox");
initialBBox = svg.getScreenBBox();
inserts.appendChild(connectorsGroup);
inserts.appendChild(markersGroup);
svg.appendChild(messageBackground);
svg.appendChild(message);
Thread th =
new Thread() {
public void run() {
while (true) {
try {
if (state == STATE_PLAYING) {
if (!paused) {
svgAnimator.invokeAndWait(LocationBasedService.this);
}
}
sleep(200);
} catch (InterruptedException ie) {
break;
}
}
}
};
th.start();
}
public void keyPressed(int keyCode) {
if (keyCode == KEY_START_DEMO) {
if (state != STATE_PLAYING) {
svgAnimator.play();
state = STATE_PLAYING;
}
paused = !paused;
} else if (keyCode == KEY_ROTATE) {
if (state == STATE_PLAYING) {
svgAnimator.invokeLater(new Runnable() {
public void run() {
if (svg == null) {
System.err.println("svg is null!!!!");
}
if (svg.getCurrentRotate() == 0) {
// Rotate by 90 degrees counter clock wise
svg.setCurrentRotate(-90);
// Scale by the height / width factor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -