หน้าแรก
 การบ้าน
  C/C++ new
  Java new
  Windows Phone
  แจ้งเรื่องที่ต้องการ
 บทความ
  .NET
  Android
  Blackberry
  C++Builder
  Delphi
  Java
  Mobile
  Open Source
  Pocket PC
  Visual Basic
  Web
  Windows Phone
  Others
 แบบเรียนฟรี
  ASP
  ASP.NET Webmatrix
  C++
  eVB
  J2ME
  Java
  PHP
 ซอร์สโค้ด
  Android
  ASP
  C++
  VC++
  CGI-Perl
  Delphi
  Java
  jQuery
  PHP
  Visual Basic
  .NET
 บริการจากเรา
  เปิดท้าย... ขายโค้ด
  Freeware
  อัตราค่าโฆษณา
  รายชื่อลูกค้า
  สถิติผู้เข้าเว็บ
 ดาว์นโหลด
 ค้นหาข้อมูล
 กระดานสนทนา
  .NET
  C/C++
  Delphi
  Java
  Palm/PocketPC
  Visual Basic
  Web
  อื่นๆ
 กระดานงาน
 ลิงค์เว็บ
 เกี่ยวกับผม
 อัตราค่าโฆษณา
Social Network

Facebook  Twitter  YouTube
 New Article
 Webboard
 Freelanceboard
Freeware
โปรแกรมเบอร์ดี (BerDee)
โปรแกรมเบอร์ดี (Android)
เกมส์เป่ายิ้งฉุบ
เกมส์เป่ายิ้งฉุบ(Android)
WebcamMonitor
WebcamMonitor(Windows)
scSendMail
scSendMail(Windows)
MSN Caption
MSN Caption(Windows)
  Freelance comment
  ติดต่อสอบถามมาได้ตลอดนะ...
2015-07-29 13:35:58
  ถ้าผมต้องการเว็บที่พัฒน...
2015-01-18 15:33:54
  ถ้าผมต้องการเว็บที่พัฒน...
2015-01-18 15:32:51
  ติดต่อสอบถามเข้ามาได้นะ...
2014-01-06 12:47:38
  ถ้ายังหาคนสอนไม่ได้ ก็ลอ...
2013-07-06 01:04:37
  สนใจส่งขอบเขตมาคุยราคาก...
2013-03-24 18:54:20
  ถ้ายังไม่มีคนรับงานติดต...
2012-12-16 19:18:14
  สนใจคะ ติดต่อ 0905076277...
2012-11-12 11:07:46
  รับทำโปรเจ็คนักศึกษาหรื...
2012-10-29 03:10:46
  sukit_te34@hotmail.com...
2012-10-29 03:09:36
  Webboard comment
  เขียนโปรแกรมภาษาจาวา
1.โ...

2023-09-12 11:40:28
  ต้องลอง debug ดูเองครับ
ไล่...

2023-06-08 18:03:06
  ร้านค้าแห่งหนึ่ง จะต้องน...
2021-09-21 08:47:08
  จงเขียนโปรแกรมดำเนินการ...
2021-05-07 15:06:11
  จงเขียนโปรแกรมสำหรับคำน...
2021-03-09 14:21:45
  ขอดู...
2021-01-04 23:56:45
  ขอโค้ดเพื่อทำการเรียนกา...
2020-12-09 23:02:09
  แล้ว ?...
2020-12-09 10:56:34
  จ้า...
2020-10-18 16:53:21
  พอดีผมเหลื่อแค่ตัดสต๊อก...
2020-05-31 21:49:43
  Homework comment
  เขียนโปรแกรมเพื่อรับค่า...
2022-02-24 10:45:17
  จากโค้ดนี้อะครับ พอดีลอง...
2021-03-22 01:22:39
  จากโค้ดนี้อะครับ พอดีลอง...
2021-03-22 01:22:39
  โจทย์การบ้าน c++ เขียนรูปแ...
2020-04-06 11:01:33
  แบบนี้ขอเป็น ภาษา php หน่อย...
2019-09-18 14:36:34
  ผมไม่อยากให้คุณได้คะแนน...
2019-04-27 01:29:07
  อาจารย์เขาบอกแนวข้อสอบม...
2019-04-27 00:44:29
  ขอสอบถามการเขียน c++ ครับ เ...
2018-04-02 12:19:21
  โค้ดตัวอย่างศึกษาให้เข้...
2017-11-26 14:50:05
  คำนวณค่าน้ำโดยรับค่ามิเ...
2017-11-20 23:15:26
  Article comment
  เท่าที่อ่าน error คือพื้นที่...
2022-03-23 13:56:48
  เรียน สอบถามปัญหา กับ อ.คร...
2022-01-24 17:39:49
  ต้องการ ให้เขียน โปรแกรม ...
2019-12-09 11:39:58
  รับเขียน arx จาก vc++ 2017 ล็อคโปร...
2019-09-19 09:48:09
  ทำการ register ไฟล์ที่ชื่อ mswinsck.oc...
2019-09-17 14:05:56
  ใน vb 6 ไม่มี component winsock เลยค่ะ ส...
2019-09-03 10:31:02
  รบกวนขอสอบถามหน่อยนะครั...
2019-03-04 05:31:04
  สามารถนำตัวหนังสือจากภา...
2018-12-25 08:54:32
  มีcode ของ VB ไหมค่ะ ถ้ามีรบกว...
2017-09-28 16:50:02
  น่าจะได้ครับ ไม่เคยลอง
...

2017-07-11 09:59:35
  9Mcode comment

Link Exchange

อัตราค่าโฆษณา
 
 J2ME (Java Micro Edition) Tutorial  

- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -

 J2ME (Java Micro Edition)
 
 

บทที่ 5 ภาคปฏิบัติ

 

บทที่ 5 ภาคปฏิบัติ

ตัวอย่างที่ 1 Hello Canvas

HelloCanvas.java

Download Code Download Code
/*
 * HelloCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:05 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  suppakit
 * @version
 */
public class HelloCanvas extends MIDlet implements CommandListener {
    
    private Command exitCommand; // The exit command
    private Display display;    // The display for this MIDlet
    
    public HelloCanvas() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.SCREEN, 2);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
        SimpleCanvas canvas = new SimpleCanvas();
        canvas.addCommand(exitCommand);
        canvas.setCommandListener(this);        
        display.setCurrent(canvas);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

SimpleCanvas.java

Download Code Download Code
/*
 * SimpleCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class SimpleCanvas extends Canvas {
    /**
     * paint
     */
    public void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(0,0,0);
        g.drawString("Hello Canvas",0,getHeight()/2,Graphics.TOP | Graphics.LEFT);
    }   
   
}

ผลการ Run

HelloCanvas MIDlet
รูปที่ 42 แสดงหน้าต่าง Emulator ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: จะสังเกตุได้ว่า MIDlet ตัวนี้ ประกอบด้วย ไฟล์ 2 ส่วน คือ ส่วนแรก ที่เป็น MIDlet หลัก ที่สืบทอด มาจากคลาส MIDlet และ ส่วนที่สอง เป็นคลาส SimpleCanvas ซึ่งสืบทอดมาจาก Canvas

ซึ่งในการสร้างจะเริ่มต้นจาก การสร้าง MIDlet ตัวแรก ตามแบบที่เคยสร้างๆกันมา จากบทที่แล้ว แต่จะเพิ่มไฟล์ SimpleCanvas ที่จะถูกเก็บไว้ใน path เดียวกับ MIDlet ตัวหลัก เพื่อให้สามารถเรียกใช้งาน เพื่อแสดงผล ดังรูปได้


ตัวอย่างที่ 2 Font MIDlet

FontMIDlet.java

Download Code Download Code
/*
 * FontMIDlet.java
 *
 * Created on 17 กันยายน 2546, 16:05 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  suppakit
 * @version
 */
public class FontMIDlet extends MIDlet implements CommandListener {
    
    private Command exitCommand; // The exit command
    private Display display;    // The display for this MIDlet
    
    public FontMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.SCREEN, 2);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
   Displayable d = new FontCanvas();
   Display.getDisplay(this).setCurrent(d);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

FontCanvas.java

Download Code Download Code
/*
 * FontCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class FontCanvas extends Canvas  implements CommandListener {
  private Font mSystemFont, mMonospaceFont, mProportionalFont;
  private Command mBoldCommand, mItalicCommand, mUnderlineCommand;

public FontCanvas(){
  this(Font.STYLE_PLAIN);
}

public FontCanvas(int style){
  setStyle(style);
  mBoldCommand = new Command("Bold", Command.SCREEN, 0);
  mItalicCommand = new Command("Italic", Command.SCREEN, 0);
  mUnderlineCommand = new Command("Underline", Command.SCREEN, 0);
  addCommand(mBoldCommand);
  addCommand(mItalicCommand);
  addCommand(mUnderlineCommand);
  setCommandListener(this);
}

  public void setStyle(int style){
    mSystemFont = Font.getFont(Font.FACE_SYSTEM, style, Font.SIZE_MEDIUM);
    mMonospaceFont = Font.getFont(Font.FACE_MONOSPACE, style, Font.SIZE_MEDIUM);
    mProportionalFont = Font.getFont(Font.FACE_PROPORTIONAL, style, Font.SIZE_MEDIUM);   
  }

    /**
     * paint
     */
    public void paint(Graphics g) {
        int w = getWidth();
   int h = getHeight();

   //Clear the Canvas
   g.setGrayScale(255);
   g.fillRect(0, 0, w-1, h-1);
   g.setGrayScale(0);
   g.drawRect(0, 0, w-1, h-1);

   int x = w/2;
   int y=20;

   y += showFont(g, "System", x, y, mSystemFont);
   y += showFont(g, "Monospace", x, y, mMonospaceFont);
   y += showFont(g, "Proportional", x, y, mProportionalFont);

    }   
  
  private int showFont(Graphics g, String s, int x, int y, Font f){
    g.setFont(f);
    g.drawString(s, x, y, Graphics.TOP | Graphics.HCENTER);
    return f.getHeight();

  }
  
  public void commandAction(Command c, Displayable s){
    boolean isBold = mSystemFont.isBold()^(c == mBoldCommand);
    boolean isItalic = mSystemFont.isItalic()^(c == mItalicCommand);
    boolean isUnderline = mSystemFont.isUnderlined()^(c == mUnderlineCommand);

    int style =
		(isBold ? Font.STYLE_BOLD : 0)|
		(isItalic ? Font.STYLE_ITALIC : 0)|
		(isUnderline ? Font.STYLE_UNDERLINED : 0);
    setStyle(style);
    repaint();

  }
   
}

ผลการ Run

FontMIDlet
รูปที่ 43 แสดงหน้าต่าง Emulator ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างในการแสดง ฟอนท์ แบบต่างๆ


ตัวอย่างที่ 3 Image MIDlet

  • หลังจากที่คุณได้สร้าง Project ใหม่ที่ชื่อ ImageMIDlet แล้ว
  • ให้ทำการคัดลอกไฟล์ C:\WTK20\apps\photoalbum\res\icons\Duke.png เอาไปไว้ใน C:\WTK20\apps\ImageMIDlet\res
  • คลิกปุ่ม Setting ที่หน้าต่าง ของ J2ME Wireless Tookits
  • เลือกที่แท็บ User Define
  • คลิกปุ่ม Add
  • กำหนด Property Name เป็น Duke คลิก OK
  • ที่ช่อง Value ป้อนข้อความ \res\Duke.png
  • คลิกปุ่ม OK
  • ต่อจากนั้น ก็ให้สร้างไฟล์ ImageCanvas.java แล้วบักทึกไว้ใน path ของ src ของ โปรเจ็คของคุณ
  • ทำการ Build แล้ว Run จะได้ผลตามรูป

ImageMIDlet.java

Download Code Download Code
/*
* ImageMIDlet.java
*
* Created on 17 กันยายน 2546, 16:05 น.
*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* An example MIDlet with simple "Hello" text and an Exit command.
* Refer to the startApp, pauseApp, and destroyApp
* methods so see how each handles the requested transition.
*
* @author suppakit
* @version
*/

public class ImageMIDlet extends MIDlet implements CommandListener {

private Command exitCommand; // The exit command

public ImageMIDlet() {
exitCommand = new Command("Exit", Command.SCREEN, 2);
}

/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/

public void startApp() {
Displayable d = new ImageCanvas();
Display.getDisplay(this).setCurrent(d);
}

/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/

public void pauseApp() {
}

/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/

public void destroyApp(boolean unconditional) {
}

/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}

}

ImageCanvas.java

Download Code Download Code
/*
 * ImageCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.lcdui.*;
import javax.microedition.io.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class ImageCanvas extends Canvas {
    /**
     * paint
     */
    public void paint(Graphics g) {
   g.setColor(255,255,255);
   g.fillRect(0, 0, getWidth(), getHeight());

   try{
    Image icon = Image.createImage("/Duke.png");
    g.drawImage(icon,10,10, Graphics.TOP|Graphics.LEFT);
    g.setColor(128,128,128);
    g.drawRect(9, 9, icon.getWidth()+1, icon.getHeight()+1);
   }catch(java.io.IOException ex){
    g.drawString("No image",10,10, Graphics.TOP|Graphics.LEFT);
   }
 
    }   
  
  
}

ผลการ Run

Image MIDlet
รูปที่ 44 แสดงหน้าต่าง Emulator ของ Nokia 6650
ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างในการป้อนหมายเลขโทรศัพท์ เข้าไปยัง TextBox พร้อมปุ่ม OK และ Exit


ตัวอย่างที่ 4 TextBox MIDlet

BoxTextMIDlet.java

Download Code Download Code
/*
 * BoxTextMIDlet.java
 *
 * Created on 17 กันยายน 2546, 16:05 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  suppakit
 * @version
 */
public class BoxTextMIDlet extends MIDlet implements CommandListener {
    
    private Command exitCommand; // The exit command   
    
    public BoxTextMIDlet() {        
        exitCommand = new Command("Exit", Command.SCREEN, 2);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
   Displayable d = new BoxTextCanvas();
   Display.getDisplay(this).setCurrent(d);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

BoxTextCanvas.java

Download Code Download Code
/*
 * BoxTextCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class BoxTextCanvas extends Canvas {
  private Font mFont;
  
  public BoxTextCanvas(){
    mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
  }

    public void paint(Graphics g) {
   int w = getWidth();
   int h = getHeight();
   g.setColor(255,255,255);
   g.fillRect(0,0,w,h);
   g.setColor(0,0,0);		
   String s = "dolce";
   int stringWidth = mFont.stringWidth(s);
   int stringHeight = mFont.getHeight();
   int x = (w-stringWidth)/2;
   int y = h/2;
   g.setFont(mFont);
   g.drawString(s, x, y, Graphics.TOP|Graphics.LEFT);
   g.drawRect(x, y, stringWidth, stringHeight);
 
    }   
  
  
}

ผลการ Run

BoxText MIDlet
รูปที่ 45 แสดงหน้าต่าง Emulator ของ Nokia 6650
ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างที่แสดงถึง การสร้าง BoxTextCanvas เพื่อแสดงข้อความ พร้อมกรอบ ที่ต้องการ


ตัวอย่างที่ 5 KeyMIDlet

KeyMIDlet.java

Download Code Download Code
/*
 * KeyMIDlet.java
 *
 * Created on 17 กันยายน 2546, 16:05 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  suppakit
 * @version
 */
public class KeyMIDlet extends MIDlet implements CommandListener {
    
    private Command exitCommand; // The exit command   
    
    public KeyMIDlet() {        
        exitCommand = new Command("Exit", Command.SCREEN, 2);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
   Displayable d = new KeyCanvas();
   Display.getDisplay(this).setCurrent(d);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

KeyCanvas.java

Download Code Download Code
/*
 * KeyCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class KeyCanvas extends Canvas {
 private Font mFont;
 String mMessage = "[Press keys]";
 
  
  public KeyCanvas(){
  mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    
  }

    public void paint(Graphics g) {
   int w = getWidth();
   int h = getHeight();

   //Clear Canvas
   g.setGrayScale(255);
   g.fillRect(0,0,w,h);
   g.setGrayScale(0);
   g.drawRect(0,0,w-1,h-1);
   g.setFont(mFont);

   int x = w/2;
   int y = h/2;

   g.drawString(mMessage, x, y, Graphics.BASELINE|Graphics.HCENTER); 
    }   
  
    protected void keyPressed(int keyCode){
   int gameAction = getGameAction(keyCode);
   switch(gameAction){
    case UP: mMessage = "UP"; break;
    case DOWN: mMessage = "DOWN"; break;
    case LEFT: mMessage = "LEFT"; break;
    case RIGHT: mMessage = "RIGHT"; break;
    case FIRE: mMessage = "FIRE"; break;
    case GAME_A: mMessage = "GAME_A"; break;
    case GAME_B: mMessage = "GAME_B"; break;
    case GAME_C: mMessage = "GAME_C"; break;
    case GAME_D: mMessage = "GAME_D"; break;
    default : mMessage = ""; break;

   }
  repaint(); 
  }

  
}

ผลการ Run

Key MIDlet
รูปที่ 46 แสดงหน้าต่าง Emulator ของ Nokia 6650
ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างที่แสดงถึง การรับปุ่มกดต่างๆ ที่ใช้ในการสร้างเกมส์ บนระบบ J2ME


ตัวอย่างที่ 6 OffScreen MIDlet

OffScreenMIDlet.java

Download Code Download Code
/*
 * OffScreenMIDlet.java
 *
 * Created on 17 กันยายน 2546, 16:05 น.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  suppakit
 * @version
 */
public class OffScreenMIDlet extends MIDlet implements CommandListener {
    
    private Command exitCommand; // The exit command   
    
    public OffScreenMIDlet() {        
        exitCommand = new Command("Exit", Command.SCREEN, 2);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
   Displayable d = new OffScreenCanvas();
   Display.getDisplay(this).setCurrent(d);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

OffScreenCanvas.java

Download Code Download Code
/*
 * OffScreenCanvas.java
 *
 * Created on 17 กันยายน 2546, 16:07 น.
 */

import javax.microedition.lcdui.*;

/**
 *
 * @author  suppakit
 * @version
 */
public class OffScreenCanvas extends Canvas {
 private Image mImage;

 
  
    public void paint(Graphics g) {
   if(mImage == null){
   initialize();
   g.drawImage(mImage, 0, 0, Graphics.TOP|Graphics.LEFT);
  }
    
  }

    public void initialize(){
     int w = getWidth();
     int h = getHeight();
   mImage = Image.createImage(w,h);
   Graphics g = mImage.getGraphics();
   g.drawRect(0,0,w-1,h-1);
   g.drawLine(0,0,w-1,h-1);
   g.drawLine(w-1,0,0,h-1);
   
    }    
   

  
}

ผลการ Run

OffScreen MIDlet
รูปที่ 47 แสดงหน้าต่าง Emulator ของ Nokia 6650
ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างที่แสดงถึง การแสดงภาพ ที่สร้างขึ้นมาโดยใช้ คลาส Image

 

BACK

คลิปสอนเขียนโปรแกรม
เรียนเขียนโปรแกรมดอทเน็ต
เรียนเขียนโปรแกรมแอนดรอยด์
เรียนเขียนโปรแกรมเบล็คเบอร์รี่
เรียนเขียนโปรแกรมซี ซีพลัสพลัส
เรียนเขียนโปรแกรมจาวา
เรียนการใช้งานลินุกส์
เรียนการเขียนโปรแกรมวินโดว์โฟน
เรียนการเขียนโปรแกรมพีเฮชพี มายเอสคิวเอล
อัตราค่าโฆษณา
 
Tutorial
eVB Tutorial
ASP.NET Webmatrix Tutorial
J2ME Tutorial
C++  Tutorial
Java  Tutorial
PHP Tutorial
ASP Tutorial
 
แบบสำรวจ

Home - Article - Tutorial - Sourcecode - Dev Handbook - Search - WebBoard - Links - About Us

สงวนลิขสิทธิ์ ห้ามคัดลอก ทำซ้ำ แก้ไข ดัดแปลง ไม่ว่าในรูปแบบใด โดยไม่ได้รับอนุญาตเป็นลายลักษณ์อักษร
เลขทะเบียนพาณิชย์อิเล็กทรอนิกส์จากกระทรวงพาณิชย์: 7100503002000
Copyright 2001 SourceCode.in.th