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

📄 resources.java

📁 --- --- --- 基于J2ME的游戏程序--------很有技巧性的程序
💻 JAVA
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)

package bluegammon;

import java.io.InputStream;
import java.util.Hashtable;

import javax.microedition.lcdui.Image;

/**
 * Primitive resource mapper.
 * 
 * @author Peter Andersson
 */
public class Resources
{
  // Text keys
  private static int TXTID = 0;
  // text / page titles
  public static final int TXT_T_BLUEGAMMON     = TXTID++;
  public static final int TXT_T_BLUETOOTH      = TXTID++;
  public static final int TXT_T_PHONEGAME      = TXTID++;
  public static final int TXT_T_SETTINGS       = TXTID++;
  public static final int TXT_T_RULES          = TXTID++;
  public static final int TXT_T_DEVICES        = TXTID++;
  public static final int TXT_T_SCORES         = TXTID++;
  // text / item labels
  public static final int TXT_I_BTGAME         = TXTID++;
  public static final int TXT_I_PHONE_GAME     = TXTID++;
  public static final int TXT_I_SETTINGS       = TXTID++;
  public static final int TXT_I_ABOUT          = TXTID++;
  public static final int TXT_I_EXIT           = TXTID++;
  public static final int TXT_I_BT_SERVER      = TXTID++;
  public static final int TXT_I_BT_CLIENT      = TXTID++;
  public static final int TXT_I_BT_SCORES      = TXTID++;
  public static final int TXT_I_PG_RESUME      = TXTID++;
  public static final int TXT_I_PG_NEW_GAME    = TXTID++;
  public static final int TXT_I_PG_RULES       = TXTID++;
  public static final int TXT_I_R_MAXFIVE      = TXTID++;
  public static final int TXT_I_R_EVENOUT      = TXTID++;
  public static final int TXT_I_S_COLOR        = TXTID++;
  public static final int TXT_I_S_AUDIO        = TXTID++;
  public static final int TXT_I_S_VIBRA        = TXTID++;
  // text / commands
  public static final int TXT_C_OK             = TXTID++;
  public static final int TXT_C_CANCEL         = TXTID++;
  public static final int TXT_C_BACK           = TXTID++;
  public static final int TXT_C_HELP           = TXTID++;
  public static final int TXT_C_UNDO           = TXTID++;
  public static final int TXT_C_QUIT           = TXTID++;
  public static final int TXT_C_REFRESH        = TXTID++;
  // text / bluetooth related
  public static final int TXT_BT_SERVER_OPENED = TXTID++;
  public static final int TXT_BT_DISC_FAIL     = TXTID++;
  public static final int TXT_BT_CONNECT       = TXTID++;
  public static final int TXT_BT_CONN_FAIL     = TXTID++;
  public static final int TXT_BT_NONSERVER     = TXTID++;
  public static final int TXT_BT_SERVER_FAILURE= TXTID++;
  public static final int TXT_BT_UNKNOWN       = TXTID++;
  public static final int TXT_BT_REMOTE_QUIT   = TXTID++;
  public static final int TXT_BT_CONN_LOST     = TXTID++;
  public static final int TXT_BT_NO_DEVICES    = TXTID++;
  // text / animation related
  public static final int TXT_A_QUERY_COMMIT   = TXTID++;
  public static final int TXT_A_BLACK_STARTS   = TXTID++;
  public static final int TXT_A_WHITE_STARTS   = TXTID++;
  public static final int TXT_A_BLACK_WINS     = TXTID++;
  public static final int TXT_A_WHITE_WINS     = TXTID++;
  public static final int TXT_A_POINT          = TXTID++;
  public static final int TXT_A_POINTS         = TXTID++;
  public static final int TXT_A_PIECE          = TXTID++;
  public static final int TXT_A_PIECES         = TXTID++;
  // text / popup related
  public static final int TXT_YES              = TXTID++;
  public static final int TXT_NO               = TXTID++;
  public static final int TXT_CANCEL           = TXTID++;
  public static final int TXT_OK               = TXTID++;
  public static final int TXT_QUIT_REMOTE      = TXTID++;
  public static final int TXT_GIVE_UP          = TXTID++;
  public static final int TXT_SAVE_GAME        = TXTID++;
  public static final int TXT_REMOTE_GAVE_UP   = TXTID++;
  // text / helptexts
  public static final int TXT_H_BT_GAME        = TXTID++;
  public static final int TXT_H_PHONE_GAME     = TXTID++;
  public static final int TXT_H_SCORES         = TXTID++;
  public static final int TXT_H_SETTINGS       = TXTID++;
  public static final int TXT_H_BT_SERVER      = TXTID++;
  public static final int TXT_H_BT_CLIENT      = TXTID++;
  public static final int TXT_H_PG_RESUME      = TXTID++;
  public static final int TXT_H_PG_NEW_GAME    = TXTID++;
  public static final int TXT_H_R_MAX_FIVE     = TXTID++;
  public static final int TXT_H_R_EVEN_OUT     = TXTID++;
  public static final int TXT_H_S_COLOR        = TXTID++;
  public static final int TXT_H_S_AUDIO        = TXTID++;
  public static final int TXT_H_S_VIBRA        = TXTID++;
  public static final int TXT_H_S_RULES        = TXTID++;
  // text / other stuff
  public static final int TXT_STARTING_GAME    = TXTID++;
  public static final int TXT_MESSAGE          = TXTID++;
  public static final int TXT_HANDSHAKE_FAIL   = TXTID++;
  public static final int TXT_SCORE_PLAYER     = TXTID++;
  public static final int TXT_SCORE_OPPONENT   = TXTID++;
  public static final int TXT_SCORE_GAMES      = TXTID++;
  public static final int TXT_SCORE_DATE       = TXTID++;
  public static final int TXT_NO_RULES         = TXTID++;
  public static final int TXT_ABOUT            = TXTID++;

