程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> JavaMe開發:繪制文本框TextEdit(2)

JavaMe開發:繪制文本框TextEdit(2)

編輯:J2ME

UserRegist.Java

  1. package com.token.vIEw;
  2. import Javax.microedition.lcdui.Font;
  3. import Javax.microedition.lcdui.Graphics;
  4. import Javax.microedition.lcdui.game.GameCanvas;
  5. import com.token.model.*;
  6. import com.token.util.*;
  7. import com.token.vIEw.components.*;
  8. public class UserRegist extends GameCanvas implements Runnable {
  9. private UIController controller;
  10. private Graphics graphics;
  11. private Font ft;
  12. private Menu menu;
  13. private Head head;
  14. private BackGroud backGroud;
  15. private UserDataRecord userRecord;
  16. private String title;
  17. private TextEdit textEdit_name;
  18. private TextEdit textEdit_passwd;
  19. private TextEdit textEdit_passwd_re;
  20. private int textEdit_name_x;
  21. private int textEdit_name_y;
  22. private int textEdit_passwd_x;
  23. private int textEdit_passwd_y;
  24. private int textEdit_passwd_re_x;
  25. private int textEdit_passwd_re_y;
  26. private int currentlySelectedIndex = 0;
  27. private String username;
  28. private String passwd;
  29. private String passwd_re;
  30. long caretBlinkDelay = 500L;
  31. long lastCaretBlink = 0;
  32. private String object_name;
  33. private String editor;
  34. private boolean cursorBlinkOn1;
  35. private boolean cursorBlinkOn2;
  36. private boolean cursorBlinkOn3;
  37. private int width;
  38. private int height;
  39. public UserRegist(UIController control)
  40. {
  41. super(false);
  42. this.controller=control;
  43. this.title = "用戶注冊";
  44. setFullScreenMode(true);
  45. graphics = getGraphics();
  46. width = getWidth();
  47. height = getHeight();
  48. menu = new Menu(this);
  49. head = new Head(this);
  50. backGroud = new BackGroud(this);
  51. userRecord = new UserDataRecord();
  52. textEdit_name = new TextEdit(this);
  53. textEdit_passwd = new TextEdit(this);
  54. textEdit_passwd_re = new TextEdit(this);
  55. }
  56. public void show(Object[] args) {
  57. // TODO Auto-generated method stub
  58. setFullScreenMode(true);
  59. object_name = ((String)args[0]!=null)?(String)args[0]:"";
  60. editor = ((String)args[1]!=null)?(String)args[1]:"";
  61. username = ((String)args[2]!=null)?(String)args[2]:"";
  62. passwd = ((String)args[3]!=null)?(String)args[3]:"";
  63. passwd_re = ((String)args[4]!=null)?(String)args[4]:"";
  64. if(editor.equals("regist_name"))
  65. {
  66. cursorBlinkOn1 = true;
  67. cursorBlinkOn2 = false;
  68. cursorBlinkOn3 = false;
  69. currentlySelectedIndex =0;
  70. }
  71. else if(editor.equals("regist_passwd"))
  72. {
  73. cursorBlinkOn1 = false;
  74. cursorBlinkOn2 = true;
  75. cursorBlinkOn3 = false;
  76. currentlySelectedIndex =1;
  77. }
  78. else if(editor.equals("regist_passwd_re"))
  79. {
  80. cursorBlinkOn1 = false;
  81. cursorBlinkOn2 = false;
  82. cursorBlinkOn3 = true;
  83. currentlySelectedIndex =2;
  84. }
  85. //System.out.println(object_name);
  86. //System.out.println(editor);
  87. draw();
  88. redraw();
  89. }
  90. public void draw()
  91. {
  92. //clearScreen();
  93. backGroud.drawBackGroud(this, graphics);
  94. head.drawHead(this,graphics,this.title);
  95. menu.drawMenu(this,graphics,"下一步","退出");
  96. drawBody();
  97. }
  98. private void redraw()
  99. {
  100. switch(currentlySelectedIndex)
  101. {
  102. case 0:
  103. {
  104. cursorBlinkOn2 = false;
  105. cursorBlinkOn3 = false;
  106. editor = "regist_name";
  107. break;
  108. }
  109. case 1:
  110. {
  111. cursorBlinkOn1 = false;
  112. cursorBlinkOn3 = false;
  113. editor = "regist_passwd";
  114. break;
  115. }
  116. case 2:
  117. {
  118. cursorBlinkOn1 = false;
  119. cursorBlinkOn2 = false;
  120. editor = "regist_passwd_re";
  121. break;
  122. }
  123. default:;
  124. }
  125. textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
  126. textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
  127. textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
  128. textEdit_name.flushGraphics();
  129. }
  130. public void drawBody()
  131. {
  132. int margin =5;
  133. ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
  134. String info = "用戶名:\n";
  135. String info_wrap1[] = StringDealMethod.format(info, width-10, ft);
  136. graphics.setFont(ft);
  137. graphics.setColor(Color.text);
  138. for(int i=0; i<info_wrap1.length; i++)
  139. {
  140. graphics.drawString(info_wrap1[i],5, (i) * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
  141. }
  142. textEdit_name_x = 5;
  143. textEdit_name_y = info_wrap1.length * ft.getHeight()+40;
  144. textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
  145. info = "用戶密碼:\n";
  146. String info_wrap2[] = StringDealMethod.format(info, width-10, ft);
  147. graphics.setFont(ft);
  148. graphics.setColor(Color.text);
  149. for(int i=0; i<info_wrap2.length; i++)
  150. {
  151. graphics.drawString(info_wrap2[i],5, (i+info_wrap1.length) * ft.getHeight()+textEdit_name.height+margin+40, Graphics.TOP|Graphics.LEFT);
  152. }
  153. textEdit_passwd_x = 5;
  154. textEdit_passwd_y = (info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+margin+40;
  155. textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
  156. info = "密碼確認:\n";
  157. String info_wrap3[] = StringDealMethod.format(info, width-10, ft);
  158. graphics.setFont(ft);
  159. graphics.setColor(Color.text);
  160. for(int i=0; i<info_wrap3.length; i++)
  161. {
  162. graphics.drawString(info_wrap3[i],5, (i+info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40, Graphics.TOP|Graphics.LEFT);
  163. }
  164. textEdit_passwd_re_x = 5;
  165. textEdit_passwd_re_y = (info_wrap1.length+info_wrap2.length+info_wrap3.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40;
  166. textEdit_passwd_re.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
  167. }
  168. public void clearScreen()
  169. {
  170. graphics.setColor(0xff,0xff,0xff);
  171. graphics.fillRect(0, 0, width, height);
  172. }
  173. public void checkTimeStamp()
  174. {
  175. long currentTime = System.currentTimeMillis();
  176. //System.out.println("1");
  177. if(lastCaretBlink + caretBlinkDelay < currentTime)
  178. {
  179. //System.out.println("2");
  180. if(editor.equals("regist_name"))
  181. {
  182. cursorBlinkOn1 =! cursorBlinkOn1;
  183. cursorBlinkOn2 = false;
  184. cursorBlinkOn3 = false;
  185. }
  186. else if(editor.equals("regist_passwd"))
  187. {
  188. cursorBlinkOn1 = false;
  189. cursorBlinkOn2 =! cursorBlinkOn2;
  190. cursorBlinkOn3 = false;
  191. }
  192. else if(editor.equals("regist_passwd_re"))
  193. {
  194. cursorBlinkOn1 = false;
  195. cursorBlinkOn2 = false;
  196. cursorBlinkOn3 =! cursorBlinkOn3;
  197. }
  198. lastCaretBlink = currentTime;
  199. }
  200. }
  201. public void run()
  202. {
  203. //System.out.println("run");
  204. while(true)
  205. {
  206. checkTimeStamp();
  207. redraw();
  208. try
  209. {
  210. synchronized(this)
  211. {
  212. //System.out.println("3");
  213. wait(50L);
  214. }
  215. }
  216. catch(Exception e)
  217. {
  218. e.printStackTrace();
  219. }
  220. }
  221. }
  222. protected void keyPressed(int keyCode)
  223. {
  224. switch(keyCode)
  225. {
  226. case KeyID.SOFT_RIGHT:
  227. {
  228. controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
  229. break;
  230. }
  231. case KeyID.SOFT_LEFT:
  232. {
  233. if(username!="" && passwd!=""&&passwd_re!="")
  234. {
  235. if(passwd.equals(passwd_re))
  236. {
  237. userRecord.db_deleteAllRecord();
  238. if(userRecord.db_getRecord(1)==null)
  239. {
  240. UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());
  241. userRecord.db_addRecord(userItem);
  242. userItem = null;
  243. System.gc();
  244. }
  245. String update = "start";
  246. Object [] args = {"activeScreen", null, update};
  247. controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_TOKEN_SCREEN,args);
  248. }
  249. }
  250. break;
  251. }
  252. case KeyID.KEY_EDIT:
  253. case KEY_NUM0:
  254. case KEY_NUM1:
  255. case KEY_NUM2:
  256. case KEY_NUM3:
  257. case KEY_NUM4:
  258. case KEY_NUM5:
  259. case KEY_NUM6:
  260. case KEY_NUM7:
  261. case KEY_NUM8:
  262. case KEY_NUM9:
  263. {
  264. //System.out.println(editor);
  265. Object[] args = {object_name,editor,username,passwd,passwd_re};
  266. controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);
  267. break;
  268. }
  269. default:;
  270. }
  271. keyCode = getGameAction(keyCode);
  272. switch(keyCode)
  273. {
  274. case UP:
  275. case LEFT:
  276. {
  277. currentlySelectedIndex--;
  278. if(currentlySelectedIndex<0)
  279. {
  280. currentlySelectedIndex=0;
  281. }
  282. else
  283. {
  284. redraw();
  285. }
  286. break;
  287. }
  288. case DOWN:
  289. case RIGHT:
  290. {
  291. currentlySelectedIndex++;
  292. if(currentlySelectedIndex>2)
  293. {
  294. currentlySelectedIndex=2;
  295. }
  296. else
  297. {
  298. redraw();
  299. }
  300. break;
  301. }
  302. }
  303. }
  304. }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved