原文 http://hzxdark.javaeye.com/blog/175628
3GEYEg2Q[*H.Chttp://www.3geye.net/?3/viewspace-3177
7@]#MI;tR&~3kbox 是一个MVC2模型的J2ME开源框架,用于快速开发可扩展的J2ME MIDP应用程序。
$J.LGL+MaC\3主页:http://kbox-mvc.sourceforge.net/
3GEYE1K@N9jQ+\5Au
#H1yL3T/f%~c
@Tp3其结构如图:
3GEYE9W#NM4cTZ,Sv
3GEYEj{%T!R/CA
M o2b.w-u-X
aUB33GEYE Pl1I-i
LV^
-Sz4O})v6x"i$gp YV3所有的可视组件都位于需要经由MIDlet来显示,而MIDlet上的所有事件,包括键盘事件,按钮事件提交到ActionController
上,
ActionController跟据
配置文件,将事件转发给相应的Action,Action将处理结果得到的可视组件(Displayable)通过
ActionForward返回MIDlet显示。
3GEYEt"v[2\O1kpY I{
a\&B'AzW:G%^%r3KBOX的使用非常简单, 以一个HELLO WORLD 为例:
i$iR9L},mR3创建一个MIDLET:
3GEYE([.MKl3qX/o#}9P
3GEYE!Ku\w2P] GxU
public class TestMIDlet extends MIDlet{
3GEYE na+C?0D ?N
x-Ma
3GEYE"Jq|.xSqs�E
…
F,sw%SHH rj33GEYEq I5GmP
protected void startApp() throws MIDletStateChangeException {
3GEYE{Y'bJA%`$I
3GEYEq_7S-E^` \ k
ActionController controller = ActionController.getInstance(true);
3GEYE0eMXJu8@
2h-Q
^n&OS_
[N9T E3controller.registerViewer(this); //Register the MIDlet
3GEYE(k8H!w0[mh2d-\
3GEYE7AV-G9ENU&bUg
3GEYE"C @$|jCA9Uo
3GEYE r8Y;X\
D2f8KE
sA
MyKGameCanvas mgc = new MyKGameCanvas("MYGC"); //Create a GameCanvas
3GEYE2LI
V
hVD/W
3GEYE%T,_T%jQ
k }7N!^^7S9}3
2F F p�Pb,H/o5B3KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
XW!_LjoZX0[3mgc.addCommand(kc);
s,w|{*l1vR/j3mgc.setCommandListener(controller);
l$yH4JR{+y3Display.getDisplay(this).setCurrent(mgc);
3GEYE,q�~.Bq Q6d
mJ0ISY
3GEYE\7I~ZK/l$}
}
7@�d^F$xQY{/f3
W*vKBfG jB3…
YNAUuq7O;y�y3
T%u/\ o�o?3}
I dCq!Iz33GEYECtX#N.~`OE-y s6q$i
创建一个Canvas:
3GEYEpK
a[X"R+u
%f"[�wh7y$^)z9{B3public class MyKGameCanvas extends KGameCanvas{
3GEYEM.m`SV"a:kV
S"\M3[Byz T3
vQXWN-^3
%lZ2@@9S
l3public MyKGameCanvas(String uniqueID) {
3GEYELn�~!M#fU7B H
EB
3GEYE[Mu} J@:fX]%M
super(uniqueID);
'Y6Y
U%}�Z|J2Uq$GF3
*A%Du`jc3}
3GEYE
qH#f!A+C/L&I^_
P
hE/B4p o+vKO3public void showString(String string){
cYn:{}+C
i33GEYEe+zs1xj9cDO ~U
Graphics g = this.getGraphics();
'q{@`+y'_x"Tz33GEYEx2e0U%`u-s J.SP�R!W
g.setColor(0xFFFFFFFF);
-r1DBq#O-|*x3
P \W(lj"])s+[l3g.fillRect(0, 0, this.getWidth(), this.getHeight());
)Y.x7|6d
kSy3
%}+n4]cc)ZJb3g.setColor(0xFF000000);
3GEYE@;S'Z8C Od(Q5o
-UTFj,Lmq3g.drawString(string, 0, 0, Graphics.TOP|Graphics.LEFT);
Ck:j#F7cx33GEYET(G7YG V3h
this.flushGraphics();
g/|1aA4@
t s)x!f1HS33GEYE[E\x.y!TZC�gEV
}
3[3H`.rX-VP3
^$u&ays'N"w:|6I3}
3GEYEV2rmV!`#f;R`oQ
3GEYE"RM-B6j6]+?Bge
创建相应的Action:
3GEYE6X0Z9z$Q~Z9Q
3GEYEs4~u4B5B1D*_
public class TestAction implements Action{
3GEYEkl6kRt-{Qo;Zy
3GEYE~8~6R4Io6R
S
public void excute(KCommand cmd, Item item, Displayable dis, ActionForward forward,Hashtable parameters) {
3GEYEQQ6zDR:R*}OUc
*KR+B fjHvl33GEYEko~8^|Jk
Fdch-xvH2s{y t3MyKGameCanvas mkc = ((MyKGameCanvas)dis);
K.B$Ba+pTRmBq3
8[zS3T8H3mkc.showString("hello world! From "+mkc.getUniqueID()+"."+cmd.getUniqueID());
_pb8qFh3
#g"N/h1dk/Z!nY3}
R9j
MMjuX3
rH~b/eZ
r3}
z/vlv
Ri}(DL N�m33GEYE3M^b;uLQ
将按钮事件注册给Action:
3GEYE7Z8m0dQ
H+}H)q
%{^mRl$lb3DEBUG=true //open the debug model of KBOX
gt't|dH33GEYE9qv s'KSK(?-G*Wt
CMD.MYCMD.ACTION=action.TestAction
3]v%njQ33GEYE7c%R
[y:?-M:w
注意这里的MyKGameCanvas
继承的KGameCanvas,而不是GameCanvas。KBOX的KGameCanvas类继承自GameCanvas,当并没有使用
GameCanvas那种使用线程监听状态的模型,而是重写了keypressed等方法来处理键盘事件。另外,KGameCanvas的重写的按钮事件
的处理已经实现了重复按键情况,而且可以指定重复按键的灵敏度,这点是非常好用的:)
3GEYED4_%h~y#s,]&\
3GEYESP4D1Nj/?*ov
KCommand是KBOX扩展的按钮组件。
"LOB9Dw&O!m3KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
3GEYE~bu U-[A.`$S,~
第一个参数是按钮的名字,第二参数是按钮类型,第三个参数是优先级,第四个参数是ID名,用于配置文件的映射,对应“
CMD.MYCMD.ACTION=action.TestAction”中的MYCMD,第五个参数是可传递参数,是一个Hashtable类型,用于
向Action传递参数(当然你喜欢的话,也可以将参数提到配置文件里去……)。
3GEYE8ElR"T9VmC,DP
5N
o0_&Q