  // Image keys
  private static final int OFFSET_IMG          = 100;
  public static final int IMG_BACKGROUND       = 100;
  public static final int IMG_WHITE_TEXTURES   = 101;
  public static final int IMG_BLACK_TEXTURES   = 102;
  public static final int IMG_WHITE_PIECE      = 103;
  public static final int IMG_BLACK_PIECE      = 104;
  public static final int IMG_AUDIO_ON         = 105;
  public static final int IMG_AUDIO_OFF        = 106;
  public static final int IMG_VIBRA_ON         = 107;
  public static final int IMG_VIBRA_OFF        = 108;
  public static final int IMG_ENABLED          = 109;
  public static final int IMG_DISABLED         = 110;
  public static final int IMG_BT_1             = 111;
  public static final int IMG_BT_2             = 112;
  
  // Resource (sound) keys
  private static final int OFFSET_SND          = 200;
  public static final int SND_DICES_SHORT      = 200;
  public static final int SND_DICES_LONG       = 201;
  public static final int SND_PIECE            = 202;
  public static final int SND_JAZZY            = 203;
  public static final int SND_MESSAGE          = 204;
  public static final int SND_CONNFAIL         = 205;
  public static final int SND_WINNER           = 206;
  public static final int SND_LOSER            = 207;
  
  // Text data
  protected static final char[][] TEXTBUF = {
    "BLUEGAMMON".toCharArray(),
    "BLUETOOTH".toCharArray(),
    "PHONEGAME".toCharArray(),
    "SETTINGS".toCharArray(),
    "RULES".toCharArray(),
    "DEVICES".toCharArray(),
    "SCORES".toCharArray(),

    "Play via bluetooth".toCharArray(),
    "Play on phone".toCharArray(),
    "Settings".toCharArray(),
    "About".toCharArray(),
    "Quit".toCharArray(),
    "Setup server".toCharArray(),
    "Connect as client".toCharArray(),
    "Scores".toCharArray(),
    "Resume game".toCharArray(),
    "New game".toCharArray(),
    "Rules".toCharArray(),
    "Max five".toCharArray(),
    "Even out".toCharArray(),
    "Preferred color".toCharArray(),
    "Audio".toCharArray(),
    "Vibration".toCharArray(),
    
    "Ok".toCharArray(),
    "Cancel".toCharArray(),
    "Back".toCharArray(),
    "Help".toCharArray(),
    "Undo".toCharArray(),
    "Quit".toCharArray(),
    "Refresh".toCharArray(),
    
    "Awaiting client...".toCharArray(),
    "Bluetooth discovery failed.".toCharArray(),
    "Connecting to".toCharArray(),
    "Could not connect to device.".toCharArray(),
    "This device is not running a backgammon server.".toCharArray(),
    "Failure when opening server.".toCharArray(),
    "Unknown".toCharArray(),
    ("The other player quit the game.\nOngoing game is saved "+
    "and will be resumed next game with this player.").toCharArray(),
    ("Connection to other player was lost.\nOngoing game is saved "+
    "and will be resumed next game with this player.").toCharArray(),
    "No nearby devices found.".toCharArray(),
    
    "DONE?".toCharArray(),
    "BLACK STARTS".toCharArray(),
    "WHITE STARTS".toCharArray(),
    "BLACK WINS!".toCharArray(),    
    "WHITE WINS!".toCharArray(),
    "POINT".toCharArray(),
    "POINTS".toCharArray(),
    "PIECE".toCharArray(),
    "PIECES".toCharArray(),

    "YES".toCharArray(),
    "NO".toCharArray(),
    "CANCEL".toCharArray(),
    "OK".toCharArray(),
    ("The game is still ongoing. You can give up this game, or you can save it.\n" +
        "The saved game will be resumed on next game with this player.").toCharArray(),
    "GIVE UP".toCharArray(),
    "SAVE".toCharArray(),
    "Your opponent gave up - you won the game!".toCharArray(),
    
    ("Play against another player by connecting via bluetooth.").toCharArray(),
    ("Play against another player by handing this phone " +
    		"between eachother.").toCharArray(),
    ("Show the scores between the opponents you have played " +
    		"against via bluetooth.").toCharArray(),
    ("Set up preferred rules and player color. Enable or disable vibration and audio.").toCharArray(),
    ("Open a bluetooth server on this device enabling a client to connect.").toCharArray(),
    ("Connect to a bluetooth server.").toCharArray(),
    ("Resume the game where you left off.").toCharArray(),
    ("Start a new game. This will erase any saved game.").toCharArray(),
    ("With this rule enabled there can be maximum five pieces of the same color on a row.").toCharArray(),
    ("With this rule enabled it is only possible to move out from backgammon board if the " +
    		"dice shows the same value as the row of the piece.\n\n" +
    		"For example, suppose you only have two pieces left home each standing on row " +
    		"one and row three. These can only be moved out if you throw a one or a three " +
    		"with the dices. If two is thrown the piece on row three must be moved to row " +
    		"one. A dice showing four, five or six is a lost dice.").toCharArray(),
    ("Set the player color you prefer. It is up to the backgammon server to decide what color " +
        "you actually get in a new game.").toCharArray(),
    ("Enable or disable audio.").toCharArray(),
    ("Enable or disable vibration.").toCharArray(),
    ("Set the rules you prefer in game. It is up to the backgammon server to decide the " +
        "actual rules in a new game.").toCharArray(),

    "Starting the game...".toCharArray(),
    "Message".toCharArray(),
    "Error occured when connecting to remote player.".toCharArray(),
    "Your score".toCharArray(),
    "Opponent score".toCharArray(),
    "Games".toCharArray(),
    "Last game".toCharArray(),
    "No special rules set.".toCharArray(),
    ("Copyright 2005 Sony Ericsson Mobile Communications AB. All Rights Reserved.\n\n" +
        "This is an example Bluetooth MIDlet game that was designed for the Sony Ericsson K750i phone.\n" +
        "Support for JSR-82 and JSR-184 (Mobile 3D) is required.\n\n" +
        "During gameplay, use navigation keys and fire. In distributed gaming, you can send messages " +
        "to the opponent by pressing hash-key.\n\n" +
        "By Peter Andersson\n\n" +
        "Thanks to Anders Tingsek for contributing to the music\n\n" +
        "May, 2005\n").toCharArray()
  };
  
