只要你明白了时间的珍贵,去发现一件自己想做的事情,那你的脚下的路就会慢慢的晴朗起来。
首页 arrow 开发技术 arrow 新手上路 arrow 使用指针事件在Canvas上绘画

使用指针事件在Canvas上绘画 输出PDF 打印 E-mail
Author Author: 一滴蔚蓝色 | Date Date: 2008-05-10 | View Count View: 383 | section & Category 开发技术 -  新手上路
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.setCurrent( canvas );
  }
  protected void pauseApp()
  { }

  protected void destroyApp( boolean 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(255, 255, 255);
      g.fillRect(0, 0, getWidth(), getHeight());
      clearDisplay = false;
      startx = currentx = starty = currenty = 0;
      return;
    }
    // Draw with black pen
    g.setColor(0, 0, 0);
    // 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();
  }
}


收藏到您的网摘: Google书签 Yahoo书签 雅虎收藏夹 365Key网摘 新浪ViVi 百度收藏 天极网摘 diglog 和讯网摘 POCO网摘 YouNote网摘 博拉网 天下图摘 spurl blogmarks BlinkList reddit digg Del.icio.us

本文关键字本文关键字: 指针  事件  

阅读数: 384 | 打印 | E-mail

  评论 (1)
RSS评论
 1 评论者 kls, 时间 2008-05-29 16:56
这位大哥竟然没有告诉大家要实现指针要先该 配置文件。不然是没有触摸屏的指针的!把配 置文件中的touch_screen参数该为true就可以了! 然大家把模拟器按碎了也画不出直线来。

发表评论

姓名:
E-mail
您的网站/主页
标题:
评论:

验证码:* Code
若有人评论本文,请E-mail通知我。

(J2ME中用低级界面实现简单的数字输入) < 上一篇   下一篇 > (j2me读取Unicode格式)