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

📄 drawtools.java

📁 这是我在原来单位的时候写的一个坦克大战的J2ME游戏的代码,可以给其他作J2ME游戏的人作个参考
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
          m_switchImgSprite[0].setRefPixelPosition(
              menuButtonPosX + MENU_WORD_WIDTH,
              menuButtonPosY +2+ i * (MENU_BUTTON_HEIGHT + MENU_BUTTON_GAP));
          //open/close
          m_switchImgSprite[1].setFrame(m_switchIndex[1]);
          m_switchImgSprite[1].setRefPixelPosition(
              menuButtonPosX + MENU_WORD_WIDTH+SWITCH_WORD_WIDTH,
              menuButtonPosY +2+ i * (MENU_BUTTON_HEIGHT + MENU_BUTTON_GAP));
        }
      }

      if (MENU_BUTTON_WORD_INDEX_MUSIC_CHOSEN ==
          chosenWordIndex[m_currentMenuButtonIndex]) {
        getMusicState(switchState,true);
        m_switchImgSprite[0].setFrame(m_switchIndex[0]);
        m_switchImgSprite[1].setFrame(m_switchIndex[1]);
      }
      m_menuButtonImgSprite[m_currentMenuButtonIndex].setFrame(
          MENU_BUTTON_INDEX_CHOSEN);
      m_menuWordImgSprite[m_currentMenuButtonIndex].setFrame(chosenWordIndex[
          m_currentMenuButtonIndex]);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

   private static void getMusicState(int state, boolean focused) {


    switch (state) {
      case DrawTools.SWITCH_OPEN:
        if (focused == true) {
          m_switchIndex[0] = DrawTools.SWITCH_LINE_FOCUSED;
          m_switchIndex[1] = DrawTools.SWITCH_OPEN_FOCUSED;
        }else{
          m_switchIndex[0] = DrawTools.SWITCH_LINE_UNFOCUSED;
          m_switchIndex[1] = DrawTools.SWITCH_OPEN_UNFOCUSED;
        }

        break;
      case DrawTools.SWITCH_CLOSE:
        if (focused == true) {
          m_switchIndex[0] = DrawTools.SWITCH_LINE_FOCUSED;
          m_switchIndex[1] = DrawTools.SWITCH_CLOSE_FOCUSED;
        }else{
          m_switchIndex[0] = DrawTools.SWITCH_LINE_UNFOCUSED;
          m_switchIndex[1] = DrawTools.SWITCH_CLOSE_UNFOCUSED;
        }

        break;
      default:
        break;
    }
  }

  // 参数:游戏画面的起点(destX, destY),元素图上的起点(sourceX,
  // sourceY),要画的图片区域宽,高(sourceWidth, sourceHeight)
  public static void XDrawImage(Graphics g, Image pImage, int destX, int destY,
                         int sourceX, int sourceY, int sourceWidth,
                         int sourceHeight) {
   if(null==pImage){
     return;
   }

    g.setClip(destX , destY , sourceWidth, sourceHeight);
    g.drawImage(pImage, destX - sourceX , destY - sourceY ,
                (Graphics.TOP | Graphics.LEFT));
    g.setClip(0, 0, GameLogic.MAP_WIDTH, GameLogic.MAP_HEIGHT);
  }
  
}



//单一纪录的类名
class Appointment{
  public int m_curLevel;//当前的难度
  public int m_curTankType;//我方坦克类型
  public int m_curStage;//当前关卡
  public int m_curGamePoints;//获得计分
  public int m_tankLives;

  public Appointment(){
  	m_curLevel =m_curTankType=m_curStage=m_curGamePoints=m_tankLives=0;
  }

  public Appointment(byte[] rec) {
    initAppointment(rec);
  }

  //当前的难度/我方坦克类型/当前关卡/累计计分
  public Appointment(int level,int tankType,int stage,int points,int tankLives){
    m_curLevel = level;
    m_curTankType = tankType;
    m_curStage = stage;
    m_curGamePoints = points;
    m_tankLives=tankLives;
  }

  public int getStage() {
    return m_curStage;
  }

  public int getPoints() {
    return m_curGamePoints;
  }

  public int getLevel(){
    return m_curLevel;
  }


  private void initAppointment(byte[] rec) { //从字节读取内容
    ByteArrayInputStream bais = new ByteArrayInputStream(rec);
    DataInputStream dis = new DataInputStream(bais);

    try {
      m_curLevel = dis.readInt();
      m_curTankType=dis.readInt();
      m_curStage=dis.readInt();
      m_curGamePoints=dis.readInt();
      m_tankLives=dis.readInt();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public byte[] toBytes() { //写成字节
    byte[] data = null;

    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(baos);
      dos.writeInt(m_curLevel);
      dos.writeInt(m_curTankType);
      dos.writeInt(m_curStage);
      dos.writeInt(m_curGamePoints);
      dos.writeInt(m_tankLives);
      data = baos.toByteArray();
      baos.close();
      dos.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return data;
  }
}

//RMS操作
class RMS {
  public static boolean addRecord(String name, Appointment app) {
    boolean result = false;

    try {
      RecordStore rs = RecordStore.openRecordStore(name, true);
      byte[] data = app.toBytes();
      rs.addRecord(data, 0, data.length);
      rs.closeRecordStore();
      result = true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

  public static int getNumOfRecords(String name) {//得到RMS中记录的条数
    try {
      RecordStore rs = RecordStore.openRecordStore(name, true);
      return rs.getNumRecords();
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return 0;
    }
  }

  public static Appointment[] getRecords(String name) { //取得RMS中的所有记录
    Appointment[] result = {};
    try {
      RecordStore rs = RecordStore.openRecordStore(name, false);
      RecordEnumeration re = rs.enumerateRecords(null, null, false);
      result = new Appointment[rs.getNumRecords()];
      for (int i = 0; i < result.length; i++) {
        int j = re.previousRecordId();
        Appointment app = new Appointment(rs.getRecord(j));
        result[i] = app;
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

  public static Appointment getRecord(String name, int j) { //根据记录编号(参数 int j)取得一条记录
    Appointment result = new Appointment();
    try {
      RecordStore rs = RecordStore.openRecordStore(name, false);
      RecordEnumeration re = rs.enumerateRecords(null, null, false);
      result = new Appointment(rs.getRecord(j));
      rs.closeRecordStore();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

  public static boolean setRecord(String name,int id,Appointment appoint) {
    boolean result = false;
    RecordStore rs = null;
    RecordEnumeration re = null;

    try {
      rs = RecordStore.openRecordStore(name, false); //open
      re = rs.enumerateRecords(null, null, false); //enumeration
      byte[] data = appoint.toBytes();
      rs.setRecord(id, data, 0, data.length);
      result = true;
      rs.closeRecordStore();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }
}

class LPAudioPlayer implements Runnable {
	private Player player;
	private String filename;
	private String format;

	public LPAudioPlayer(String filename, String format, boolean isLoad) {
		this.format = format;
		this.filename = filename;
		if (isLoad) {
			loadResource();
		}
	}

	public LPAudioPlayer(String filename, String format) {
		this.format = format;
		this.filename = filename;
	}

	public void loadResource() {
		try {
			System.gc();
			InputStream is = getClass().getResourceAsStream(filename);
			player = Manager.createPlayer(is, format);
		} catch (IOException ex) {
			System.out.println("can't load " + filename);
			System.out.println(ex.toString());
		} catch (MediaException ex) {
			System.out.println("can't create audio");
			System.out.println(ex.toString());
		}
	}

	public void setLoop() {
		if (player != null) {
			player.setLoopCount(-1);
		}
	}

	public void setVolume(int level) {
		if (player != null) {
			VolumeControl control = (VolumeControl) player
					.getControl("VolumeControl");
			control.setLevel(level);
		}
	}

	public void stop() {
		if (player != null) {
			try {
				player.stop();
			} catch (MediaException ex) {
				System.out.println("can't stop audio");
				System.out.println(ex.toString());
			}
		}
	}

	public void play() {
		if (player != null) {
			try {
				player.realize();
				player.prefetch();
				player.start();

			} catch (MediaException ex) {
				System.out.println("can't play audio");
				System.out.println(ex.toString());
			}
		}
	}

	public void replay() {
		close();
		System.gc();
		loadResource();
		play();
	}

	public void run() {
		replay();
	}

	public void threadPlay() {
		new Thread(this).start();
	}

	public void close() {
		if (player != null) {
			player.close();
			player = null;

⌨️ 快捷键说明

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