| 网站首页 | 无线增值 | 软件下载 | 电信 | 手机 | 手机游戏 | 手机软件 | 手机电子书 | 网中游论坛 | CP.SP论坛 | 网站投稿 | 
您现在的位置: CP.SP论坛 >> 无线增值 >> 技术文章 >> J2ME技术 >> 文章正文 用户登录 新用户注册
2D绘画操作中的坐标系统变换代码         ★★★ 【字体:
2D绘画操作中的坐标系统变换代码
作者:未知    文章来源:转载    点击数:    更新时间:2008-1-26    

  private translatecanvas canvas;   // canvas 
  public translate()
  {
    display = display.getdisplay(this);
    canvas  = new translatecanvas(this);
  }
  protected void startapp()
  {
    display.setcurrentcanvas );
  }
  protected void pauseapp()
  { }
 
  protected void destroyappboolean unconditional )
  { }
  public void exitmidlet()
  {
    destroyapp(true);
    notifydestroyed();
  }
}
 
/*--------------------------------------------------
* class translate
*
* draw image using translated coordinates
*-------------------------------------------------*/
class translatecanvas extends canvas implements commandlistener
{
  private command cmexit;  // exit midlet
  private translate midlet;
  private image im = null;
  private int translatex = 0, translatey = 0;
  public translatecanvas(translate midlet)
  {
    this.midlet = midlet;
    // create exit command & listen for events
    cmexit = new command("exit", command.exit, 1);
    addcommand(cmexit);
    setcommandlistener(this);
 
    try
    {
      // create immutable image
      im = image.createimage("/bolt.png");
    }
    catch (java.io.ioexception e)
    {
      system.err.println("unable to locate or read .png file");
    }    
  
 
  protected void paint(graphics g)
  {
    if (im != null)
    {
      // clear the background
      g.setcolor(255255255);
      g.fillrect(00, getwidth(), getheight());
      // translate coordinates
      g.translate(translatex, translatey);      
      // always draw at 0,0
      g.drawimage(im, 00, graphics.left | graphics.top);
    }
  }
 
  public void commandaction(command c, displayable d)
  {
    if (c == cmexit)
      midlet.exitmidlet();
  }
  protected void keypressed(int keycode)
  {
    switch (getgameaction(keycode))
    {
      case up: 
        // if scrolling off the top, roll around to bottom
        if (translatey - im.getheight() 0)
          translatey = getheight() - im.getheight();
        else
          translatey -= im.getheight();          
        break;
      case down:
 
        // if scrolling off the bottom, roll around to top
        if ((translatey + im.getheight() + im.getheight()) > getheight())
          translatey = 0;
        else
          translatey += im.getheight();          
        break;
      case left:
        // if scrolling off the left, bring around to right
        if (translatex - im.getwidth() 0)
          translatex = getwidth() - im.getwidth();
        else
          translatex -= im.getwidth();          
        break;
 
      case right:
        // if scrolling off the right, bring around to left
        if ((translatex + im.getwidth() + translatex> getwidth())
          translatex = 0;
        else
          translatex += im.getwidth();          
        break;
    }        
    repaint();
  }
}

文章录入:十月    责任编辑:十月 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    没有相关文章
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)