📄 lscreen.java
字号:
package com.cuit.lui;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class LScreen extends Canvas implements Runnable {
public final static int TOP = Graphics.TOP;
public final static int LEFT = Graphics.LEFT;
public final static int RIGHT = Graphics.RIGHT;
public final static int BOTTOM = Graphics.BOTTOM;
public final static int HCENTER = Graphics.HCENTER;
public final static int VCENTER = Graphics.VCENTER;
public final static int TOP_RIGHT = Graphics.TOP | Graphics.RIGHT;
public final static int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
public final static int BOTTOM_LEFT = Graphics.BOTTOM | Graphics.LEFT;
public final static int BOTTOM_RIGHT = Graphics.BOTTOM | Graphics.RIGHT;
public final static int HVCENTER = Graphics.HCENTER | Graphics.VCENTER;
private static boolean console = false;
private static boolean warning = false;
private static boolean error = false;
private boolean firstDisplay = true;
private static Hashtable keyHash = new Hashtable();
public static Font wrapTextFont = null;
public static int screenWidth, screenHeight;
private int realScreenx, realScreeny;
private int clockStep = LUIConfig.DEFAULT_CLOCKSTEP;
private boolean isAlive = true;
private boolean isAnimated = false;
private int switchStep = 45;
public Panel panel;
private Display display = null;
private Image offScreen;
private Graphics g, offg;
private static LScreen singleton = null;
private Row alert;
private int panelSwitchDir = LUIConfig.DEFAULT_PANELSWITCH_ANIM_DIR;
private boolean alertMode, busyMode;
private LScreen(Display display) {
setFullScreenMode(true); // /////must place it in first sentence!!!
this.display = display;
initialLScreen();
setupKeys();
setupFont();
Thread t = new Thread(this);
t.start();
// display.callSerially(this);
// console("call serially");
}
public static LScreen getScreen(Display display) {
if (display != null && singleton == null) {
singleton = new LScreen(display);
}
return singleton;
}
private void initialLScreen() {
panel = null;
screenWidth = getWidth();
screenHeight = getHeight();
offScreen = Image.createImage(screenWidth, screenHeight);
offg = offScreen.getGraphics();
}
public void setCurrentPanel(Panel p) {
if (panel != p) {
if (panel == null) {
isAnimated = false;
panel = p;
} else {
panel = p;
panel.reset();
if (panelSwitchDir == LEFT) {
realScreenx = screenWidth;
realScreeny = 0;
} else if (panelSwitchDir == RIGHT) {
realScreenx = -screenWidth;
realScreeny = 0;
}
isAnimated = true;
System.out.println(realScreenx + "------" + realScreeny);
}
console("panel set current");
} else {
console("panel is on display already");
}
repaint();
}
protected void paint(Graphics arg0) {
if (g != arg0)
g = arg0;
if (offg != null) {
offg.setColor(0xffffff);
offg.fillRect(0, 0, screenWidth, screenHeight);
}
if (panel != null) {
if (offg == null) {
warning("offscreen graphic can't be null");
}
panel.paint(offg);
console("call panel paint");
if (alertMode) {
drawAlert(offg);
}
} else {
warning("panel can't be null!");
}
finalPaint();
}
private void finalPaint() {
g.drawImage(offScreen, realScreenx, realScreeny, 0);
}
public void run() {
console("thread started");
while (isAlive) {
clock();
try {
Thread.sleep(clockStep);
} catch (Exception e) {
error("thread sleep error!");
}
console("tictac...");
if (firstDisplay) {
setupDisplay();
repaint();
}
if (isAnimated || alertMode) {
repaint();
}
// display.callSerially(this);
console("thread call serially");
}
}
public void clock() {
if (isAnimated) {
switch (panelSwitchDir) {
case LEFT:
realScreenx -= switchStep;
if (realScreenx <= 0) {
realScreenx = 0;
isAnimated = false;
}
break;
case RIGHT:
realScreenx += switchStep;
if (realScreenx >= 0) {
realScreenx = 0;
isAnimated = false;
}
break;
default:
break;
}
} else {
console("animation over");
}
}
public void keyPressed(int keyCode) {
// System.out.println("keycode is:"+keyCode);
// System.out.println("key name: "+this.getKeyName(keyCode));
// int k=getGameAction(keyCode);
int k = transformKey(keyCode);
keyProcess(k);
}
public void keyReleased(int keyCode) {
}
public void pointerPressed(int x, int y) {
}
public void pointerReleased(int x, int y) {
}
private void keyProcess(int keyCode) {
if (panel != null) {
if (alertMode) {
alertMode = !alertMode;
repaint();
return;
}
boolean b = panel.keyEvent(keyCode);
if (panel.rowPointer == 4) {
}
if (b) {
repaint();
}
} else {
warning("no panel in LScreen!");
return;
}
}
public void showPopup() {
}
public void closePopup() {
}
public void showShortcut() {
}
public void closeShortcut() {
}
public void setCurrent(Displayable d) {
if (display != null) {
display.setCurrent(d);
} else {
error("LScreen display is null!");
}
}
public Display getDisplay() {
return display;
}
private static void setupKeys() {
console("seting up keys ");
keyHash.put(new Integer(-6), new Integer(LUIConfig.LEFT_SOFTKEY));
keyHash.put(new Integer(-7), new Integer(LUIConfig.RIGHT_SOFTKEY));
keyHash.put(new Integer(-1), new Integer(LUIConfig.UP));
keyHash.put(new Integer(-2), new Integer(LUIConfig.DOWN));
keyHash.put(new Integer(-3), new Integer(LUIConfig.LEFT));
keyHash.put(new Integer(-4), new Integer(LUIConfig.RIGHT));
keyHash.put(new Integer(-5), new Integer(LUIConfig.FIRE));
keyHash.put(new Integer(35), new Integer(LUIConfig.POUND));
}
private static void setupFont() {
wrapTextFont = LUIConfig.DEFAULT_WRAP_FONT;
}
public static int transformKey(int k) {
console("transform: " + k);
Integer i = (Integer) keyHash.get(new Integer(k));
if (i == null)
warning("no map value found in keyHash!");
return i.intValue();
}
public static void setWrapFont(Font f) {
wrapTextFont = f;
}
public void setGraphics(Graphics g) {
this.g = g;
}
public Graphics getGraphics() {
return g;
}
public void setSwitchDirection(int dir) {
if (dir != LEFT && dir != RIGHT) {
error("panel switch direction parameter invalid");
} else {
panelSwitchDir = dir;
}
}
public static Vector wrapText(String s, int width) {
return wrapText(s, width, wrapTextFont);
}
public static Vector wrapText(String s, int width, Font f) {
Vector stringContainer = new Vector();
wrapTextFont = f;
if (width <= 0) {
warning("wrap width <=0!");
return null;
}
int wrapWidth = width;
int pointer = 0;
int spacePointer = 0;
if (s == null || s == "") {
warning("wrap string is null!");
return null;
}
char[] text = s.toCharArray();
int w = 0;
char c;
int currLinePointer = 0;
while (pointer < text.length) {
c = text[pointer];
if (c == ' ') {
spacePointer = pointer;
}
w += wrapTextFont.charWidth(c);
if (w >= wrapWidth) {
if (spacePointer > currLinePointer) {
stringContainer.addElement(s.substring(currLinePointer,
spacePointer));
currLinePointer = spacePointer + 1;
pointer = spacePointer;
} else {
stringContainer.addElement(s.substring(currLinePointer,
pointer));
currLinePointer = pointer + 1;
}
w = 0;
}
pointer++;
}
if (pointer >= currLinePointer)
stringContainer.addElement(s.substring(currLinePointer, pointer));
return stringContainer;
}
public static void console(String s) {
if (console)
System.out.println(s);
else
return;
}
public static void warning(String s) {
if (warning)
System.out.println("warning: " + s);
else
return;
}
public static void error(String s) {
if (error)
System.out.println("error: " + s);
else
return;
}
public void showAlert(Image imgpart, String strpart) {
alertMode = true;
if (alert == null) {
setupAlert(imgpart, strpart);
}
}
private void setupAlert(Image imgpart, String strpart) {
alert = new Row(imgpart, strpart, 150);
alert.setAlert(true);
System.out.println("width: " + alert.getWidth());
int alertposx = (screenWidth - alert.getWidth()) / 2;
int alertposy = (screenHeight - alert.getHeight()) / 2;
System.out.println("alertposx " + alertposx + "alertposy "
+ alertposy);
alert.setPosx(alertposx);
alert.setPosy(alertposy);
}
private void drawAlert(Graphics g) {
if (!alertMode)
return;
g.setColor(186, 184, 192);
g.setClip(alert.getPosx() - 5, alert.getPosy() - 5,
alert.getWidth() + 10, alert.getHeight() + 10);
g.fillRoundRect(alert.getPosx() - 5, alert.getPosy() - 5, alert
.getWidth() + 10, alert.getHeight() + 10, 10, 10);
alert.paint(g);
g.setClip(0, 0, screenWidth, screenHeight);
}
private void setupDisplay() {
try {
if (panel != null) {
for (int i = 0; i < panel.rows.size(); i++) {
Component c = (Component) panel.rows.elementAt(i);
c.needRepaint = true;
}
firstDisplay = false;
}
} catch (Exception e) {
System.out.println("got it");
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -