📄 fullcanvas.java
字号:
/*
* @(#)FullCanvas.java
*
* version: 1.1
* created: 4/03/2003
* modified: 20/03/2003
*/
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
public abstract class FullCanvas extends com.nokia.mid.ui.FullCanvas {
/*
* canvas font
*/
static Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
/*
* some constant to customize the painted softkeys. can be modified anytime.
*/
static int SOFTKEY_HEIGHT = 20;
static int SOFTKEY_TOPLINE = getColorOfRGB(0,0,0);
static int SOFTKEY_BACKGROUND = getColorOfRGB(255,255,255);
static int SOFTKEY_FOREGROUND = getColorOfRGB(0,0,0);
static int SOFTKEY_TEXTAREA = getColorOfRGB(0,0,0);
static int AUTO_HEIGHT_TIME = 5000;
static boolean AUTO_HEIGHT = false;
static int AUTO_HEIGHT_LEVEL = SOFTKEY_HEIGHT;
/*
* workaround for emulator bug.
*/
static final boolean emulator = false;
/*
* key constants
*/
static final int KEY_PRESSED_EVENT = 0;
static final int KEY_RELEASED_EVENT = 1;
static final int KEY_0 = 0x00;
static final int KEY_1 = 0x01;
static final int KEY_2 = 0x02;
static final int KEY_3 = 0x03;
static final int KEY_4 = 0x04;
static final int KEY_5 = 0x05;
static final int KEY_6 = 0x06;
static final int KEY_7 = 0x07;
static final int KEY_8 = 0x08;
static final int KEY_9 = 0x09;
static final int KEY_ASTERISK = 0x0a;
static final int KEY_POUND = 0x0b;
static final int KEY_LEFT = 0x10;
static final int KEY_UP = 0x11;
static final int KEY_RIGHT = 0x12;
static final int KEY_DOWN = 0x13;
static final int KEY_SELECT = 0x14;
static final int KEY_SOFT1 = 0x15;
static final int KEY_SOFT2 = 0x16;
public static final int SOFT_KEY_1 = 0;
public static final int SOFT_KEY_2 = 1;
/*
* place holders of Command objects.
*/
protected Command cmdLeft = null;
protected Command cmdRight = null;
/*
* key handling.
*/
private int keypadState;
private CommandListener listener = null;
private boolean callProcessEvent = true;
private int[] keyPressCount = new int[20]; // see getKeyIndex() also
/*
* paint softkeys.
*/
boolean paint_lock = false;
protected static Image buffer = null;
private long time = 0;
private int fadelevel = 1;
private int fadecount = 0;
private int left_margin = 5;
private int right_margin = 5;
/*
* icon type constants
*/
public static final int ICON_BACK = 0;
public static final int ICON_PAUSE = 1;
public static final int ICON_CONTINUE = 2;
public static final int ICON_RETRY = 3;
public static final int ICON_MENU = 4;
public static final int ICON_TITLE = 5;
public static final int ICON_QUIT = 6;
public FullCanvas() {
if (buffer == null) buffer = Image.createImage(getWidth(), getHeight());
}
public final void setSoftLabel(int key, String label) {
if (key == SOFT_KEY_1) {
if (label == null) {
cmdLeft = null;
} else if (cmdLeft == null) {
cmdLeft = new Command(label, Command.SCREEN, 1);
if(AUTO_HEIGHT) hideSoftKeys(AUTO_HEIGHT_TIME, AUTO_HEIGHT_LEVEL);
} else {
if (!cmdLeft.getLabel().equals(label)) {
cmdLeft = new Command(label, Command.SCREEN, 1);
if(AUTO_HEIGHT) hideSoftKeys(AUTO_HEIGHT_TIME, AUTO_HEIGHT_LEVEL);
}
}
} else if (key == SOFT_KEY_2) {
if (label == null) {
cmdRight = null;
} else if (cmdRight == null) {
cmdRight = new Command(label, Command.SCREEN, 2);
if(AUTO_HEIGHT) hideSoftKeys(AUTO_HEIGHT_TIME, AUTO_HEIGHT_LEVEL);
} else {
if (!cmdRight.getLabel().equals(label)) {
cmdRight = new Command(label, Command.SCREEN, 2);
if(AUTO_HEIGHT) hideSoftKeys(AUTO_HEIGHT_TIME, AUTO_HEIGHT_LEVEL);
}
}
}
repaint();
}
public final void setSoftLabels(String label1, String label2) {
setSoftLabel(SOFT_KEY_1, label1);
setSoftLabel(SOFT_KEY_2, label2);
}
public String[] getSoftLabels() {
String[] labels = new String[2];
if (cmdLeft != null) labels[0] = cmdLeft.getLabel();
if (cmdRight != null) labels[1] = cmdRight.getLabel();
return labels;
}
public final int getKeypadState() {
int result = keypadState;
// release the softkeys.
if ((keypadState & (1 << KEY_SOFT1)) != 0) {
keypadState &= ~(1 << KEY_SOFT1);
processEvent(KEY_RELEASED_EVENT, KEY_SOFT1);
}
if ((keypadState & (1 << KEY_SOFT2)) != 0) {
keypadState &= ~(1 << KEY_SOFT2);
processEvent(KEY_RELEASED_EVENT, KEY_SOFT2);
}
return result;
}
public final void resetKeypadState() {
keypadState = 0;
}
public final Graphics getGraphics() {
if (buffer == null) buffer = Image.createImage(getWidth(), getHeight());
Graphics g = buffer.getGraphics();
g.setFont(font);
return g;
}
public void paint(Graphics g) {
if(paint_lock) return;
paint_lock = true;
g.drawImage(buffer, 0, 0, Graphics.LEFT | Graphics.TOP);
if(cmdLeft != null || cmdRight != null) paintSoftKeys(g);
paint_lock = false;
}
public void paintSoftKeys(Graphics g) {
g.translate(0-g.getTranslateX(), 0-g.getTranslateY());
if (time != 0 && System.currentTimeMillis() > time) {
// fades it..
fadecount++;
if (fadecount > fadelevel) return;
}
// draw it.
int top = getHeight() - SOFTKEY_HEIGHT + ((fadecount*SOFTKEY_HEIGHT)/fadelevel);
// draw background.
g.setColor(SOFTKEY_BACKGROUND);
g.fillRect(0,top-1,getWidth(),SOFTKEY_HEIGHT+1);
// draw textarea background
int w;
g.setColor(SOFTKEY_TEXTAREA);
if(cmdLeft != null) {
w = font.stringWidth(cmdLeft.getLabel());
g.fillRect(0, top, w+left_margin*2, SOFTKEY_HEIGHT+1);
}
if(cmdRight != null) {
w = font.stringWidth(cmdRight.getLabel());
g.fillRect(getWidth()-w-right_margin*2, top, w+right_margin*2, SOFTKEY_HEIGHT+1);
}
// draw top line.
g.setColor(SOFTKEY_TOPLINE);
g.drawLine(0,top,getWidth(),top);
// draw softkeys.
paintSoftkeys(g, top);
}
public void paintSoftkeys(Graphics g, int top) {
g.setFont(font);
// set color.
g.setColor(SOFTKEY_FOREGROUND);
// draw left command.
if (cmdLeft != null) {
g.drawString(cmdLeft.getLabel(), left_margin, top+4, g.LEFT|g.TOP);
}
// draw right command.
if (cmdRight != null) {
g.drawString(cmdRight.getLabel(), getWidth()-right_margin, top+4, g.RIGHT|g.TOP);
}
}
public void setSoftButtonsMargin(int left, int right) {
left_margin = left;
right_margin = right;
}
public void paintSoftIcon(Graphics g, int icon, int color, int pos) {
int j, x = 0;
int y = getHeight() - SOFTKEY_HEIGHT/2 + ((fadecount*SOFTKEY_HEIGHT)/fadelevel);
if(pos == SOFT_KEY_1) x = left_margin - 5 - 8;
if(pos == SOFT_KEY_2) x = getWidth() - right_margin - g.getFont().stringWidth(cmdRight.getLabel()) - 5 - 8;
// set color and draw the icon
g.setColor(color);
switch(icon) {
case ICON_BACK:
for (j=0; j<4; j++) g.fillRect(x+2*j, y-1-j, 2, 2+2*j);
break;
case ICON_PAUSE:
g.fillRect(x, y-4, 3, 8);
g.fillRect(x+6,y-4, 3, 8);
break;
case ICON_CONTINUE:
for (j=0; j<4; j++) g.fillRect(x+2*j, y-4+j, 2, 9-j*2);
break;
case ICON_RETRY:
g.fillRect(x, y-4, 2, 8);
g.fillRect(x+3,y-4, 2, 8);
g.drawLine(x+5, y-3, x+5, y+2);
g.drawLine(x+6, y-2, x+6, y+1);
g.drawLine(x+7, y-1, x+7, y+0);
break;
case ICON_MENU:
for(j=0; j<2; j++) {
g.fillRect(x, y-4+5*j, 2, 2);
g.drawLine(x+5, y-3+5*j, x+9, y-3+5*j);
}
break;
case ICON_TITLE:
g.fillRect(x, y-4, 7, 7);
break;
case ICON_QUIT:
g.fillRect(x, y-4, 2, 2);
g.fillRect(x, y, 2, 2);
g.fillRect(x+2, y-2, 2, 2);
g.fillRect(x+4, y-4, 2, 2);
g.fillRect(x+4, y, 2, 2);
break;
}
}
public void showSoftKeys() {
time = 0;
fadelevel = 1;
fadecount = 0;
repaint();
}
public void hideSoftKeys(long time, int level) {
this.time = System.currentTimeMillis() + time;
if (level > 0) fadelevel = level;
fadecount = 0;
repaint();
}
public final void keyPressed(int keyCode) {
callProcessEvent = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -