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

📄 utils.java

📁 一款基于java 的赛车类游戏 一款基于java 的赛车类游戏
💻 JAVA
字号:
/*
 * Created on 2005-02-10
 * 
 */

//package com.nano.KangooJumper;

import javax.microedition.lcdui.*;
import java.io.*;

/**
 * @author plumkawka
 * 
 */
class Loader
{
	Loader()
	{
	}

	InputStream Open(String fileName)
	{
		return getClass().getResourceAsStream(fileName);
	}
}

public class Utils
{

	private static Loader	loader	= new Loader();


	public static long startTime = -1;

	public static int getTime ()
	{
		if (startTime < 0)
			startTime = System.currentTimeMillis ();
	
		return (int) (System.currentTimeMillis () - startTime);
	}
	
	
	public static void callGc()
	{
		Runtime runtime = Runtime.getRuntime();

		for (int loop = 0; loop < 2; loop++)
		{
			runtime.gc();

			try
			{
				Thread.sleep(300);
			}
			catch (InterruptedException e)
			{
			}
		}
	}

	public static void callGcShort()
	{
		System.gc();
		System.gc();
	}

	

	public static Image LoadImage(String aFileName)
	{
		Image newImg = null;

		try
		{
			newImg = Image.createImage(aFileName);
		}
		catch (Exception e)
		{
			System.out.println("LoadImage () failed " + e.getMessage() + " " + aFileName);
			//GameCanvas.stopGame();
		}
		return newImg;
	}



	final static byte[] loadByteArray(String fileName)
	{
		try
		{
			// lyczymy rozmiar
			InputStream in = loader.Open(fileName);
			int len = -1, tmp;

			do
			{
				len++;
				tmp = in.read();
			} while (tmp != -1);

			in.close();

			// wgrywamy
			DataInputStream din = new DataInputStream(loader.Open(fileName));
			byte[] array = new byte[len];

			din.readFully(array);
			din.close();
			din = null;
			return array;
		}
		catch (IOException e)
		{
		}
		return null;
	}


	
	public static Image LoadImage (int id)
	{
   		Image	newImg = null;
		
		try 
		{
			byte[] tmpArray = loadByteArray (id);  
			newImg = Image.createImage (tmpArray, 0, tmpArray.length);
			tmpArray = null;
   		}
   		catch (Exception e)
   		{
   			Err("LoadImage () failed " + e.getMessage() + " #" + id);
   			e.printStackTrace ();
		}
   		if (newImg == null)
   			Err("LoadImage () failed (was null) #" + id);
   		return newImg;
	}




	public static byte[] loadByteArray (int id)
	{
		try
		{
			if (fileTable == null)
				readFileTable ();
			
			int num = getFileLen (id);			
			byte[] dest = new byte[num];

			if (Config.SYSTEM_LOAD_DATA_AT_STARTUP == false)
			{
				InputStream stream = openStream (id);

				stream.read (dest);
				stream.close();
				stream = null;
				
				return dest;
			}
			else
			{
				if (dataFile == null)
				{
					Utils.Err("loadByteArray(): error, data file not loaded!");
					return null;
				}

				//Utils.Log("lba: start="+fileTable[id * 3 + 1]+" - len="+num);
				System.arraycopy (dataFile,
								  fileTable[id * 3 + 1],
								  dest,
								  0,
								  num);
				return dest;
			}
		}
		catch (Exception e)
		{
		}
		return null;
	}



	public static String[] readUTFStrings(String fileName)
	{
		try
		{
			DataInputStream stream = new DataInputStream((fileName.getClass().getResourceAsStream(fileName)));

			//
			// count strings in file...
			//
			int num = 0;

			try
			{
				for (;;)
				{
					stream.readUTF();
					num++;
				}
			}
			catch (Exception e)
			{
				if (num == 0)
					Err("file " + fileName + " not found!!!");
			}
			//
			// now read them
			//
			stream = new DataInputStream((fileName.getClass().getResourceAsStream(fileName)));

			String[] dest = new String[num];

			for (int lp = 0; lp < num; lp++)
			{
				dest[lp] = stream.readUTF();
				Log(dest[lp]);
			}

			return dest;
		}
		catch (Exception e)
		{
		}
		return null;
	}

	//
	//
	//	merged data handlers
	//
	//
	final static String mergedName = "/merged.data";
	static int[] fileTable;
	static int filesNum;
	static byte[] dataFile;
	
	public static InputStream openStream (int id) throws IOException
	{
		if (fileTable == null)
			readFileTable ();
				
		if (Config.SYSTEM_LOAD_DATA_AT_STARTUP == false)
		{
			InputStream stream = mergedName.getClass ().getResourceAsStream (mergedName);
			stream.skip (fileTable[id * 3 + 1]);
			return stream;
		}
		else
		{
			return new ByteArrayInputStream (dataFile, fileTable[id * 3 + 1], getFileLen (id));
		}
	}
	
	public static int getFileLen (int id) throws IOException
	{
		if (fileTable == null)
			readFileTable ();
		
		return fileTable[id * 3 + 2];
	}
	
