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

WAP之家技术文章J2ME技术进阶教程使用Timer制作动画效果

使用Timer制作动画效果
作者:j2medev  来源:j2medev  发布时间:2006-7-25 8:46:16

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

public class TimerDemo extends MIDlet {

  Display    display;
  StarField  field = new StarField();
  FieldMover mover = new FieldMover();
  Timer      timer = new Timer();

  public TimerDemo() {
    display = Display.getDisplaythis );
  }

  protected void destroyAppboolean unconditional ) { }

  protected void startApp() {
    display.setCurrentfield );
    timer.schedulemover, 100100 );
  }

  protected void pauseApp() { }

  public void exit(){
    timer.cancel()// stop scrolling
    destroyApptrue );
    notifyDestroyed();
  }

class FieldMover extends TimerTask {
  public void run(){
    field.scroll();
  }
}

class StarField extends Canvas {
  int        height;
  int        width;
  int[]      stars;
  Random     generator = new Random();
  boolean    painting = false;

  public StarField(){
    height      = getHeight();
    width       = getWidth();
    stars       = new intheight ];

    forint i = 0; i < height; ++i ){
      stars[i= -1;
    }
  }

  public void scroll() {
    ifpainting return;

    forint i = height-1; i > 0; --i ){
      stars[i= stars[i-1];
    }

    stars[0generator.nextInt() * width ) ) 2;
    ifstars[0>= width ){
      stars[0= -1;
    }

    repaint();
  }

  protected void paintGraphics g ){
    painting = true;

    g.setColor00);
    g.fillRect00, width, height );

    g.setColor255255255 );

    forint y = 0; y < height; ++y ){
      int x = stars[y];
      ifx == -continue;

      g.drawLinex, y, x, y );
    }

    painting = false;
  }

  protected void keyPressedint keyCode ){
    exit();
  }
}
}

 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

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

内 容:

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