📄 menu.java
字号:
package no.auc.one.portableplayer.userinterface;
import java.util.*;
import javax.microedition.lcdui.*;
import org.apache.log4j.Logger;
public class Menu extends Canvas {
private static Logger LOG = Logger.getLogger("MENU");
/**
* Specifies the height of each MenuElementBase.
*/
public static final int menuElementHeight = 18;
private int maxCurrentMenuElements = 0;
private int topIndex = 0;
private int[] colorBackground = null;
private int[] colorMarker = null;
private MenuElementContainer currentContainer = null;
Vector menuEventListeners = null;
int lastX = -1;
int lastY = -1;
String lastAction = "";
public Menu () {
System.out.println("Menu ctor");
colorBackground = new int[]{135, 206, 235};
colorMarker = new int[]{155, 226, 255};
maxCurrentMenuElements = (int)Math.floor(
(double)getHeight() / (double)menuElementHeight) - 1;
LOG.debug("Max Curr ME: " + maxCurrentMenuElements);
LOG.debug("Supports repeat? " + hasRepeatEvents());
LOG.debug("Supports pointer press/release? " + hasPointerEvents());
LOG.debug("Supports pointer motion? " + hasPointerMotionEvents());
}
public void setCurrentContainer(MenuElementContainer container) {
currentContainer = container;
repaint();
}
public MenuElementContainer getCurrentContainer() {
return currentContainer;
}
public int getTopIndex() {
return topIndex;
}
public int getMaxCurrentMenuElements() {
return maxCurrentMenuElements;
}
protected void paint(Graphics g) {
g.setColor(colorBackground[0], colorBackground[1], colorBackground[2]);
g.fillRect(0, 0, g.getClipWidth(), g.getClipHeight());
g.setColor(colorMarker[0], colorMarker[1],colorMarker[2]);
g.fillRoundRect(
5,
((currentContainer.getMarkerPosition() + 1) * 18) - 10,
g.getClipWidth() - 10,
20,
5,
5);
g.setColor(0, 0, 0);
int oldX = g.getClipX();
int oldY = g.getClipY();
int oldHeight = g.getClipHeight();
int oldWidth = g.getClipWidth();
g.setClip(
10,
10,
oldWidth - 10,
maxCurrentMenuElements * menuElementHeight);
currentContainer.paintChildren(g);
/*
for (int i = listMarker; i < (listMarker + menuElementLimiter); i++) {
g.setClip(
10,
10 + (i * menuElementHeight),
oldWidth - 10,
menuElementHeight);
currentMenuElements[i].paint(g);
}
*/
g.setClip(oldX, oldY, oldHeight, oldWidth);
/* XXX Info for debugging pointer action
g.drawString(
"Last: " + lastAction + "(" + lastX + ", " + lastY + ")",
0,
oldHeight - 50,
Graphics.LEFT | Graphics.BOTTOM);
*/
}
protected void keyPressed (int keyCode) {
// System.out.println("Key pressed: " + keyCode);
//MenuElement me = currentMenuElements[markerPosition+listMarker];
//
// Action invocation
//
if (keyCode == KEY_NUM5 || getGameAction(keyCode) == FIRE) {
currentContainer.invokeAction();
//
// Navigate Right
//
} else if (keyCode == KEY_NUM6 || getGameAction(keyCode) == RIGHT) {
currentContainer.navigateRight();
//
// Navigate Left
//
} else if (keyCode == KEY_NUM4 || getGameAction(keyCode) == LEFT) {
currentContainer.navigateLeft();
//
// Navigate Up
//
} else if (keyCode == KEY_NUM2 || getGameAction(keyCode) == UP) {
currentContainer.navigateUp();
//
// Navigate Down
//
} else if (keyCode == KEY_NUM8 || getGameAction(keyCode) == DOWN) {
currentContainer.navigateDown();
}
repaint();
}
protected void keyReleased (int keyCode) {
// System.out.println("Key released: " + keyCode);
}
// TODO Should support this in the future, for easier navigation
protected void keyRepeated (int keyCode) {
System.out.println("*** Key repeated: " + keyCode + "***");
}
/**
* If the user presses the pointer for a long time (2 sec?) then
* display a context menu.
*/
protected void pointerPressed(int x, int y) {
long pointerPressStart = java.lang.System.currentTimeMillis();
System.out.println(
"Pointer pressed! (" + pointerPressStart +
") (" + x + ", " + y + ")");
//lastX = x;
//lastY = y;
repaint();
}
/*
protected void pointerDragged(int x, int y) {
//System.out.println("Pointer dragged!");
}
*/
protected void pointerReleased(int x, int y) {
// if (pointerPressStart >= someAmount) { }
// Detect which MenuElement was hit
// Only the y-axis is important
// Each MenuElement should be 20 pixels high, and be positioned
// at ((index + 1) * 18) - 10 pixel.
int index = (int)(y / 20.0);
lastAction = "ix=" + index;
if (index >= currentContainer.childrenLength()) {
return;
}
/*
for double clicking...
MenuElement me = currentMenuElements[index];
int ret = me.menuAction();
handleActionOrIntoReturnCode(ret, me);
*/
lastX = x;
lastY = y;
repaint();
}
protected void sizeChanged(int w, int h) {
LOG.debug("Size changed (" + w + ", " + h + ")");
maxCurrentMenuElements = (int)Math.floor(
(double)h / (double)menuElementHeight) - 1;
LOG.debug("New height: " + h);
LOG.debug("Max Curr ME: " + maxCurrentMenuElements);
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -