?? gametools.java
字號(hào):
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[][][];
/**創(chuàng)建圖片時(shí)使用的數(shù)組*/
private static byte byteArray[];
/**讀文件時(shí)使用的輸入流*/
private static InputStream input;
/**
* 圖片文件大小
*/
public int image_size[] ;
// 一維數(shù)組
public void createArray_1(int index)
{
Array_1=readFromFile("/res/gamedata.c",index);
}
//二維數(shù)組
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]);
}
}
//三維數(shù)組
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;
}
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////
//從文件讀取一維數(shù)組
//index 數(shù)組編號(hào)
////////////////////////
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數(shù)據(jù)
break;
case 2://shortInt數(shù)據(jù)
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數(shù)據(jù)
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 自動(dòng)生成 catch 塊
e1.printStackTrace();
}
}
return null;
}
////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* @param Image[] img 存放的圖片數(shù)組
* @param String imgname 讀取資源圖片名稱
* @param int Image_ID 圖片讀取的ID,一般為零
* @param int Image_Num 圖片讀取的數(shù)目
*/
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();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -