📄 cityguidemidlet.java
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package examples.cityguide;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import java.util.Vector;import javax.microedition.lcdui.*;import javax.microedition.lcdui.List;import javax.microedition.location.AddressInfo;import javax.microedition.location.Coordinates;import javax.microedition.location.Criteria;import javax.microedition.location.Landmark;import javax.microedition.location.LandmarkStore;import javax.microedition.location.LocationException;import javax.microedition.location.LocationProvider;import javax.microedition.location.QualifiedCoordinates;import javax.microedition.midlet.*;/** * Main CityGuide MIDlet */public class CityGuideMIDlet extends MIDlet implements CommandListener { /** landmark store name to store points of interest */ private static final String LANDMARKSTORE_NAME = "waypoints"; /** welcome text for welcome screen */ private static final String WELCOME_TEXT = "Welcome to The City Guide. First JSR179 enabled city guide." + " Select your favorite landmark categories and never miss interesting place while walking through the city." + " Don't forget to run the citywalk.xml script.\n\n" + "Enjoy the show!\n\n"; /** explanation label for settings screen */ private static final String SETTINGS_TEXT = "The City Guide will alert you whenever you get close to landmark of selected category."; /** choice group label for categories */ private static final String CHOICEGRP_TEXT = "Watch for categories:"; /** no landmark in proximity */ private static final String NOLANDMARK_TEXT = "Sorry no landmark in proximity now.\nKeep on walking."; /** map corners coordinates */ private static final Coordinates MAP_TOP_LEFT_COORDINATES = new Coordinates(14.387594174164514, 50.1049422484619, 310); private static final Coordinates MAP_BOTTOM_RIGHT_COORDINATES = new Coordinates(14.40423976700292, 50.09531507039965, 310); /** initial visitor coordinates */ private static final Coordinates MAP_VISITOR_COORDINATES = new Coordinates(14.389796708964603, 50.09985002736201, 310); private static final String[] PRELOAD_IMAGES = { "map", "visitoron", "visitoroff", "anim1", "anim2", "anim3", "anim4", "anim5", "anim6", "anim7", "anim8", "logo" }; private static CityGuideMIDlet instance; /** error screen uses last error occurred */ private String lastError = ""; /** list of selected categories */ private Vector selectedCategories; /** landmark store containing points of interest */ private LandmarkStore landmarkStore = null; /** location provider */ private LocationProvider locationProvider = null; /** distance when proximity event is sent */ private float proximityRadius = 50.0f; /** image cache */ private ImageManager imageManager = null; /** landmarks successfully loaded */ private boolean landmarksLoaded = false; /** main MIDlet display */ private Display display; /** map canvas */ private MapCanvas mapCanvas; /** city map */ private CityMap cityMap; /** Screens and commands */ private Command WelcomeScreen_nextCommand; private Command ProgressScreen_nextCommand; private Command showErrorCommand; private Command SettingsScreen_backCommand; private Command DetailsScreen_backCommand; private Command exitCommand; private Command closeCommand; private final Command detailsCommand; private final Command settingsCommand; private Gauge progressGauge; private Form progressScreen; private Form welcomeScreen; private Form settingsScreen; private Form detailsScreen; private Alert errorAlert; /** * Main City Guide MIDlet */ public CityGuideMIDlet() { exitCommand = new Command("Exit", Command.EXIT, 1); closeCommand = new Command("Close", Command.SCREEN, 1); showErrorCommand = new Command("Error", Command.SCREEN, 1); detailsCommand = new Command("Detail", Command.SCREEN, 1); settingsCommand = new Command("Settings", Command.SCREEN, 1); display = Display.getDisplay(this); imageManager = ImageManager.getInstance(); imageManager.getImage("logo"); createLocationProvider(); if (locationProvider == null) { System.out.println("Cannot run without location provider!"); destroyApp(false); notifyDestroyed(); } instance = this; } public static CityGuideMIDlet getInstance() { if (instance == null) { instance = new CityGuideMIDlet(); } return instance; } public void startApp() { if (display.getCurrent() == null) { // startApp called for the first time showWelcomeScreen(); } else { if (cityMap != null) { cityMap.enable(); } } } public void pauseApp() { if (cityMap != null) { cityMap.disable(); } } public void destroyApp(boolean unconditional) { if (cityMap != null) { cityMap.cleanup(); } } /** * Action handler */ public void commandAction(Command c, Displayable s) { if (c == detailsCommand) { showDetailsScreen(); } if (c == settingsCommand) { showSettingsScreen(true); } if (c == WelcomeScreen_nextCommand) { if (landmarkStore == null) { showProgressScreen("Loading landmarks ...", 20); } //load landmarks from resources Thread loadLandmarksThread = new Thread() { public void run() { loadLandmarks(); } }; loadLandmarksThread.start(); //load images from resources Thread loadImagesThread = new Thread() { public void run() { loadImages(); } }; loadImagesThread.start(); } if (c == ProgressScreen_nextCommand) { //initialize categories showSettingsScreen(false); //update map display with changed set of selected categories selectedCategoriesChanged(); //initialize map display showMapCanvas(true); } if (c == SettingsScreen_backCommand) { selectedCategoriesChanged(); showMapCanvas(true); } if (c == DetailsScreen_backCommand) { showMapCanvas(false); } if (c == showErrorCommand) { showErrorForm(lastError); } if (c == closeCommand) { if (display.getCurrent() == errorAlert) { display.setCurrent(welcomeScreen); } } if (c == exitCommand) { if ((display.getCurrent() == settingsScreen) || (display.getCurrent() == mapCanvas)) { display.setCurrent(welcomeScreen); } if (display.getCurrent() == welcomeScreen) { destroyApp(false); notifyDestroyed(); } } } /** * Initializes LocationProvider * uses default criteria */ void createLocationProvider() { if (locationProvider == null) { Criteria criteria = new Criteria(); try { locationProvider = LocationProvider.getInstance(criteria); } catch (LocationException le) { System.out.println("Cannot create LocationProvider for this criteria."); le.printStackTrace(); } } } private void generateAndDisplayMap() { if (cityMap == null) { try { cityMap = new CityMap(new String[] { "map", "visitoron", "visitoroff" }, MAP_TOP_LEFT_COORDINATES, MAP_BOTTOM_RIGHT_COORDINATES, MAP_VISITOR_COORDINATES, selectedCategories, ImageManager.getInstance(), LandmarkStore.getInstance(LANDMARKSTORE_NAME), LocationProvider.getInstance(new Criteria()));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -