J2ME GUI实战之五 ----------LWUIT的绘图功能

function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();} 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:
J2ME GUI实战之五 ----------LWUIT的绘图功能

首先,需要说明一下LWUIT的控件:
LWUIT的控件,可以算是做得很全,他的设计模式跟J2SE有点类似,而做过J2ME的人要掌握也不需要长时间。LWUIT的控件,是原来高级控件与低级控件的集合,并加入了更多元素,因此你可以在用高级控件的同时,很自然地实现低级控件的功能。例如本文中的绘图功能,就是如此!
在本例中,依然使用大家熟悉的void paint(Graphics g) 函数,作为绘图的主体,然而paint的“上司”(绘图类)并不再是继承Canvas,而是继承Component,并且继承Component之后要这 样被调用:form.addComponent(BorderLayout.CENTER, new Painting());//Painting就是“上司”。可见,在LWUIT里,已经没低级控件这个概念,但是有这个用法-----传统的绘图类也作为控件类,被调用了。或许,你想用回原来的javax.microedition.lcdui.Graphics,但是lcdui的Graphics和Display 与 LWUIT的Graphics和Display不兼容(LWUIT多了很多方法),因此“新欢旧爱不能兼得”!

OK,先给出调用绘图功能的代码:
  1. privateclassButtonActionListenerimplementsActionListener{
  2. publicvoidactionPerformed(ActionEventevt){
  3. Stringsel_button=((Button)(evt.getSource())).getText();//取得所选按钮的名称
  4. if(sel_button.equals("Image1"))
  5. newAnimationDemo().form.show();
  6. elseif(sel_button.equals("Image2"))
  7. newPaintingDemo().form.show();
  8. }
  9. }
接着再给出绘图功能实现的代码:
  1. /*
  2. *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  3. *Useissubjecttolicenseterms.
  4. *
  5. */
  6. packagecom.sun.lwuit.uidemo;

  7. importcom.sun.lwuit.Command;
  8. importcom.sun.lwuit.Component;
  9. importcom.sun.lwuit.Form;
  10. importcom.sun.lwuit.Graphics;
  11. importcom.sun.lwuit.events.ActionEvent;
  12. importcom.sun.lwuit.events.ActionListener;
  13. importcom.sun.lwuit.layouts.BorderLayout;


  14. /**
  15. *Demonstratessimpleanimationbothstaticandmanual
  16. *
  17. *@authorShaiAlmog
  18. */
  19. publicclassPaintingDemoextendsFormimplementsActionListener{
  20. publicFormform=newForm("PaintingDemo");
  21. privateCommandbackCommand=newCommand("Back",1);
  22. privateCommandnextCommand=newCommand("next",2);
  23. PaintingDemo()
  24. {
  25. form.addCommand(backCommand);
  26. form.addCommand(nextCommand);
  27. form.setCommandListener(this);
  28. form.setLayout(newBorderLayout());
  29. form.addComponent(BorderLayout.CENTER,newPainting());

  30. }
  31. publicclassPaintingextendsComponent{
  32. privateintw;
  33. publicvoidpaint(Graphicsg){
  34. g.setColor(0x000000);
  35. g.fillRect(0,0,this.getWidth(),this.getHeight());
  36. w=getWidth();
  37. drawSqrt1(g);
  38. g.setColor(0xffffff);
  39. g.drawString("hellogv",12,33);

  40. }
  41. privatevoiddrawSqrt1(Graphicsg){
  42. longstart=System.currentTimeMillis();

  43. intcenterY1=50;

  44. //绘制X轴
  45. //设置绘图颜色为蓝色
  46. g.setColor(0x0000FF);
  47. g.drawLine(0,centerY1,w,centerY1);

  48. //设置绘图颜色为白色
  49. g.setColor(0xFFFFFF);
  50. intoldX=0;
  51. intoldY1=centerY1;

  52. inty1;
  53. for(inti=1;i<w;i++){
  54. //放大3倍
  55. y1=centerY1-(int)(3*Math.sqrt(i));
  56. g.drawLine(oldX,oldY1,i,y1);
  57. oldX=i;
  58. oldY1=y1;
  59. }
  60. longtime=System.currentTimeMillis()-start;
  61. System.out.println("drawSqrt1Runtime:"+time);
  62. }

  63. }
  64. publicvoidactionPerformed(ActionEventarg0){
  65. if(arg0.getCommand()==backCommand)
  66. {
  67. UIDemoMIDlet.backToMainMenu();
  68. }
  69. elseif(arg0.getCommand()==nextCommand)
  70. {
  71. }
  72. }
  73. }
OK,还是那句,希望大家多多支持LWUIT,让它可以做得更加好!

你可能感兴趣的:(设计模式,.net,Blog,J2SE,sun)