WEB开发网
开发学院软件开发Java J2ME GUI实战之五 ----------LWUIT的绘... 阅读

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

 2009-09-12 00:00:00 来源:WEB开发网   
核心提示: 接着再给出绘图功能实现的代码: 1. /* 2. * Copyright ?2008 Sun Microsystems, Inc. All rights reserved. 3. * Use is subject to license terms. 4. * 5. */ 6. package c

接着再给出绘图功能实现的代码:

   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.

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

  16.


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

  34.     }
  35.   public class Painting extends Component{
  36.         private int w;
  37.    public void paint(Graphics g) {
  38.           g.setColor(0x000000);
  39.           g.fillRect(0, 0, this.getWidth(), this.getHeight());
  40.           w = getWidth();
  41.           drawSqrt1(g);
  42.           g.setColor(0xffffff);
  43.           g.drawString("hellogv", 12, 33);
  44.

  45.       }
  46.            private void drawSqrt1(Graphics g) {
  47.             long start = System.currentTimeMillis();
  48.

  49.             int centerY1 = 50;
  50.

  51.             //绘制X轴
  52.             //设置绘图颜色为蓝色
  53.             g.setColor(0x0000FF);
  54.             g.drawLine(0, centerY1, w, centerY1);
  55.

  56.             //设置绘图颜色为白色
  57.             g.setColor(0xFFFFFF);
  58.             int oldX = 0;
  59.             int oldY1 = centerY1;
  60.

  61.             int y1;
  62.             for(int i=1;i<w;i++) {
  63.                 //              放大3倍
  64.                 y1 = centerY1 - (int)(3*Math.sqrt(i));
  65.                 g.drawLine(oldX, oldY1, i, y1);
  66.                 oldX = i;
  67.                 oldY1 = y1;
  68.             }
  69.             long time = System.currentTimeMillis() - start;
  70.             System.out.println("drawSqrt1 Runtime: " + time);
  71.         }
  72.

  73.   }
  74.     public void actionPerformed(ActionEvent arg0) {
  75.         if(arg0.getCommand()==backCommand)
  76.         {
  77.             UIDemoMIDlet.backToMainMenu();
  78.         }
  79.         else if(arg0.getCommand()==nextCommand)
  80.         {
  81.          
  82.         }
  83.     }
  84. }

OK,还是那句,希望大家多多支持LWUIT,让它可以做得更加好!

上一页  1 2 

Tags:JME GUI 实战

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接