13,100
社区成员
发帖
与我相关
我的任务
分享
public class SelectCode {
Font large_font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
public StringBuffer bu = new StringBuffer();// 输入的字符串
private String c_num = "";
private int displayNum = 0;
private boolean displayBiao = true;
private String biao = "|";// 光标
public int biaoIndex;// 光标在字符串中的位置
public int startIndex;// 字符串显示部分的起始位置
public int endIndex;// 字符串显示部分的结束位置
public void drawMainMenu(Graphics g,int x,int y) {
g.setFont(large_font);
g.setColor(0xff0000);
g.drawString(bu.toString().substring(0, biaoIndex), x, y, 20);
if (displayBiao) {
g.drawString(biao, x + large_font.stringWidth(bu.toString()
.substring(0, biaoIndex)), y, 20);
}
}
public void KeyPressed(int key) {
switch (key) {
// case -3:
// if (biaoIndex[text_index] > 0) {
// if (biaoIndex[text_index] == startIndex[text_index]) {
// startIndex[text_index]--;
// while (large_font.stringWidth(bu[text_index].toString()
// .substring(startIndex[text_index],
// endIndex[text_index])) > 100 - 4) {
// endIndex[text_index]--;
// }
// }
// biaoIndex[text_index]--;
// }
// break;
// case -4:
// if (biaoIndex[text_index] < bu[text_index].length()) {
// if (biaoIndex[text_index] == endIndex[text_index]) {
// endIndex[text_index]++;
// while (large_font.stringWidth(bu[text_index].toString()
// .substring(startIndex[text_index],
// endIndex[text_index])) > 100 - 4) {
// startIndex[text_index]++;
// }
// }
// biaoIndex[text_index]++;
// }
// break;
case -3:
if (biaoIndex > 0) {
bu.delete(biaoIndex - 1, biaoIndex);
biaoIndex--;
if (biaoIndex <= startIndex) {
startIndex = biaoIndex;
}
if (endIndex > bu.length()) {
endIndex = bu.length();
}
while (large_font.stringWidth(bu.toString().substring(
startIndex, endIndex)) > 96) {
endIndex--;
}
}
break;
}
// if (large_font.stringWidth(bu.toString()) <= 30) {
pressedNum(key);
// }
}
private void pressedNum(int keyCode) {
switch (keyCode) {
case Key.KEY_NUM0:
c_num = "0";
updateInput();
break;
case Key.KEY_NUM1:
c_num = "1";
updateInput();
break;
case Key.KEY_NUM2:
c_num = "2";
updateInput();
break;
case Key.KEY_NUM3:
c_num = "3";
updateInput();
break;
case Key.KEY_NUM4:
c_num = "4";
updateInput();
break;
case Key.KEY_NUM5:
c_num = "5";
updateInput();
break;
case Key.KEY_NUM6:
c_num = "6";
updateInput();
break;
case Key.KEY_NUM7:
c_num = "7";
updateInput();
break;
case Key.KEY_NUM8:
c_num = "8";
updateInput();
break;
case Key.KEY_NUM9:
c_num = "9";
updateInput();
break;
}
}
private void updateInput() {
// if (large_font.stringWidth(bu.toString().substring(startIndex, endIndex)) <= 30) {
bu.insert(biaoIndex, c_num);
c_num = "";
biaoIndex++;
if (biaoIndex == bu.length()) {
endIndex = bu.length();
}
// }
}
public void NumData() {
displayNum++;
if (displayNum == 8) {
displayNum = 1;
}
if (displayNum > 4) {
displayBiao = false;
} else {
displayBiao = true;
}
}
}
public class SelectCode {
Font large_font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
public StringBuffer bu = new StringBuffer();// 输入的字符串
private String c_num = "";
private int displayNum = 0;
private boolean displayBiao = true;
private String biao = "|";// 光标
public int biaoIndex;// 光标在字符串中的位置
public int startIndex;// 字符串显示部分的起始位置
public int endIndex;// 字符串显示部分的结束位置
public void drawMainMenu(Graphics g,int x,int y) {
g.setFont(large_font);
g.setColor(0xff0000);
g.drawString(bu.toString().substring(0, biaoIndex), x, y, 20);
if (displayBiao) {
g.drawString(biao, x + large_font.stringWidth(bu.toString()
.substring(0, biaoIndex)), y, 20);
}
}
public void KeyPressed(int key) {
switch (key) {
// case -3:
// if (biaoIndex[text_index] > 0) {
// if (biaoIndex[text_index] == startIndex[text_index]) {
// startIndex[text_index]--;
// while (large_font.stringWidth(bu[text_index].toString()
// .substring(startIndex[text_index],
// endIndex[text_index])) > 100 - 4) {
// endIndex[text_index]--;
// }
// }
// biaoIndex[text_index]--;
// }
// break;
// case -4:
// if (biaoIndex[text_index] < bu[text_index].length()) {
// if (biaoIndex[text_index] == endIndex[text_index]) {
// endIndex[text_index]++;
// while (large_font.stringWidth(bu[text_index].toString()
// .substring(startIndex[text_index],
// endIndex[text_index])) > 100 - 4) {
// startIndex[text_index]++;
// }
// }
// biaoIndex[text_index]++;
// }
// break;
case -3:
if (biaoIndex > 0) {
bu.delete(biaoIndex - 1, biaoIndex);
biaoIndex--;
if (biaoIndex <= startIndex) {
startIndex = biaoIndex;
}
if (endIndex > bu.length()) {
endIndex = bu.length();
}
while (large_font.stringWidth(bu.toString().substring(
startIndex, endIndex)) > 96) {
endIndex--;
}
}
break;
}
// if (large_font.stringWidth(bu.toString()) <= 30) {
pressedNum(key);
// }
}
private void pressedNum(int keyCode) {
switch (keyCode) {
case Key.KEY_NUM0:
c_num = "0";
updateInput();
break;
case Key.KEY_NUM1:
c_num = "1";
updateInput();
break;
case Key.KEY_NUM2:
c_num = "2";
updateInput();
break;
case Key.KEY_NUM3:
c_num = "3";
updateInput();
break;
case Key.KEY_NUM4:
c_num = "4";
updateInput();
break;
case Key.KEY_NUM5:
c_num = "5";
updateInput();
break;
case Key.KEY_NUM6:
c_num = "6";
updateInput();
break;
case Key.KEY_NUM7:
c_num = "7";
updateInput();
break;
case Key.KEY_NUM8:
c_num = "8";
updateInput();
break;
case Key.KEY_NUM9:
c_num = "9";
updateInput();
break;
}
}
private void updateInput() {
// if (large_font.stringWidth(bu.toString().substring(startIndex, endIndex)) <= 30) {
bu.insert(biaoIndex, c_num);
c_num = "";
biaoIndex++;
if (biaoIndex == bu.length()) {
endIndex = bu.length();
}
// }
}
public void NumData() {
displayNum++;
if (displayNum == 8) {
displayNum = 1;
}
if (displayNum > 4) {
displayBiao = false;
} else {
displayBiao = true;
}
}
}
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class DrawString extends MIDlet {
private Display display;
private StringCanvas sc;
//被绘制的字符串
private final static String STR= "你好,柯锐";
public DrawString() {
super();
sc=new StringCanvas();
// TODO 自动生成构造函数存根
}
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
//获得当前MIDlet的Display对象
display=Display.getDisplay(this);
//设置StringCanvas对象为当前显示对象
display.setCurrent(sc);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
class StringCanvas extends Canvas{
/**
* 在这个方法中绘制屏幕
*/
protected void paint(Graphics g){
//绘制屏幕-背景白色
g.setColor(0XFFFFFF);
g.fillRect(0,0,this.getWidth(),this.getHeight());
//设置绘图颜色为黑色
g.setColor(0X000000);
Font font=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_LARGE);
g.setFont(font);
int y=0;
//定位点对于字符串绘制的影响
g.drawString(STR,0,0,
Graphics.LEFT | Graphics.TOP);
y+=font.getHeight();
g.drawString(STR,(getWidth()-font.stringWidth(STR))/2,
y,Graphics.TOP | Graphics.LEFT);
y+=font.getHeight();
g.drawString(STR,(getWidth()-font.stringWidth(STR))/2,
y,Graphics.TOP | Graphics.HCENTER);
y+=font.getHeight();
g.drawString(STR,(getWidth()-font.stringWidth(STR))/2,
y,Graphics.TOP | Graphics.RIGHT);
//下面三个等式相等
font=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
g.setFont(font);
y+=font.getHeight();
int x=20;
g.drawString(STR, x, y, Graphics.TOP | Graphics.LEFT);
y+=font.getHeight();
g.drawString(STR, x+font.stringWidth(STR)/2, y, Graphics.TOP | Graphics.HCENTER);
y+=font.getHeight();
g.drawString(STR,x+font.stringWidth(STR),y,Graphics.TOP | Graphics.RIGHT);
//下面三个等式相等
font=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
g.setFont(font);
y+=font.getHeight();
g.drawString(STR, x, y+font.getBaselinePosition(), Graphics.BASELINE | Graphics.LEFT);
y+=font.getHeight();
g.drawString(STR, x+font.stringWidth(STR)/2, y+font.getBaselinePosition(), Graphics.BASELINE | Graphics.HCENTER);
y+=font.getHeight();
g.drawString(STR,x+font.stringWidth(STR),y+font.getBaselinePosition(),Graphics.BASELINE | Graphics.RIGHT);
// 下面三个等式相等
font=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_LARGE);
g.setFont(font);
y+=font.getHeight();
g.drawString(STR, x, y+font.getHeight(), Graphics.BOTTOM | Graphics.LEFT);
y+=font.getHeight();
g.drawString(STR, x+font.stringWidth(STR)/2, y+font.getHeight(), Graphics.BOTTOM | Graphics.HCENTER);
y+=font.getHeight();
g.drawString(STR,x+font.stringWidth(STR),y+font.getHeight(),Graphics.BOTTOM | Graphics.RIGHT);
}
}
}