⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgcanvas.java

📁 J2me实现的MSN Messeger客户端程序。聊天、添加好友、删除好友、阻止好友
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

  /**
   *线程休眠n毫秒
   */
  public void sleep(int Time) {
    try {
      this.wait(Time);
    }
    catch (Exception e) {}
  }

  /**
   *停止线程
   */
  public void stop() {
    pausethread = true;
    stopthread = true;
    thread = null;
    System.gc();
  }

  /**
   * 重载了getHeight的方法
   * 其实是玩了个小把戏,让用户得到的屏幕高度是除去消息以外的高度
   * @return int
   */
  public int getRealHeight(){
    //return ScreenHeight;
    if (bShowMsg){
      //System.out.println("Get Height "+(this.ScreenHeight - this.msgHeight));
      return this.ScreenHeight - this.msgHeight;
    }else{
      //System.out.println("Get Height "+this.ScreenHeight);
      return this.ScreenHeight;
    }
  }

  /*public Graphics getScreenG() {
    return screenG;
  }*/

  /**
   *线程恢复
   */
  public void resume() {
    pausethread = false;
  }

  /**
   *设置线程跳动的间隙时间
   */
  public void setInterval(int Interval) {
    interval = Interval;
  }

  /**
   *得到线程跳动的间隙时间
   */
  public int getInterval() {
    return interval;
  }

  /**
   *线程是否停止
   */
  public boolean isStop() {
    return stopthread;
  }

  /**
   *线程是否暂停
   */
  public boolean isPause() {
    return pausethread;
  }

  /**
   * 设置游戏状态
   * @param Status int
   */
  public void setStatus(int Status) {
    appStatus = Status;
  }

  /**
   * 获取游戏状态
   * @return int
   */
  public int getAppStatus() {
    return appStatus;
  }

  /**
   * 获取按键状态值
   * @return float
   */
  public int getKeyPressed() {
    return keyStatus;
  }

  /**
   * 显示文本信息
   * @param text String 内容
   * @param title String 标题
   */
  public void showTextBoard(String text, String title) {
    try{
      textBoard = new TextBoard(this, text, title);
    }catch(Exception e){
      System.out.println(e.toString());
    }
    textBoard.setListener(this); //设置监听器
    setStatus(STATUS_TEXTBOARD);
    setButton(this.BUTTON_TYPE_NONE, this.BUTTON_TYPE_CANCEL);
  }

  /**
   * 显示文本信息 在指定区域
   * @param text String 内容
   * @param x int  (x, y, w, h)显示区域
   * @param y int
   * @param w int
   * @param h int
   */
  public void showTextBoard(String text, int x, int y, int w, int h) {
    textBoard = new TextBoard(this, text, null, 1, x, y, w, h);
    textBoard.setListener(this);
    setStatus(STATUS_TEXTBOARD);
    setButton(this.BUTTON_TYPE_NONE, this.BUTTON_TYPE_CANCEL);
  }

  /**
   * 显示文本 在指定区域用指定颜色
   * @param text String
   * @param bColor int
   * @param fColor int
   * @param x int
   * @param y int
   * @param w int
   * @param h int
   */
  public void showTextBoard(String text, int bColor, int fColor, int x, int y,
                            int w, int h) {
    textBoard = new TextBoard(this, text, null, 1, bColor, fColor, x, y, w, h);
    textBoard.setListener(this);
    setStatus(STATUS_TEXTBOARD);
    setButton(this.BUTTON_TYPE_NONE, this.BUTTON_TYPE_CANCEL);
  }

  /**
   * 设置下角button位置和对应类型
   * @param typeLeft int 左边类型
   * @param typeRight int右边类型
   */
  public void setButton(int typeLeft, int typeRight) {

    buttonTypeLeft = typeLeft;
    buttonTypeRight = typeRight;
  }

  /**
   * 刷屏,效果同flushGraphics(), 但增加画Button
   */
  public void flushGraphicsWithButton() {
    /*Graphics paintG = this.getPaintG();
    if (buttonImg != null) {
      //left button
      if (buttonTypeLeft != BUTTON_TYPE_NONE) {
        paintG.setClip(1, ScreenHeight - buttonImg.getHeight(),
                        buttonImg.getWidth() / BUTTON_IMAGE_FRAME,
                        buttonImg.getHeight());
        paintG.drawImage(buttonImg,
                          1 -
                          buttonTypeLeft * buttonImg.getWidth() /
                          BUTTON_IMAGE_FRAME,
                          ScreenHeight - buttonImg.getHeight(),
                          Graphics.TOP | Graphics.LEFT);
      }
      //right button
      if (buttonTypeRight != BUTTON_TYPE_NONE) {
        paintG.setClip(ScreenWidth - buttonImg.getWidth() / BUTTON_IMAGE_FRAME -
                        1, ScreenHeight - buttonImg.getHeight(),
                        buttonImg.getWidth() / BUTTON_IMAGE_FRAME,
                        buttonImg.getHeight());
        paintG.drawImage(buttonImg,
                          ScreenWidth -
                          buttonImg.getWidth() / BUTTON_IMAGE_FRAME -
                          1 -
                          buttonTypeRight * buttonImg.getWidth() /
                          BUTTON_IMAGE_FRAME,
                          ScreenHeight - buttonImg.getHeight(),
                          Graphics.TOP | Graphics.LEFT);
      }
      paintG.setClip(0, 0, ScreenWidth, ScreenHeight);
    }*/
    flushGraphics();
  }

  /**
   * 重载了flshGraphics的方法,在画图前把screenImg的内容画上去
   */
  public void flushGraphics(){
    //System.out.println("Flush Graphics!");
    if (bShowMsg && msg != null){//需要绘制消息
      ownG.setClip(0, 0, ScreenWidth, ScreenHeight);
      if (bShowMsgOnTop){//画在顶部
        paintMsg(ownG, 0, 0, this.ScreenWidth);
        ownG.drawImage(screenImg, 0, msgHeight, Graphics.LEFT | Graphics.TOP);
      }else{//画在底部
        ownG.drawImage(screenImg, 0, 0, Graphics.LEFT | Graphics.TOP);
        paintMsg(ownG, 0, ScreenHeight - msgHeight, this.ScreenWidth);
      }
    }
    super.flushGraphics();
   }

  private void paintMsg(Graphics g, int left, int top, int width){
    //绘制消息框
      g.setColor(255,255,128);
      g.fillRect(left, top, width, msgHeight - 1);
      g.setColor(0, 0, 0);
      g.drawRect(left -1, top, width +1, msgHeight - 1);
      //绘制消息提示符
      g.drawImage(img_msg,
                  left+2,
                  top + (msgHeight - img_msg.getHeight())/2,
                  Graphics.LEFT | Graphics.TOP);

      //写消息
      g.drawString(msg,
                   left + img_msg.getWidth() + 4,
                   top + 1,
                   Graphics.LEFT | Graphics.TOP);
  }

  /**
   * 设置是否显示消息
   * @param bShow boolean
   */
  public void showMsg(boolean bShow){
    bShowMsg = bShow;
  }

  /**
   * 设置消息的内容
   * @param msg String
   */
  public void setMsg(String message){
    this.msg = message;
  }

  /**
   * 显示指定消息
   * @param message String
   */
  public void showMsg(String message){
    showMsg(true);
    this.msg = message;
  }

  /**
   * 设置消息显示的位置
   * @param <any> boolean
   */
  public void setMsgOnTop(boolean bTop){
    bShowMsgOnTop = bTop;
  }

  /**
   *
   * @param mediaName String
   * @return Player
   */
  public Player createPlayer(String mediaName) {
    if (mediaName == null || mediaName == "")
      return null;
    Player player = null;
    try {
      String audioType;
      if (mediaName.endsWith(".wav")) {
        audioType = "audio/x-wav";
      }
      else if (mediaName.endsWith(".mid")) {
        audioType = "audio/midi";
      }
      else if (mediaName.endsWith(".mmf")) {
        audioType = "audio/smf";
      }
      else if (mediaName.endsWith(".mp3")) {
        audioType = "audio/mpeg";
      }
      else {
        audioType = "audio/x-tone-seq";
      }
      InputStream is = getClass().getResourceAsStream(mediaName);
      player = Manager.createPlayer(is, audioType);
    }
    catch (Exception e) {
      System.out.println("Create Player Fail:" + e.toString());
    }
    return player;
  }


  /**
   *
   * @param c Command
   * @param d Displayable
   */
 /* public void commandAction(Command c, Displayable d){
    //?这块地方会进入吗?!
    //测试一下
    if (c == leftCmd){
      System.out.println("Left Command Pressed!");
      OnLeftCommand(c.getLabel());
    }else if (c == rightCmd){
      System.out.println("Right Command Pressed!");
      OnRightCommand(c.getLabel());
      //this.setCommand("yes", "no");
    }

  }
*/


  /**
   * keyPressed
   * 先对按键信息进行处理,然后再交给用户处理
   * @param keyCode int
   * @todo Implement this javax.microedition.lcdui.Canvas method
   */
  protected void keyPressed(int keyCode) {
    //  super.keyPressed(keyCode);
    if (keyCode < 0) {
      keyStatus |= 1 << (keyCode + 18);
    }
    else if (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9) {
      keyStatus |= 1 << (keyCode - 30);
    }
    else if (keyCode == KEY_STAR) {
      keyStatus |= KEY_STAR_PRESSED;
    }
    else if (keyCode == KEY_POUND) {
      keyStatus |= KEY_POUND_PRESSED;

    }
    switch (appStatus) {
      case STATUS_SPLASH:
        if (splash != null) {
          splash.keyPressed(keyCode);
        }
        break;
      case STATUS_TEXTBOARD:
        if (textBoard != null) {
          textBoard.keyPressed(keyCode);
        }
        break;
      default:
        super.keyPressed(keyCode);
        keyPressedEvent(keyCode);
        break;
    }
  }

  /**
   * keyReleased
   *
   * @param keyCode int
   * @todo Implement this javax.microedition.lcdui.Canvas method
   */
  protected void keyReleased(int keyCode) {
    //  super.keyReleased(keyCode);
    if (keyCode < 0) {
      keyStatus &= ~ (1 << (keyCode + 18));
    }
    else if (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9) {
      keyStatus &= ~ (1 << (keyCode - 30));
    }
    else if (keyCode == KEY_STAR) {
      keyStatus &= ~KEY_STAR_PRESSED;
    }
    else if (keyCode == KEY_POUND) {
      keyStatus &= ~KEY_POUND_PRESSED;

    }
    keyReleasedEvent(keyCode);

  }

  /**
   * 第一次播放动画时必须调用
   * @param splashName String
   * @param splashMusicName String
   */
  public  void playFlash(String splashName, String splashMusicName, String waitString){
    if (flash == null){//如果动画对象还没有创建出来
      flash = new Splash(this, splashName, splashMusicName, waitString, appVerStr);
      flash.setListener(this);
      flash.startSplash(); //闪屏开始

      //设置为动画状态
      this.setStatus(STATUS_FLASH);
    }else{
      flash.setWaitString(waitString);
      playFlash();

    }
  }

  /**
   * 暂停闪屏
   */
  public void stopFlash(){
    System.out.println("stop flash");
    if (flash != null){
      flash.pause();
    }
  }

  /**
   * 播放动画
   */
  public void playFlash(){
     System.out.println("playFlash()");
    this.setStatus(STATUS_FLASH);
    if (flash!=null){
      flash.resume();
      System.out.println("Flash resume!!!!");
    }
  }

  /**
   * 读取音乐文件
   * 这个实在类的初始化的时候调用

  protected  void loadSound(){
    try{
      menuPlayer = this.createPlayer("/img/menu.wav");
      msgPlayer = this.createPlayer("/img/msg.wav");
//      InputStream is = getClass().getResourceAsStream("/res/pyonpa.wav");
//      String ctype = "audio/x-wav";
//      menuPlayer = Manager.createPlayer(is,ctype);
//      is.reset();
//      is = getClass().getResourceAsStream("/res/erase.wav");
//      player_erase = Manager.createPlayer(is,ctype);
//
//      is.reset();
//      is = getClass().getResourceAsStream("/res/shot.wav");
//      player_shot = Manager.createPlayer(is,ctype);
//      is.close();
    }
    catch (Exception e){
      //System.out.println(e.toString());
    }
  }*/

//  /**
//   * 播放声音
//   * @param soundType byte
//   */
//  public void playSound(byte soundType){
//    if (!bSoundOn)
//      return;
//    Player player = null;
//    switch(soundType){
//      case SOUND_MENU:
//        player = menuPlayer;
//        break;
//      case SOUND_MSG_IN:
//        player = msgPlayer;
//        break;
//    }
//
//    if (player != null) {
//      try{
//        if (player.getState() == 0)
//          player.stop();
//        player.start();
//        System.out.println("Play Sound!");
//        }
//        catch(Exception e){System.out.println(e.toString());}
//    }
//
//  }

  /**
   * 设置声音的开关
   * @param on boolean
   */
  public void setSoundOn(boolean on){
    this.bSoundOn = on;
  }

  /**
   * 取得文本板上的文字
   * @return String
   */
  public String getTextBoardText(){
    return textBoard.getText();
  }


  /**
   * 开发者必须重载, 初始华程序
   */
  abstract public void initProgramData();

  /**
   *开发者在其它基于XCanvas的子类中重载此方法,它将在开发者自定义的状态中随着线程循环而被XCanvas反复调用
   */
  abstract public void update();

  /**
   * 开发者自定义状态中,处理按键事件
   * @param code int
   */
  abstract public void keyPressedEvent(int code);

  /**
   * 开发者者自定义状态中,处理松键事件
   * @param code int
   */
  abstract public void keyReleasedEvent(int code);

//  /**
//   * 左边的按钮被按下
//   * @param cmdStr String
//   */
//  protected abstract void OnLeftCommand(String cmdStr);
//
//  /**
//   * 右边的按钮被按下
//   * @param cmdStr String
//   */
//  protected abstract void OnRightCommand(String cmdStr);

  protected void hideNotify(){
//    System.out.println("hideNotify");
//    if (this.getAppStatus() == this.STATUS_FLASH){
//      this.stopFlash();
//    }
  }

  protected void showNotify(){
//    System.out.println("showNotify");
//    if (this.getAppStatus() == this.STATUS_FLASH){
//      this.playFlash();
//    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -