J2ME GUI實戰之五 ———-LWUIT的繪圖功能

來源:互聯網
上載者:User
  本文來自: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(0, 0, this.getWidth(), this.getHeight());
  36.           w = getWidth();
  37.           drawSqrt1(g);
  38.           g.setColor(0xffffff);
  39.           g.drawString("hellogv", 12, 33);
  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,讓它可以做得更加好!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.