📄 browsercanvas.java
字号:
import javax.microedition.lcdui.*;
import java.util.*;
public class BrowserCanvas extends Canvas implements CommandListener, Receiver {
static final int BLINK_PERIOD = 1000;
static final int REPEAT_DELAY = 1000;
static final int REPEAT_PERIOD = 100;
static final int BACKGROUND_COLOR = 0xffffff;
static final int SELECTION_COLOR = 0x800080;
static final int FOCUS_COLOR = 0x000000;
static final int EPSILON = 10;
int x, y;
int focusX, focusY;
int focusWidth, focusHeight;
int focusX0, focusY0;
int screenWidth, screenHeight;
int selectionColor = SELECTION_COLOR;
MapBrowser browser;
int level;
int scale;
int zoom;
int imageSize;
boolean busy;
int skipImages;
Image[] imageCache;
String[] imageUrl;
int imageCachePtr;
int stepX, stepY;
int focusStepX, focusStepY;
Timer repeatTimer = new Timer();
Timer blinkTimer = new Timer();
RepeatTask repeatTask;
BlinkTask blinkTask;
int cacheSize;
Location[] selection;
Index selectionIndex;
int currSelection;
String buildImageUrl(int x, int y) {
return browser.url + level + '/' + browser.city + y + '_' + x + ".png";
}
Image getImage(int x, int y) {
String url = buildImageUrl(x, y);
for (int i = 0; i < cacheSize; i++) {
if (url.equals(imageUrl[i])) {
return imageCache[i];
}
}
return null;
}
void startImageLoading(int x, int y) {
busy = true;
new InternetConnection(buildImageUrl(x, y), this);
}
public void failure(String url) {
System.out.println("Failed to load image " + url);
// try once again
new InternetConnection(url, this);
}
public void received(String url, byte[] data, int len) {
// System.out.println("Receive image " + url);
Image image = Image.createImage(data, 0, len);
imageCache[imageCachePtr] = image;
imageUrl[imageCachePtr] = url;
imageCachePtr = (imageCachePtr + 1) % cacheSize;
repaint();
}
void setPositions(Index index, Location[] locations) {
selection = locations;
selectionIndex = index;
currSelection = -1;
if (locations.length == 1) {
Location l = locations[0];
if ((browser.settings.flags & Settings.AUTO_ZOOM) != 0) {
for (scale = 1, level = browser.levels;
(l.getWidth() > screenWidth*scale || l.getHeight() > screenHeight*scale)
&& level > 1;
level -= 1, scale *= zoom);
if (level != 1) {
removeCommand(MapBrowser.ZOOM_OUT_CMD);
}
if (level != browser.levels) {
addCommand(MapBrowser.ZOOM_IN_CMD);
}
setFocus();
}
x = l.getCenterX() - screenWidth*scale/2;
y = l.getCenterY() - screenHeight*scale/2;
}
if (locations.length > 0) {
if (cacheSize >= 4 && (browser.settings.flags & Settings.BLINKING) != 0) {
startBlinking();
}
if (index.type == Index.POINT) {
addCommand(MapBrowser.NEXT_CMD);
}
addCommand(MapBrowser.CLEAN_CMD);
}
check();
}
void startBlinking() {
blinkTask = new BlinkTask();
blinkTimer.schedule(blinkTask, BLINK_PERIOD, BLINK_PERIOD);
}
void stopBlinking() {
if (blinkTask != null) {
blinkTask.cancel();
blinkTask = null;
}
}
void setFocus() {
focusX0 = screenWidth*(scale-1)/2;
focusY0 = screenHeight*(scale-1)/2;
focusX = x + focusX0;
focusY = y + focusY0;
}
BrowserCanvas(MapBrowser browser) {
setFullScreenMode(true);
this.browser = browser;
setCommandListener(this);
addCommand(MapBrowser.INDEX_CMD);
addCommand(MapBrowser.SETTINGS_CMD);
addCommand(MapBrowser.HELP_CMD);
addCommand(MapBrowser.ABOUT_CMD);
addCommand(MapBrowser.QUIT_CMD);
cacheSize = browser.settings.cacheSize;
imageCache = new Image[cacheSize];
imageUrl = new String[cacheSize];
screenWidth = getWidth();
screenHeight = getHeight();
stepX = screenWidth/4;
stepY = screenHeight/4;
zoom = browser.zoom;
scale = 1;
x = 0;
y = 0;
level = 1;
focusWidth = screenWidth/zoom;
focusHeight = screenHeight/zoom;
focusStepX = focusWidth/zoom;
focusStepY = focusHeight/zoom;
for (int i = browser.levels; --i != 0;) {
scale *= zoom;
}
if (browser.levels > 1) {
addCommand(MapBrowser.ZOOM_IN_CMD);
}
imageSize = browser.cell;
setFocus();
check();
Display.getDisplay(browser).setCurrent(this);
}
protected void paint(Graphics g) {
if (skipImages == 0 && level == 1) {
g.setColor(BACKGROUND_COLOR);
g.fillRect(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight());
}
//System.out.println("Repaint xc=" + g.getClipX() + ", yc=" + g.getClipY() + ", width=" + g.getClipWidth() + ", height=" + g.getClipHeight() + ", x=" + x + ", y=" + y + ", scale=" + scale);
//System.out.println("Draw x=" + x + ", y=" + y + ", focusX = " + focusX + ", focusY=" + focusY + ", scale=" + scale);
int x0 = x/scale + g.getClipX();
int y0 = y/scale + g.getClipY();
int x1 = x0 + g.getClipWidth() - 1;
int y1 = y0 + g.getClipHeight() - 1;
if (x1 >= browser.width/scale) {
x1 = browser.width/scale-1;
}
if (y1 >= browser.width/scale) {
y1 = browser.width/scale-1;
}
int n = 0;
for (int i = x0/imageSize; i <= x1/imageSize; i++) {
for (int j = y0/imageSize; j <= y1/imageSize; j++) {
if (++n >= skipImages) {
Image image = getImage(i, j);
if (image != null) {
// System.out.println("Draw image j=" + j + ", i=" + i + " at x=" + (i*imageSize-x/scale) + ", y=" + (j*imageSize-y/scale));
g.drawImage(image, i*imageSize-x/scale, j*imageSize-y/scale, Graphics.TOP|Graphics.LEFT);
} else {
if (n > skipImages) {
skipImages = n;
startImageLoading(i, j);
}
return;
}
}
}
}
busy = false;
skipImages = 0;
if (level != browser.levels) {
g.setColor(FOCUS_COLOR);
g.setStrokeStyle(Graphics.DOTTED);
g.drawRect((focusX-x)/scale, (focusY-y)/scale, focusWidth, focusHeight);
}
if (selection != null) {
g.setStrokeStyle(Graphics.DOTTED);
g.setColor(selectionColor);
for (int i = 0; i < selection.length; i++) {
Location loc = selection[i];
switch (selectionIndex.type) {
case Index.POINT:
g.drawLine((loc.points[0]-x)/scale-EPSILON, (loc.points[1]-y)/scale,
(loc.points[0]-x)/scale+EPSILON, (loc.points[1]-y)/scale);
g.drawLine((loc.points[0]-x)/scale, (loc.points[1]-y)/scale-EPSILON,
(loc.points[0]-x)/scale, (loc.points[1]-y)/scale+EPSILON);
if (i == currSelection) {
g.drawArc((loc.points[0]-x)/scale-EPSILON, (loc.points[1]-y)/scale-EPSILON,
EPSILON*2, EPSILON*2, 0, 360);
}
break;
case Index.RECTANGLE:
g.drawRect((loc.points[0]-x)/scale, (loc.points[1]-y)/scale,
(loc.points[2]-loc.points[0])/scale+1,
(loc.points[3]-loc.points[1])/scale+1);
break;
case Index.LINE:
for (int j = 2; j < loc.points.length; j += 2) {
g.drawLine((loc.points[j-2]-x)/scale, (loc.points[j-1]-y)/scale,
(loc.points[j]-x)/scale, (loc.points[j+1]-y)/scale);
}
break;
}
}
}
}
boolean zoomIn() {
if (scale == 1) {
return false;
}
level += 1;
scale /= zoom;
x = focusX;
y = focusY;
addCommand(MapBrowser.ZOOM_OUT_CMD);
if (scale == 1) {
removeCommand(MapBrowser.ZOOM_IN_CMD);
} else {
setFocus();
check();
}
return true;
}
boolean zoomOut() {
if (level == 1) {
return false;
}
scale *= zoom;
level -= 1;
focusX = x;
focusY = y;
focusX0 = screenWidth*(scale-1)/2;
focusY0 = screenHeight*(scale-1)/2;
x -= focusX0;
y -= focusY0;
addCommand(MapBrowser.ZOOM_IN_CMD);
if (level == 1) {
removeCommand(MapBrowser.ZOOM_OUT_CMD);
}
return true;
}
public void commandAction(Command c, Displayable d)
{
if (c == MapBrowser.QUIT_CMD) {
browser.quit();
} else if (c == MapBrowser.ABOUT_CMD) {
new HelpForm(browser, Locale.current.getResource("About"), Locale.current.getResource("AboutText"), this);
} else if (c == MapBrowser.HELP_CMD) {
new HelpForm(browser, Locale.current.getResource("Help"), Locale.current.getResource("HelpText"), this);
} else if (c == MapBrowser.ZOOM_IN_CMD) {
zoomIn();
check();
} else if (c == MapBrowser.ZOOM_OUT_CMD) {
zoomOut();
check();
} else if (c == MapBrowser.INDEX_CMD) {
new GroupList(this);
} else if (c == MapBrowser.NEXT_CMD) {
nextPoint();
repaint();
} else if (c == MapBrowser.INFO_CMD) {
pointInfo();
} else if (c == MapBrowser.CLEAN_CMD) {
selection = null;
stopBlinking();
removeCommand(MapBrowser.CLEAN_CMD);
removeCommand(MapBrowser.NEXT_CMD);
removeCommand(MapBrowser.INFO_CMD);
} else if (c == MapBrowser.SETTINGS_CMD) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -