OpenCanvas.java
上传用户:whhzxy
上传日期:2009-12-31
资源大小:269k
文件大小:6k
源码类别:

J2ME

开发平台:

Java

  1. import java.util.Timer;
  2. import javax.microedition.lcdui.*;
  3. import com.siemens.mp.io.*;
  4. /**
  5.   * 开始画板(游戏开始的显示的FLASH图片)。
  6.   * @author SoftStar,嘟嘟熊
  7.   * @version 1.0
  8.   */
  9. public class OpenCanvas
  10.     extends Canvas {
  11.   /**
  12.    * 画板宽度
  13.    */
  14.   public int canvasWidth;
  15.   /**
  16.    * 画板高度
  17.    */
  18.   public int canvasHeight;
  19.   /**
  20.    * richMan实例
  21.    */
  22.   private final KMRichMan richManObject;
  23.   /**
  24.    * 定时器
  25.    */
  26.   private Timer timer;
  27.   /**
  28.    * 要绘制的FLASH图片ID
  29.    */
  30.   private int paintImageID;
  31.   /**
  32.    * FLASH图片
  33.    */
  34.   private Image image[];
  35.   /**
  36.    * 西门子的乐谱生成器
  37.   */
  38.   com.siemens.mp.game.MelodyComposer composer;
  39.   /**
  40.    * 构造一个对象
  41.    */
  42.   public OpenCanvas(KMRichMan kmrichman) {
  43.     image = null;
  44.     timer = null;
  45.     paintImageID = 0;
  46.     richManObject = kmrichman;
  47.     image = new Image[2];
  48.     canvasWidth = getWidth();
  49.     canvasHeight = getHeight();
  50.     try {
  51.       image[0] = Image.createImage("/res/image/logobw2.png");
  52.       image[1] = Image.createImage("/res/image/logobw.png");
  53.     }
  54.     catch (Exception exception) {}
  55.     // 音乐
  56.     composer = new com.siemens.mp.game.MelodyComposer();
  57.     composer.setBPM(120);
  58.     try {
  59.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_1_8);
  60.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_16);
  61.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_16);
  62.       composer.appendNote(composer.TONE_C2, composer.TONELENGTH_DOTTED_1_16);
  63.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  64.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  65.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  66.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  67.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  68.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  69.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  70.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_1_8);
  71.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  72.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  73.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_1_8);
  74.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  75.       composer.appendNote(composer.TONE_E2, composer.TONELENGTH_DOTTED_1_16);
  76.       composer.appendNote(composer.TONE_C2, composer.TONELENGTH_DOTTED_1_16);
  77.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  78.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_16);
  79.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  80.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_1_8);
  81.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_16);
  82.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  83.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_8);
  84.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_8);
  85.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_1_8);
  86.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_16);
  87.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_16);
  88.       composer.appendNote(composer.TONE_C2, composer.TONELENGTH_DOTTED_1_16);
  89.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  90.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  91.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  92.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  93.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  94.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  95.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  96.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_8);
  97.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_8);
  98.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_1_8);
  99.       composer.appendNote(composer.TONE_G2, composer.TONELENGTH_DOTTED_1_16);
  100.       composer.appendNote(composer.TONE_E2, composer.TONELENGTH_DOTTED_1_16);
  101.       composer.appendNote(composer.TONE_C2, composer.TONELENGTH_DOTTED_1_16);
  102.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  103.       composer.appendNote(composer.TONE_G1, composer.TONELENGTH_DOTTED_1_16);
  104.       composer.appendNote(composer.TONE_PAUSE, composer.TONELENGTH_DOTTED_1_16);
  105.       composer.appendNote(composer.TONE_A1, composer.TONELENGTH_DOTTED_1_16);
  106.       composer.appendNote(composer.TONE_A1, composer.TONELENGTH_DOTTED_1_32);
  107.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_32);
  108.       composer.appendNote(composer.TONE_A1, composer.TONELENGTH_DOTTED_1_32);
  109.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_32);
  110.       composer.appendNote(composer.TONE_A1, composer.TONELENGTH_DOTTED_1_32);
  111.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_32);
  112.       composer.appendNote(composer.TONE_A1, composer.TONELENGTH_DOTTED_1_32);
  113.       composer.appendNote(composer.TONE_H1, composer.TONELENGTH_DOTTED_1_32);
  114.       composer.appendNote(composer.TONE_REPEAT, 2); // 奏乐2次
  115.       composer.getMelody().play(); //开始奏乐
  116.     }
  117.     catch (Exception ex) {
  118.       ex.printStackTrace();
  119.     }
  120.     timer = new Timer();
  121.     timer.schedule(new OpenCanvasTimerTask(this,richManObject), 10L);
  122.   }
  123.   /**
  124.    * 切换FLASH图片
  125.    */
  126.   public void paintOpenCanvas() {
  127.     paintImageID++;
  128.     image[paintImageID - 1] = null;
  129.     repaint();
  130.   }
  131.   /**
  132.    * 绘制画板
  133.    */
  134.   public void paint(Graphics g) {
  135.     if (image[paintImageID] != null) {
  136.       g.drawImage(image[paintImageID], 0, 0, 20);
  137.     }
  138.     if (paintImageID == 1 && timer != null) {
  139.       timer.cancel();
  140.       timer = null;
  141.     }
  142.   }
  143.   /**
  144.    * 处理按键
  145.    */
  146.   public void keyPressed(int i) {
  147.     if (paintImageID != 1) {
  148.       return;
  149.     }
  150.     else {
  151.       richManObject.setDisplayToGameMenu();
  152.       return;
  153.     }
  154.   }
  155. }