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

JavaMe開發:也說MVC設計模式(2)

編輯:J2ME

UserDataRecord.Java

  1. package com.token.util;
  2. import Java.util.Vector;
  3. import Javax.microedition.rms.RecordComparator;
  4. import Javax.microedition.rms.RecordEnumeration;
  5. import Javax.microedition.rms.RecordStore;
  6. import Javax.microedition.rms.RecordStoreException;
  7. import com.token.model.*;
  8. public class UserDataRecord {
  9. private static final String RECORDSTORE_NAME="USER_DB";
  10. private static RecordStore info;
  11. public UserDataRecord(){
  12. }
  13. //打開RecordStore,沒有則創建
  14. public void openDataBase() {
  15. try {
  16. info = RecordStore.openRecordStore(RECORDSTORE_NAME, true);
  17. }catch (RecordStoreException ex) {
  18. info =null;
  19. }
  20. }
  21. //關閉RecordStore
  22. public void closeDataBase() {
  23. if (info!= null) {
  24. try {
  25. info.closeRecordStore();
  26. info=null;
  27. } catch (RecordStoreException ex) {}
  28. }
  29. }
  30. //增加記錄
  31. public int db_addRecord(UserDataItem item) {
  32. try {
  33. this.openDataBase();
  34. byte[] data=item.getBytes();
  35. int id=info.getNextRecordID();
  36. info.addRecord(data,0,data.length);
  37. this.closeDataBase();
  38. return id;
  39. } catch (RecordStoreException ex) { }
  40. return -1;
  41. }
  42. //更新記錄
  43. public void db_updateRecord(UserDataItem item){
  44. try {
  45. this.openDataBase();
  46. byte[] data=item.getBytes();
  47. info.setRecord(item.getId(),data,0,data.length);
  48. this.closeDataBase();
  49. } catch (RecordStoreException ex) { }
  50. }
  51. //訪問一條記錄
  52. public UserDataItem db_getRecord(int id){
  53. UserDataItem item=null;
  54. try {
  55. this.openDataBase();
  56. item = new UserDataItem(id,info.getRecord(id));
  57. this.closeDataBase();
  58. } catch (RecordStoreException ex) { }
  59. return item;
  60. }
  61. //刪除一條記錄
  62. public void db_deleteRecord(int id){
  63. try {
  64. this.openDataBase();
  65. info.deleteRecord(id);
  66. this.closeDataBase();
  67. } catch (RecordStoreException ex) {}
  68. }
  69. //刪除所有記錄
  70. public void db_deleteAllRecord(){
  71. try {
  72. RecordStore.deleteRecordStore(RECORDSTORE_NAME);
  73. } catch (RecordStoreException ex) {}
  74. }
  75. //訪問所有記錄
  76. public Vector db_getRecords(){
  77. Vector items=new Vector(10,3);
  78. this.openDataBase();//打開RecordStore
  79. RecordEnumeration enum1=null;
  80. int ind=0;
  81. try{
  82. UserDataItem item=null;
  83. enum1=info.enumerateRecords(null,new InnerComparator(),false);
  84. while(enum1.hASPreviousElement()){
  85. ind=enum1.previousRecordId();
  86. item=new UserDataItem(ind,info.getRecord(ind));
  87. items.addElement(item);
  88. }
  89. }catch(Exception ex){ex.printStackTrace();}
  90. finally{
  91. try{
  92. enum1.destroy();
  93. }catch(Exception e){}
  94. this.closeDataBase();//關閉RecordStore
  95. }//end finally
  96. return items;
  97. }
  98. //一個簡單的比較器
  99. private class InnerComparator implements RecordComparator{
  100. public int compare(byte[] rec1, byte[] rec2){
  101. if(rec1.length>rec2.length)
  102. return FOLLOWS;
  103. else if(rec1.length<rec2.length)
  104. return PRECEDES;
  105. else
  106. return EQUIVALENT;
  107. }
  108. }
  109. }

視圖(vIEw)

WelcomeScreen.Java

  1. package com.token.vIEw;
  2. import Javax.microedition.lcdui.*;
  3. import Javax.microedition.lcdui.game.GameCanvas;
  4. import com.token.util.UIController;
  5. import com.token.util.StringDealMethod;
  6. import com.token.vIEw.components.*;
  7. public class WelcomeScreen extends GameCanvas {
  8. private UIController controller;
  9. private Graphics graphics;
  10. private Font ft;
  11. private String info;
  12. private String info_wrap[];
  13. private int width;
  14. private int height;
  15. private Menu menu;
  16. private Head head;
  17. private BackGroud backGroud;
  18. public WelcomeScreen(UIController control) {
  19. // TODO Auto-generated constructor stub
  20. super(false);
  21. controller=control;
  22. setFullScreenMode(true);
  23. graphics = getGraphics();
  24. width = getWidth();
  25. height = getHeight();
  26. menu = new Menu(this);
  27. head = new Head(this);
  28. backGroud = new BackGroud(this);
  29. }
  30. public void show() {
  31. // TODO Auto-generated method stub
  32. //clearScreen();
  33. backGroud.drawBackGroud(this, graphics);
  34. head.drawHead(this,graphics,"");
  35. menu.drawMenu(this, graphics,"下一步","退出");
  36. drawBody();
  37. }
  38. public void drawBody()
  39. {
  40. ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
  41. info = "歡迎使用!\n"
  42. +"1 MVC測試;\n"
  43. +"2 自動換行測試,繪制可自動識別換行的字符串。\n";
  44. info_wrap = StringDealMethod.format(info, width-10, ft);
  45. graphics.setColor(Color.text);
  46. graphics.setFont(ft);
  47. for(int i=0; i<info_wrap.length; i++)
  48. {
  49. graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
  50. }
  51. }
  52. public void clearScreen()
  53. {
  54. graphics.setColor(0xff,0xff,0xff);
  55. graphics.fillRect(0, 0, width, height);
  56. }
  57. protected void keyPressed(int keycode)
  58. {
  59. switch(keycode)
  60. {
  61. case KeyID.SOFT_RIGHT:
  62. {
  63. controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
  64. break;
  65. }
  66. case KeyID.SOFT_LEFT:
  67. {
  68. String editor = "regist_name";
  69. Object [] args = {"registScreen",editor, null,null,null};
  70. controller.handleEvent(UIController.EventID.EVENT_NEXT_USER_REGIST_SCREEN,args);
  71. break;
  72. }
  73. default:;
  74. }
  75. }
  76. }

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_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. }

*TextEdit是利用GameCanvas繪制的自定義文本編輯框。後續文章將給出具體實現。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved