Resource.java
资源名称:PaoPao.rar [点击查看]
上传用户:sh2222
上传日期:2009-12-31
资源大小:84k
文件大小:7k
源码类别:
J2ME
开发平台:
Java
- package paopao;
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.*;
- import java.util.Hashtable;
- import java.util.Random;
- public class Resource {
- static Hashtable angleValue;
- private static int imageNum = 0;
- private static Image img[];
- static String helpStr = "";
- static RecordStore rs = null;
- static final byte KEY_UP = -1;
- static final byte KEY_DOWN = -2;
- static final byte KEY_LEFT = -3;
- static final byte KEY_RIGHT = -4;
- static final byte KEY_FIRE = -5;
- static final byte KEY_LS = -6; //左软件
- static final byte KEY_RS = -7; //右软件
- //static final byte KEY_LS= 42; //左软件
- // static final byte KEY_RS= 35; //右软件
- static byte[] key = new byte[] {
- KEY_LEFT, KEY_RIGHT, KEY_LS, KEY_RS, 0, 0, 1
- };
- static String[] keyDes = new String[] {
- "向左移动:", "向右移动:", "暂停游戏:", "重新开始:"
- };
- static final int setupKeyNum = 4;
- static boolean isSoundOn = true;
- static final String DIR = "/resource/";
- //images
- static final int BG = 16;
- static final int BALL = 17;
- static final int BALL_BOMB = 18;
- static final int POINT = 19;
- static final int AID_LINE = 20;
- static final int PROPS = 21;
- static final int BBALL = 22;
- public static Random random = new Random();
- static int tempAngle;
- static int tempValue;
- public static void init(int m_imageNum) {
- imageNum = m_imageNum;
- img = new Image[imageNum];
- }
- //从.PNG文件生成IMAGE
- //参数 int m_imageId 图片id,例如 Resource.IMG_LOGO
- //参数 String m_imageName 图片资源路径名称,例如 "/logo.png"
- public static void setImage(int m_imageId, String m_imageName) {
- img[m_imageId] = loadImage(m_imageName);
- }
- //从IMAGE生成IMAGE
- public static void setImage(int m_imageId, Image m_image) {
- img[m_imageId] = m_image;
- }
- public static Image getImage(int m_imageId) {
- return img[m_imageId];
- }
- //释放所有图片资源
- public static void releasAllImage() {
- for (int i = 0; i < imageNum; i++) {
- img[i] = null;
- }
- System.gc();
- }
- //释放指定id的图片资源
- public static void releasImage(int ImageId) {
- img[ImageId] = null;
- System.gc();
- }
- private static Image loadImage(String m_imageName) {
- Image img = null;
- try {
- img = Image.createImage(m_imageName);
- return img;
- }
- catch (Exception e) {
- System.out.println("Error: " + e.getMessage() + " - " + m_imageName);
- return null;
- }
- }
- static void loadAngleValue()
- {
- angleValue = new Hashtable();
- angleValue.put(String.valueOf(0),new Integer(0));
- angleValue.put(String.valueOf(3),new Integer(5236));
- angleValue.put(String.valueOf(6),new Integer(10453));
- angleValue.put(String.valueOf(9),new Integer(15643));
- angleValue.put(String.valueOf(12),new Integer(20791));
- angleValue.put(String.valueOf(15),new Integer(25882));
- angleValue.put(String.valueOf(18),new Integer(30902));
- angleValue.put(String.valueOf(21),new Integer(35837));
- angleValue.put(String.valueOf(24),new Integer(40674));
- angleValue.put(String.valueOf(27),new Integer(45400));
- angleValue.put(String.valueOf(30),new Integer(50000));
- angleValue.put(String.valueOf(33),new Integer(54464));
- angleValue.put(String.valueOf(36),new Integer(58779));
- angleValue.put(String.valueOf(39),new Integer(62932));
- angleValue.put(String.valueOf(42),new Integer(66913));
- angleValue.put(String.valueOf(45),new Integer(70711));
- angleValue.put(String.valueOf(48),new Integer(74314));
- angleValue.put(String.valueOf(51),new Integer(77715));
- angleValue.put(String.valueOf(54),new Integer(80902));
- angleValue.put(String.valueOf(57),new Integer(83867));
- angleValue.put(String.valueOf(60),new Integer(86603));
- angleValue.put(String.valueOf(63),new Integer(89101));
- angleValue.put(String.valueOf(66),new Integer(91355));
- angleValue.put(String.valueOf(69),new Integer(93358));
- angleValue.put(String.valueOf(72),new Integer(95106));
- angleValue.put(String.valueOf(75),new Integer(96593));
- angleValue.put(String.valueOf(78),new Integer(97815));
- angleValue.put(String.valueOf(81),new Integer(98769));
- angleValue.put(String.valueOf(84),new Integer(99452));
- angleValue.put(String.valueOf(87),new Integer(99863));
- angleValue.put(String.valueOf(90),new Integer(100000));
- }
- static int getSinAngleValue(int angle)
- {
- tempAngle = (angle > 90 ? (180 - angle) : angle);
- tempValue = ((Integer)(angleValue.get(String.valueOf(tempAngle)))).intValue();
- return tempValue;
- }
- static int getCosAngleValue(int angle)
- {
- tempAngle = (angle > 90 ? angle - 90 : 90 - angle);
- tempValue = ((Integer)(angleValue.get(String.valueOf(tempAngle)))).intValue();
- return (angle <= 90 ? tempValue : (-tempValue));
- }
- public static String GetUserKeyName(int keycode) {
- switch (keycode) {
- case '0': // '0'
- return "0";
- case '1': // '1'
- return "1";
- case '2': // '2'
- return "2";
- case '3': // '3'
- return "3";
- case '4': // '4'
- return "4";
- case '5': // '5'
- return "5";
- case '6': // '6'
- return "6";
- case '7': // '7'
- return "7";
- case '8': // '8'
- return "8";
- case '9': // '9'
- return "9";
- case '#': // '#'
- return "#";
- case '*': // '*'
- return "*";
- case KEY_UP:
- return "上";
- case KEY_DOWN:
- return "下";
- case KEY_LEFT:
- return "左";
- case KEY_RIGHT:
- return "右";
- case KEY_FIRE:
- return "中";
- default:
- return " ";
- }
- }
- public static void initrecord() {
- boolean existingOrNot = true;
- try {
- rs = RecordStore.openRecordStore("option", false);
- }
- catch (RecordStoreNotFoundException e) {
- existingOrNot = false;
- e.printStackTrace();
- }catch (RecordStoreException e){
- existingOrNot = false;
- e.printStackTrace();
- }
- // }
- if (existingOrNot) {
- try {
- rs.closeRecordStore();
- }
- catch (Exception x) {
- }
- loadOption();
- }
- else {
- try {
- rs = RecordStore.openRecordStore("option", true);
- }
- catch (Exception e) {
- }
- try {
- if (isSoundOn)
- key[6] = 1;
- else
- key[6] = 0;
- rs.addRecord(key, 0, key.length);
- }
- catch (Exception e) {
- }
- }
- try {
- rs.closeRecordStore();
- }
- catch (Exception x) {
- }
- }
- public static void saveOption() {
- try {
- // rs.closeRecordStore();
- rs.deleteRecordStore("option");
- // rs=null;
- rs = RecordStore.openRecordStore("option", true);
- }
- catch (Exception e) {
- }
- try {
- if (isSoundOn)
- key[6] = 1;
- else
- key[6] = 0;
- rs.addRecord(key, 0, key.length);
- }
- catch (Exception e) {
- }
- try {
- rs.closeRecordStore();
- }
- catch (Exception x) {
- }
- }
- public static void loadOption() {
- boolean existingOrNot = true;
- try {
- rs = RecordStore.openRecordStore("option", false);
- }
- catch (Exception e) {
- existingOrNot = false;
- }
- if (existingOrNot) {
- try {
- key = rs.getRecord(1);
- if (key[6] == 1)
- isSoundOn = true;
- else
- isSoundOn = false;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- else { //not exist
- System.out.println("not exist");
- }
- try {
- rs.closeRecordStore();
- }
- catch (Exception x) {
- x.printStackTrace();
- }
- }
- }