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

📄 zonlayer.java

📁 基于J2ME 开发的冒险类手机游戏
💻 JAVA
字号:
package zonja;

import java.io.InputStream;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;



public class ZonLayer {
	
	public int scrWidth = 0;
	public int scrHeight = 0;
	public int offsetX = 0;
	public int offsetY = 0;
	
	public static final int _FIND_NOTHING_ = -1;
	
	public static final int MAX_IMAGES = 25;
	// the imagenum can't exceed MAX_IMAGES
	static protected Image [] images = new Image[MAX_IMAGES];
	static protected String [] imageNames = new String[MAX_IMAGES];
	
	public ZonLayer(int scrWidth, int scrHeight) {
		this.scrWidth = scrWidth;
		this.scrHeight = scrHeight;
	}

	//methods : abstract methods
	void paint(Graphics g, boolean bReverse, boolean bWrap){
		
	}
	
	//methods : inherited by sub-class;
	protected int  getHeight(){
		return scrHeight;
	}
	protected int  getWitdth(){
		return scrWidth;
	}
	protected int  getOffsetX(){
		return offsetX;
	}
	protected int  getOffsetY(){
		return offsetY;
	}
	protected void move(int dx, int dy){
		offsetX += dx;
		offsetY += dy;
	}
	protected void setPosition(int x, int y){
		offsetX = x;
		offsetY = y;
	}

	
	
	//methods: use resource image
	protected static boolean addImage(Image image, String imageName) {
		int i = 0;
		while(images[i] != null) {
			i ++;
			if(i >= MAX_IMAGES)
				return false;
		}
		
		images[i] = image;
		imageNames[i] = imageName;
		
		return true;
	}
	
	public  Image readImage(String binfile, int pos)
	{
		byte buffer[];
		int len;

		try {
			InputStream is = this.getClass().getResourceAsStream("/" + binfile);
			
			is.skip(pos);
			
			len  = (is.read() & 0xFF) << 24;
			len  |= (is.read() & 0xFF) << 16;
			len  |= (is.read() & 0xFF) << 8;
			len  |= (is.read() & 0xFF);

			buffer = new byte[len];
		
			is.read(buffer, 0, buffer.length);

			is.close();
			is = null;
		
			System.gc();
		} catch (Exception e) {
			buffer = null;
			e.printStackTrace();
			System.gc();
			return null;
		}

		return Image.createImage(buffer, 0, buffer.length);
	}
	
	public boolean loadImage(String imageName, String fileName, boolean buildReverse, int pos) {
		Image image;
		String name;
		
		try {
			image = readImage("image.bin", pos);
			name = imageName;
			
			if(!addImage(image, name))
				return false;
		} catch (Exception e) {
			return false;
		}

/*
    		if (buildReverse) {
			
			// Implimented in MIDP 1.0.Transparent pixel is not supported.
			int width = image.getWidth(); 
			int height = image.getHeight();
			
			reverseImage = null;
			reverseImage = Image.createImage(width, height);
			Graphics g = reverseImage.getGraphics();
	
			for (int j = 0; j < width / 2; j++) {
				g.translate(j, 0);
				g.setClip(0, 0, 1, height);
				g.drawImage(image, - (width - 1 - j), 0, Graphics.TOP | Graphics.LEFT);
				g.translate(- j, 0);
				g.translate(width - 1 - j, 0);
				g.setClip(0, 0, 1, height);
				g.drawImage(image, - j, 0, Graphics.TOP | Graphics.LEFT);
				g.translate(- (width - 1 - j), 0);
			}


			int width = image.getWidth(); 
			int height = image.getHeight(); 

 			// this solution cannot show transparent color on nokia 3220
 			// Implimented in MIDP 2.0
			try {
				image = Image.createImage(image, 0, 0, width, height, Sprite.TRANS_MIRROR);
				name = name + "Reverse";
				
				addImage(image, name);
			}
			catch(Exception e) {
				return false;
			}


	
			// this solution doesn't work on nokia 7610
			int [] rgb  = new int[width * height];

			try {
				image.getRGB(rgb, 0, width, 0, 0, width, height); 			
				for(int i = 0; i < height; i ++) 
					for(int j = 0; j < width / 2; j ++) { 
						int tempColor = rgb[i * width + j]; 
						rgb[i * width + j] = rgb[i * width + (width - 1 - j)]; 
						rgb[i * width + (width - 1 - j)] = tempColor; 
					} 
				
				image = Image.createRGBImage(rgb, width, height, true);
				name = name + "Reverse";
				
				addImage(image, name);
			}
			catch(Exception e) {
				return false;
			}			

		}
	*/
		image = null;
		name = null;
		return true;
	}
	
	public void removeImage(String imageName) {
		int i = 0;
		while(imageNames[i] == null || 
			(imageNames[i] != null && !imageNames[i].equals(imageName))) {
			i ++;
			if(i >= MAX_IMAGES)
				return;
		}
		
		//when resource is null, system do gc job
		images[i] = null;
		imageNames[i] = null;
	}
	
	public static boolean isImageReady(String imageName) {
		int i = 0;
		while(imageNames[i] == null || 
			(imageNames[i] != null && !imageNames[i].equals(imageName))) {
			i ++;
			if(i >= MAX_IMAGES)
				return false;
		}
		
		return true;
	}
	
	public static Image findImage(String imageName) {
		int i = 0;
		while(imageNames[i] == null || 
			(imageNames[i] != null && !imageNames[i].equals(imageName))) {
			i ++;
			if(i >= MAX_IMAGES)
				return null;
		}
		
		return images[i];
	}
	
	public static int getWidth(String imageName){
		int i = 0;
		while(imageNames[i] == null || 
			(imageNames[i] != null && !imageNames[i].equals(imageName))) {
			i ++;
			if(i >= MAX_IMAGES)
				return ZonLayer._FIND_NOTHING_;
		}
		return images[i].getWidth();
	}
	
	public static int getHeight(String imageName){
		int i = 0;
		while(imageNames[i] == null || 
			(imageNames[i] != null && !imageNames[i].equals(imageName))) {
			i ++;
			if(i >= MAX_IMAGES)
				return ZonLayer._FIND_NOTHING_;
		}
		return images[i].getHeight();
	}
	

}

⌨️ 快捷键说明

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