  /** Image cache */
  protected static Hashtable m_images = new Hashtable();
  
  // Image resource names
  protected static final String[] IMGNAME_MAP ={
    "background.png",
    "textures_wh.png",
    "textures_bl.png",
    "white.png",
    "black.png",
    "audio_on.png",
    "audio_off.png",
    "vibra_on.png",
    "vibra_off.png",
    "enabled.png",
    "blank.png",
    "bt1.png",
    "bt2.png",
  };
  
  // Resource (audio) names
  protected static final String[] RESOURCE_MAP = {
    "dices_short.amr",  
    "dices_long.amr",  
    "piece.amr",  
    "bluegammon.mid",  
    "message.mid",  
    "connfail.mid",  
    "winner.mid",  
    "loser.mid",  
  };
  
  /**
   * Returns specified resource as stream
   * @param resourceId  The id of the resource.
   * @return            An inputstream to the resource.
   */
  public static InputStream getResource(int resourceId)
  {
    resourceId -= OFFSET_SND;
	
	/*
	   * 注意原来这里写的是"/" +,始终无法加载图像;
	   * 所以我改为了"/res/" + 
	   */
    return Resources.class.getResourceAsStream("/res/" + RESOURCE_MAP[resourceId]);
  }
  
  /**
   * Returns specified text as character array.
   * @param id  The id of the text.
   * @return    A text as char array.
   */
  public static char[] getChars(int id)
  {
    return TEXTBUF[id];
  }
  
  /**
   * Returns specified text as string.
   * @param id  The id of the text.
   * @return    A text as String.
   */  
  public static String getString(int id)
  {
    return new String(getChars(id));
  }
  
  /**
   * Returns specified image.
   * @param id  The id of the image.
   * @return    An image.
   */
  public static synchronized Image getImage(int id)
  {
    id -= OFFSET_IMG;
    Image img = (Image)m_images.get(new Integer(id));
    if (img == null)
    {
      try
      {
		  /*
		   * 注意原来这里写的是"/" +,始终无法加载图像;
		   * 所以我改为了"/res/" + 
		   */
        img = Image.createImage("/res/" + IMGNAME_MAP[id]);
		System.out.println("get image:" + IMGNAME_MAP[id]);
        m_images.put(new Integer(id), img);
      }
      catch (Exception e)
      {
        System.out.println("Error getting resource img " + IMGNAME_MAP[id]
           + ">>" + e.getMessage());
        e.printStackTrace();
      }
    }
    return img;
  }
}

⌨️ 快捷键说明

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