欢迎所有的测试博友对我的文章以及见解给出建议,意见。枞榕之测试所见所得,将从现在开始与大家一同分享。。。

j2me中canvas的基础用法

上一篇 / 下一篇  2010-04-22 17:52:44 / 个人分类:开发积累

6^kNyIY0最近学习了一些j2me的开发,在学到画布(canvas)里遇到了一些问题,没了思路,不知道如何去写代码,现在贴出来做一个总结也希望可以帮到初学者;

wI],Q _051Testing软件测试网2c-\0y5T'gy I#e

import javax.microedition.midlet.MIDlet;51Testing软件测试网'c8^B:D1|tD E
import javax.microedition.midlet.MIDletStateChangeException;
HYcS!u;r7jC I0import javax.microedition.lcdui.*;

#q'y'm j2e?5u1Z%M051Testing软件测试网6U h4r8O^)[

51Testing软件测试网8g0|5Ph2dM#PB#N
public class test extends MIDlet {51Testing软件测试网E)OG5}h;GC
private Display dis;51Testing软件测试网8x-L6\/],VM
private MyCanvas canvas;
-Jzt i \BMo;k0 51Testing软件测试网9{ iig ?[ ^nC-I
 public test(){51Testing软件测试网5d$u)OIp#si-e I&|*B
  dis=Display.getDisplay(this);
W;ta2h/V!D0  canvas=new MyCanvas(this);
;U^S#k\5j4TQqT0 }

oN1g1O]7\ W5V051Testing软件测试网:d i g-Y$_ AX;T,l.r:|7P^3l

 protected void startApp() throws MIDletStateChangeException {51Testing软件测试网X9rbzl
          dis.setCurrent(canvas);//显示对象为canvas
6n3l*m3[6V@Z3Nvn0         
q%\R]9|[ L0 }51Testing软件测试网:Y$]/s)KCeI
 
uQ_Hd!Yj%D0 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
{/qR~/g |0     51Testing软件测试网t.O7C`k7}\
 }51Testing软件测试网G*?+L!S4B E

51Testing软件测试网WDpVXKH5b X3{S

 protected void exit(){51Testing软件测试网i l3bm ^+f
  notifyDestroyed();//退出程序
r,zire]0 }51Testing软件测试网NfA'V*H+HUs
 protected void pauseApp() {51Testing软件测试网;p|,Sc R

51Testing软件测试网2y'c Sp4PS5F

 }51Testing软件测试网t}]L7djGg

51Testing软件测试网#y,o&cF!p

 51Testing软件测试网 aYC7Fk'`U
 class MyCanvas extends Canvas implements CommandListener{51Testing软件测试网C(X+n(`R-{iGp
  private Command cmdEXIT=new Command("EXIT",Command.EXIT,1);//定义一个退出按钮51Testing软件测试网|Um3f4x
  private String direction;
.U-hu!Y0l @ sh0  private test test;51Testing软件测试网R-v f7C;S
  private Image image=null;//定义一个图片,初始化值为null
'C@+\k0}0  public MyCanvas(test test){
8Xr(ghq(v,rm#J0   direction="2=up 8=dn 4=left 6=right";51Testing软件测试网xY xI6^7D"H#f
   this.test=test;51Testing软件测试网%|,b@%JR Z
   addCommand(cmdEXIT);51Testing软件测试网bTA2h~*X9Fv-`\
   setCommandListener(this);//给退出按钮加一个事件51Testing软件测试网:H#u A lHFv0?
  }
PZ:~1~*y0 public  void paint(Graphics g){51Testing软件测试网-_A] [3^(M0}
  g.setColor(150,150,100);//RGB颜色值51Testing软件测试网]5x3u^qq+ez4i5l
  g.drawRect(0, 0,20, 20);51Testing软件测试网x/Ht7h,nm X'_N
  g.fillRect(0,0,20,20);   51Testing软件测试网 Dg8x-\oM,K'C
  g.drawString("this is the Second line",25,0,g.TOP|g.LEFT);//以上为第一行51Testing软件测试网:d WY2@z&n e
  51Testing软件测试网9i7j%gRC9e%rC
  g.setColor(200,200,10);//RGB颜色值
4v Tpt;k] vcZ0     g.drawRect(0,20,20,20);
lxonj2N0yi5fi0     g.fillRect(0,20,20,20);
U1j4JAR*U:w*V0     g.drawString("this is the Second line", 25,20, g.TOP|g.LEFT);//这是第二行
cpVH(d;w0    
"]&hA.@8[p&VT0     g.setColor(210,200,190);//RGB颜色值
S#KiL)h_ l u^]"j/J0     g.drawRect(0,40,20,20);
+f#^ y*wflN0     g.fillRect(0,40,20,20);51Testing软件测试网u.C'Bu\
     g.drawString("this is the third line",25,40,g.TOP|g.LEFT);//这是第三行51Testing软件测试网Ig(zU*r y~8G.~(s
     try{51Testing软件测试网%Dxr8RD y
        image=Image.createImage("/dd.png");//实例化图片
7wA8q;x.G+A'a8BK0         g.drawImage(image, 0, 60,20);51Testing软件测试网/? @@(X0J+o%Y2h+QZ
         g.drawString("this is the fourth line",35,60,g.LEFT|g.TOP);
Mu`/Em0i-z!lt#Q&L0     }
;K3J4Orv0    catch(Exception e){51Testing软件测试网&ih,M.e&{i
     System.out.println("报错了,请查看原因");51Testing软件测试网;[9@|.C9O R0|zF5Qs"]
  }51Testing软件测试网I g*lJ~3J7S!IV:O5[*P
   51Testing软件测试网6^Wu E-^0o)iQ.O.bO
 
'pC CB8h!e0   51Testing软件测试网5gVNY{)G
 }
0aWX r8ju0  public void commandAction(Command c,Displayable d){51Testing软件测试网Vf*]5pf0g!K
   if(c==cmdEXIT){
Hakt,T{d0    test.exit();
d#e @/A-a*N)bP0   }
.V*bW d,}!ok1fu"i0  }
m;A(a:b*g0  }51Testing软件测试网E.XJ$z^;?;t&{

51Testing软件测试网Y$H-iF\{#a.D.W

}

'rO {*vo%q Sv0

!H:r O)nj OP0最后出来的结果如下:51Testing软件测试网K7p0bta0~^

51Testing软件测试网2?f a0A o(K|I Ow

51Testing软件测试网-N`rmxx7U1t&A@.N

51Testing软件测试网oR{QX%_4l

希望高手也可以给予指正,本人初学,水平不高。。
]\5S5@.gb9v|0

9@dtK"A3aBX-m0

TAG:

 

评分:0

我来说两句

Open Toolbar