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

  <script>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();}</script> 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:


首先,需要说明一下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.     private class ButtonActionListener implements ActionListener {
  2.         public void actionPerformed(ActionEvent evt) {
  3.           String sel_button=((Button)(evt.getSource())).getText();//取得所选按钮的名称
  4.           if(sel_button.equals("Image 1"))
  5.               new AnimationDemo().form.show();
  6.           else if(sel_button.equals("Image 2"))
  7.               new PaintingDemo().form.show();
  8.         }
  9.     }
接着再给出绘图功能实现的代码:
  1. /*
  2.  * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
  3.  * Use is subject to license terms.
  4.  *
  5.  */
  6. package com.sun.lwuit.uidemo;

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


  14. /**
  15.  * Demonstrates simple animation both static and manual
  16.  *
  17.  * @author Shai Almog
  18.  */
  19. public class PaintingDemo   extends Form implements ActionListener {
  20.     public Form form = new Form("PaintingDemo");
  21.     private  Command backCommand = new Command("Back"1);
  22.     private  Command nextCommand = new Command("next"2);
  23.     PaintingDemo()
  24.     {
  25.             form.addCommand(backCommand);
  26.             form.addCommand(nextCommand);
  27.             form.setCommandListener(this);
  28.             form.setLayout(new BorderLayout());
  29.             form.addComponent(BorderLayout.CENTER, new Painting());

  30.     }
  31.   public class Painting extends Component{
  32.         private int w;
  33.    public void paint(Graphics g) {
  34.           g.setColor(0x000000);
  35.           g.fillRect(00this.getWidth(), this.getHeight());
  36.           w = getWidth();
  37.           drawSqrt1(g);
  38.           g.setColor(0xffffff);
  39.           g.drawString("hellogv"1233);

  40.       }
  41.            private void drawSqrt1(Graphics g) {
  42.             long start = System.currentTimeMillis();

  43.             int centerY1 = 50;

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

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

  52.             int y1;
  53.             for(int i=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.             long time = System.currentTimeMillis() - start;
  61.             System.out.println("drawSqrt1 Runtime: " + time);
  62.         }

  63.   }
  64.     public void actionPerformed(ActionEvent arg0) {
  65.         if(arg0.getCommand()==backCommand)
  66.         {
  67.             UIDemoMIDlet.backToMainMenu();
  68.         }
  69.         else if(arg0.getCommand()==nextCommand)
  70.         {
  71.           
  72.         }
  73.     }
  74. }
OK,还是那句,希望大家多多支持LWUIT,让它可以做得更加好!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值