WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程使用指针事件在Canvas上绘画

使用指针事件在Canvas上绘画
作者:j2medev  来源:j2medev.com  发布时间:2006-7-25 8:39:38
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Doodle extends MIDlet
{
  private Display  display;       // The display
  private DoodleCanvas canvas;   // Canvas 
  public Doodle()
  {
    display = Display.getDisplay(this);
    canvas  = new DoodleCanvas(this);
  }
  protected void startApp()
  {
    display.setCurrentcanvas );
  }
  protected void pauseApp()
  { }

  protected void destroyAppboolean unconditional )
  { }
  public void exitMIDlet()
  {
    destroyApp(true);
    notifyDestroyed();
  }
}

/*--------------------------------------------------
* Class DoodleCanvas
*
* Pointer event handling
*-------------------------------------------------*/
class DoodleCanvas extends Canvas implements CommandListener
{
  private Command cmExit;          // Exit midlet
  private Command cmClear;         // Clear display
  private int startx = 0,   // Where pointer was clicked
              starty = 0,
              currentx = 0// Current location
              currenty = 0;
  private Doodle midlet;
  private boolean clearDisplay = false;

  /*--------------------------------------------------
  * Constructor
  *-------------------------------------------------*/
  public DoodleCanvas(Doodle midlet)
  {
    this.midlet = midlet;
    // Create exit command & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    cmClear = new Command("Clear", Command.SCREEN, 1);    
    addCommand(cmExit);
    addCommand(cmClear);
    setCommandListener(this);
  

  /*--------------------------------------------------
  * Paint the text representing the key code 
  *-------------------------------------------------*/
  protected void paint(Graphics g)
  {
    // Clear the background (to white)
    if (clearDisplay)
    {
      g.setColor(255255255);
      g.fillRect(00, getWidth(), getHeight());
      clearDisplay = false;
      startx = currentx = starty = currenty = 0;
      return;
    }
    // Draw with black pen
    g.setColor(000);
    // Draw line
    g.drawLine(startx, starty, currentx, currenty);
    // New starting point is the current position
    startx = currentx;
    starty = currenty;
  }

  /*--------------------------------------------------
  * Command event handling
  *-------------------------------------------------*/  
  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
    else if (c == cmClear)
    {
      clearDisplay = true
      repaint();
    }
  }

  /*--------------------------------------------------
  * Pointer pressed
  *-------------------------------------------------*/  
  protected void pointerPressed(int x, int y)
  {
    startx = x;
    starty = y;
  }

  /*--------------------------------------------------
  * Pointer moved
  *-------------------------------------------------*/  
  protected void pointerDragged(int x, int y)
  {
    currentx = x;
    currenty = y;       
    repaint();
  
}
[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码