gametools.java

来自「手机射击游戏源代码,nokia s60模拟器开发包,eclipse工具开发.不可」· Java 代码 · 共 341 行

JAVA
341
字号
package src;


/**
 * <p>Title: image</p>
 * <p>Description:GameCollege </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company:GameFactory </p>
 * @author shadow
 * @version 4.0
 */
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.EOFException;


import javax.microedition.lcdui.Image;

public class GameTools {

	byte type;
	int length_1;
	int length_2;
	int length_3;
	int Array_1[];
	int Array_2[][];
	int Array_3[][][];
	/**创建图片时使用的数组*/
	  private static byte byteArray[];
	  /**读文件时使用的输入流*/
	  private  static InputStream input;
	  /**
	  * 图片文件大小
	  */ 
	  public int image_size[] ;


	
	

	
//	一维数组
public void createArray_1(int index)
{
	Array_1=readFromFile("/res/gamedata.c",index);	

}
//二维数组
public void createArray_2(int index)
{
	int data[];
	//=new int[];

	data=readFromFile("/res/gamedata.c",index);
	int column ;
	int row ;
	Array_2=new int[ length_1][ length_2];
			for (int i = 0; i < data.length; i++) {
//				int column = i / length_1;
//				int row = i % length_2;
				column = i / length_2;
				row = i %  length_2;
				Array_2[column] [row] =data[i];
				System.out.println("Array_2[column][row]="+Array_2[column][row]);				
			}

}
//三维数组
public void createArray_3(int index)
{
	int data[];
	data=readFromFile("/res/gamedata.c",index);
	int column;
	int row;
	int line;
	System.out.println(length_1);
	System.out.println(length_2);
	System.out.println(length_3);
	Array_3=new int[ length_1][ length_2][length_3];
	for (int i = 0; i < data.length; i++) {
		column = (i / length_2)/length_3;
		row = (i / length_2)%2;
		line= i / length_1;                                                                        
		Array_3[column] [row][line] =data[i];
		System.out.println("Array_3[column][row][line]="+Array_3[column][row][line]);
	}
}
////////////////////////////
public int [] GetArray_1(int index)
{
	createArray_1(index);
	return Array_1;

}
public int [][] GetArray_2(int index)
{
	createArray_2(index);
	return Array_2;
}
public int [][][] GetArray_3(int index)
{
	createArray_3(index);
	return Array_3;

}

////////////////////////////////////////////////////////////////////////////////////////////	
/////////////////////////
//从文件读取一维数组
//index 数组编号
////////////////////////
	public int [] readFromFile(String fileName,int index) {
		
		String returnString = null;
		
		DataInputStream dis = new DataInputStream (getClass().getResourceAsStream(fileName));
		try {
		
				int ch = 0;
				byte a=0;
				byte b=0;
				byte c=0;
				int skip =0;
				 type=0;
				
					do
					{
						 length_1=1;
						 length_2=1;
						 length_3=1;
						type=dis.readByte();
						System.out.println(" TYPE="+type);
					 
						switch(type)
						{
						case 1:
							a = dis.readByte();
							b = dis.readByte();
							length_1=(b << 8) | (a & 0xff);
							a = dis.readByte();
							b = dis.readByte();
							skip=(b << 8) | (a & 0xff);
						
							break;
						case 2:
							a = dis.readByte();
							b = dis.readByte();
							length_1=(b << 8) | (a & 0xff);
							a = dis.readByte();
							b = dis.readByte();
							length_2=(b << 8) | (a & 0xff);
							a = dis.readByte();
							b = dis.readByte();
							skip=(b << 8) | (a & 0xff);
							break;
						case 3:
							a = dis.readByte();
							b = dis.readByte();
							length_1=(b << 8) | (a & 0xff);
							a = dis.readByte();
							b = dis.readByte();
							length_2=(b << 8) | (a & 0xff);
							a = dis.readByte();
							b = dis.readByte();
							length_3=(b << 8) | (a & 0xff);
                            a = dis.readByte();
							b = dis.readByte();
							skip=(b << 8) | (a & 0xff);
							break;
						default :
							System.out.println("ERROR TYPE!!!");
						}
						System.out.println(length_1+"+"+length_2+"+"+length_3);
						if(index!=1) {
							
							System.out.println("skip:"+skip);
							dis.skipBytes(skip);
							
						}
					} while(index--!=1);
			         int k=length_1*length_2*length_3; 
					int[] data=new int[k];
					System.out.println(k);
					for (int i=0;i<k;i++ ) 
						{
						
							a = dis.readByte();
							
							if(a==-128)
							 {
							    System.out.println("MARK= "+a);
								b = dis.readByte();
								System.out.println("Type Of MARK= "+b);
								switch(b)
								{
								 case 1://相同的shortInt数据
									
									break;		
								
								 case 2://shortInt数据
								 	c = dis.readByte();
								 	System.out.println("Num Of INT= "+c);
									
									for(int j=1;j<=c;j++)
									{
											
										a = dis.readByte();
										b = dis.readByte();			
										ch=(b << 8) | (a & 0xff);
										data[i]=ch;
										System.out.println("INT=="+data[i]);
										i++;
										
										
									}
									i--;
									break;
									
								 case 3://相同的byte数据
									
									c = dis.readByte();
									b = dis.readByte();
									System.out.println("Num Of Byte= "+c);
									for(int j=1;j<=c;j++)
									{

										data[i]=b;	
										System.out.println("trhe same i="+i+"   val"+data[i]);
										if(i==k-1)continue;
										i++;
									}
									
										i--;	
									break;	
								
									
													
								}	
							 }
							else{
								
							data[i]=a;
							System.out.println("i="+i+"   val"+data[i]);
							
								}
							//System.out.println("i="+i+"   val"+data[i]);
							if(i==k-1)break;
						}
					
					return data;
			
			
			} catch(EOFException e)
			{
				}catch (IOException e) {
				e.printStackTrace();
			}
			
			finally{
				try {
					dis.close();
					
				} catch (IOException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
				}
			return null;
			
		}
////////////////////////////////////////////////////////////////////////////////////////////	

	 
	  /**
	   *
	   * @param  Image[] img 存放的图片数组
	   * @param String imgname 读取资源图片名称
	   * @param  int Image_ID 图片读取的ID,一般为零
	   * @param  int Image_Num 图片读取的数目
	   */
	  public void createImage(Image[] img, String imgname,int ID,
	                           
	                           int Image_Num,Object o) {


	    int AllImage=0;
	    int start=1;
	  	
	    
	    try {
	     
	      input = o.getClass().getResourceAsStream("/res/"+imgname);
	      DataInputStream datainputstream = new DataInputStream(input);
	      AllImage=datainputstream.readUnsignedByte();
	      System.out.println(AllImage);
	      
	      image_size=new int [AllImage];
	      
	       for (int i = 0; i <AllImage ; i++) {
	       	byte a=datainputstream.readByte();
	       	byte b=datainputstream.readByte();
	      	image_size[i] =  (b << 8) | (a & 0xff);
	      	if(i<=Image_Num)
	      	{
	      		start+=2;
	      	}
	      	System.out.println("image_size[i]="+image_size[i]);
	      }
	       
	       //datainputstream.readByte();
	       for(int i=0;i<ID;i++)
       {
	       datainputstream.skip(image_size[i]);
	       }
	       datainputstream.skip(1);
	      for (int i = ID; i <= Image_Num; i++) {
	            byteArray = null;
	            byteArray = new byte[image_size[i]];
	            System.out.println("i="+i);
//	        	for(int j=0;j<image_size[i] ;j++)
//	        		{
//	        		System.out.println("j="+j);
//	        		byteArray[j]=datainputstream.readByte();
//	        		System.out.println("byteArray[j]="+byteArray[j]);
//	        		}
	            datainputstream.read(byteArray);
	          img[i] = Image.createImage(byteArray, 0, byteArray.length );
	          datainputstream.readByte();   
	        start += image_size[i]+1;
	      }
	    }
	    catch (Exception ex) {
	      ex.printStackTrace();
	    }
	  }
	}
	
	
	

⌨️ 快捷键说明

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