	private static void readFileTable () throws IOException
	{
		DataInputStream din = new DataInputStream (mergedName.getClass().getResourceAsStream (mergedName));
		filesNum = din.readInt ();
		
		fileTable = new int[filesNum * 3];

		for (int i = 0; i < filesNum * 3; i++)
			fileTable[i] = din.readInt ();
		
		if (Config.SYSTEM_LOAD_DATA_AT_STARTUP)
		{
			int len = 0;
			
			// get lenght of all data in datafile
			for (int i = 0; i < filesNum; i++)
				len += fileTable[i * 3 + 2];
		
			// allocate buffer and read
			dataFile = new byte [len];
			
			for (int i = 0; i < len; i++)
				dataFile[i] = din.readByte ();
			
			// subtract header size from file offsets
			int headerSize = filesNum * 3 * 4 + 4;

			for (int i = 0; i < filesNum; i++)
				fileTable[i * 3 + 1] -= headerSize;
		}
		
		din.close ();
		din = null;
	}


	public static void Log(String s)
	{
		System.out.println("String = " + s);
	}



	public static void Log(int s)
	{
		System.out.println(s);
	}



	public static void Err(String s)
	{
		System.err.println(s);
	}



	//
	// draws text on black board. used to display messages line 'game over'
	// 'level completed' etc
	//
	public static void paintTextOnBar(Graphics g, Image back, FontWriter font, int barColor, int frameColor, String str)
	{
		int fontHeight = font.getHeight();
		int y = (Config.SCREEN_HEIGHT - fontHeight) / 2;

		g.setClip(0, 0, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);

		// two white lines above
		g.setColor(frameColor);
		g.drawLine(0, y - 2, Config.SCREEN_WIDTH, y - 2);
		g.drawLine(0, y + 1 + fontHeight, Config.SCREEN_WIDTH, y + 1 + fontHeight);

		// draw background
		if (back != null)
		{
			g.setClip(0, y - 1, Config.SCREEN_WIDTH, fontHeight + 2);
			g.drawImage(back, 0, y - 1, Config.ANCHOR);
		}
		else
		{
			// black board
			g.setColor(barColor);
			g.fillRect(0, y - 1, Config.SCREEN_WIDTH, fontHeight + 2);
		}
		

		// message
		font.drawTextCentered(g, str, Config.SCREEN_WIDTH / 2, Config.SCREEN_HEIGHT / 2);

	}



	//
	// almost the same as paintTextOnBar with two arguments.
	// used to display two line messages
	//
	public static void paintTextOnBar(Graphics g, Image back, FontWriter font, int barColor, int frameColor, String line1, String line2)
	{
		int fontHeight = font.getHeight();
		int y = (Config.SCREEN_HEIGHT - fontHeight * 2 + 2) / 2;
		
		g.setClip(0, 0, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);
		
		// two white lines above
		g.setColor(frameColor);
		g.drawLine(0, y - 2, Config.SCREEN_WIDTH, y - 2);
		g.drawLine(0, y + 3 + (fontHeight << 1), Config.SCREEN_WIDTH, y + 3 + (fontHeight << 1));

		// draw background
		if (back != null)
		{
			g.setClip(0, y - 1, Config.SCREEN_WIDTH, (fontHeight << 1) + 4);
			g.drawImage(back, 0, y - 1, Config.ANCHOR);
		}
		else
		{
			// black board
			g.setColor(barColor);
			g.fillRect(0, y - 1, Config.SCREEN_WIDTH, (fontHeight << 1) + 4);
		}		

		// message
		font.drawTextCentered(g, line1, Config.SCREEN_WIDTH / 2, y + (fontHeight >> 1));
		font.drawTextCentered(g, line2, Config.SCREEN_WIDTH / 2, y + (fontHeight >> 1) + 2 + fontHeight);

	}

	public static void paintTextOnBar(Graphics g, Image back, FontWriter font, int barColor, int frameColor, String line1, String line2, String line3)
	{
		int fontHeight = font.getHeight();
		int y = (Config.SCREEN_HEIGHT / 2) - ((fontHeight * 3) + 4) / 2;

		g.setClip(0, 0, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);

		// two white lines above
		g.setColor(frameColor);
		g.drawLine(0, y - 2, Config.SCREEN_WIDTH, y - 2);

		g.drawLine(0, y + 6 + (fontHeight * 3), Config.SCREEN_WIDTH, y + 6 + (fontHeight * 3));
		
		// draw background
		if (back != null)
		{
			g.setClip(0, y - 1, Config.SCREEN_WIDTH, (fontHeight * 3) + 7);
			g.drawImage(back, 0, y - 1, Config.ANCHOR);
		}
		else
		{
			// black board
			g.setColor(barColor);
			g.fillRect(0, y - 1, Config.SCREEN_WIDTH, (fontHeight * 3) + 7);
		}

		// message
		font.drawTextCentered(g, line1, Config.SCREEN_WIDTH / 2, y + 1 + (fontHeight >> 1));
		font.drawTextCentered(g, line2, Config.SCREEN_WIDTH / 2, y + 3 + fontHeight + (fontHeight >> 1));
		font.drawTextCentered(g, line3, Config.SCREEN_WIDTH / 2, y + 5 + (fontHeight << 1) + (fontHeight >> 1));

	}

	public static int interpolate (int y2, int x2, int y1, int x1, int x)
	{
		return (int) (((long) (y2 - y1) * (x - x1)) / (x2 - x1)) + y1; 
	}
	
}

⌨️ 快捷键说明

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