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

📄 standard.java

📁 --- --- --- -基于蓝牙的J2ME游戏框架---------------很有用的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

//import java.io.IOException;

class Standard
{

	Standard()
	{
	}

	/*****************************************************************************************************/
	/**																									**/
	/** Text as Image																					**/
	/**																									**/
	/*****************************************************************************************************/
	public static int LEFT_SOFTKEY1 = 0;
	public static int LEFT_SOFTKEY2 = 0;
	public static int RIGHT_SOFTKEY1 = 0;
	public static int RIGHT_SOFTKEY2 = 0;

	static public void setSoftKey(Canvas c)
	{
		boolean found;
		LEFT_SOFTKEY2 = Canvas.KEY_STAR;
		RIGHT_SOFTKEY2 = Canvas.KEY_POUND;
		try
		{
		   Class.forName("com.siemens.mp.lcdui.Image");
		 // Set Siemens specific keycodes
			LEFT_SOFTKEY1 = -1;
			RIGHT_SOFTKEY1 = -4;
		}
		catch (ClassNotFoundException ignore3)
		{
			for (int i=-127;i<127;i++)
			{
				try
				{
					if (c.getKeyName(i).toUpperCase().indexOf("SOFT")>=0)
					{
						if (c.getKeyName(i).indexOf("1")>=0) LEFT_SOFTKEY1=i;
						if (c.getKeyName(i).indexOf("2")>=0) RIGHT_SOFTKEY1=i;

					}
				}
				catch(Exception e)
				{
				}
			}
			if (LEFT_SOFTKEY1 == 0 && RIGHT_SOFTKEY1 == 0)
			{
				LEFT_SOFTKEY1 = -6;
				RIGHT_SOFTKEY1 = -7;
			}
		}

	}

	/*****************************************************************************************************/
	/**																									**/
	/** Text as Image																					**/
	/**																									**/
	/*****************************************************************************************************/
	static Image[] imageText = new Image[18];

	static public void createImageText(int index, Font f, StringBuffer b)
	{
		int w = f.stringWidth(b.toString()),
			h = f.getHeight()+2;
		imageText[index] = Image.createImage(w,h);
		Graphics g = imageText[index].getGraphics();
		g.setFont(f);
		g.setClip(0,0,w,h);
//#ifdef nokia
		g.setColor(0x008080c0);
		g.fillRect(0,0,w,h);
		g.setColor(0x00000000);
		g.drawString(b.toString(),1,2, Graphics.TOP | Graphics.LEFT);
//#else
		g.setColor(0x00303060);
		g.fillRect(0,0,w,h);
		drawThickText(g,b,1,1,Graphics.TOP | Graphics.LEFT,0x00000000,0x00ffffff);
//#endif
	}

	/*****************************************************************************************************/
	/**																									**/
	/** Text wrapper																					**/
	/**																									**/
	/*****************************************************************************************************/
	static int strtLine = 0;
	static int totLines = 0;
	static int showLines = 0;

	static public void boxWrap(Graphics g, Font GAME_FONT, int fontHeight, StringBuffer buf, int x, int y, int w, int h)
	{
		int ys = (totLines < showLines) ? y+4+(((h-8)-(fontHeight*totLines))>>1) : y+4+(((h-8)-(fontHeight*showLines))>>1),
			ye = (totLines < showLines) ? fontHeight*totLines : fontHeight*showLines;
		g.setClip(x, y, w, h);
		g.setColor(0x00e0e0e0);
		g.fillRoundRect(x, y, w, h,8,8);
		g.setColor(0x00000000);
		if ((showLines+strtLine) < totLines)
		{
			// Draw down arrow
			g.drawLine(x+(w>>1)-2, y+h-4, x+(w>>1)+2, y+h-4);
			g.drawLine(x+(w>>1)-2, y+h-4, x+(w>>1),   y+h-1);
			g.drawLine(x+(w>>1)+2, y+h-4, x+(w>>1),   y+h-1);
		}
		if (strtLine != 0)
		{
			// Draw up arrow
			g.drawLine(x+(w>>1)-2, y+4, x+(w>>1)+2, y+4);
			g.drawLine(x+(w>>1)-2, y+4, x+(w>>1),   y+1);
			g.drawLine(x+(w>>1)+2, y+4, x+(w>>1),   y+1);
		}
		g.setClip(x+4, ys, w-8, ye/*h-8*/);
		wrapper(g, GAME_FONT, fontHeight, buf);
	}

	static public void wrapper(Graphics g, Font GAME_FONT, int fontHeight, StringBuffer buf)
	{
		if (totLines == 0) wrapperLines(GAME_FONT, fontHeight, buf, g.getClipWidth(), g.getClipHeight());
		int i = 0,
			lasti = 0,
			lastSpace = 0,
			xe = g.getClipWidth(),
			xs = g.getClipX()+(xe>>1),
			y = g.getClipY(),
			ye = y+g.getClipHeight() - fontHeight,
			line = 0;

		while (i < buf.length())
		{
			if (buf.charAt(i) == ' ' || buf.charAt(i) == '|')
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe)
				{
					if (strtLine <= line && lasti <= lastSpace)
					{
						g.drawString(buf.toString().substring(lasti, lastSpace),xs,y, Graphics.TOP | Graphics.HCENTER);
						y += fontHeight;
						if (y > ye) break;
					}
					lasti = lastSpace+1;
					line++;
				}
				else if (buf.charAt(i) == '|')
				{
					if (strtLine <= line)
					{
						g.drawString(buf.toString().substring(lasti, i),xs,y, Graphics.TOP | Graphics.HCENTER);
						y += fontHeight;
						if (y > ye) break;
					}
					lasti = i+1;
					line++;
				}
				else lastSpace = i;
			}
			else if ((i+1) >= buf.length())
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe)
				{
					if (strtLine <= line && lasti <= lastSpace)
					{
						g.drawString(buf.toString().substring(lasti, lastSpace),xs,y, Graphics.TOP | Graphics.HCENTER);
						y += fontHeight;
						if (y > ye) break;
					}
					lasti = lastSpace+1;
					line++;
				}
				g.drawString(buf.toString().substring(lasti, i),xs,y, Graphics.TOP | Graphics.HCENTER);
			}
			i++;
		}
	}
	static public void wrapper2(Graphics g, Font GAME_FONT, int fontHeight, StringBuffer buf, int y, int ys)
	{
		if (totLines == 0) wrapperLines(GAME_FONT, fontHeight, buf, g.getClipWidth(), g.getClipHeight());
		int i = 0,
			lasti = 0,
			lastSpace = 0,
			xe = g.getClipWidth(),
			xs = g.getClipX()+(xe>>1),
			ye = ys+g.getClipHeight() /*- fontHeight*/,
			line = 0;

		while (i < buf.length())
		{
			if (y > ye) break;
			if (buf.charAt(i) == ' ' || buf.charAt(i) == '|')
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe)
				{
					if ((y+fontHeight)>ys) g.drawString(buf.toString().substring(lasti, lastSpace),xs,y, Graphics.TOP | Graphics.HCENTER);
					y += fontHeight;
					lasti = lastSpace+1;
				}
				else if (buf.charAt(i) == '|')
				{
					if ((y+fontHeight)>ys) g.drawString(buf.toString().substring(lasti, i),xs,y, Graphics.TOP | Graphics.HCENTER);
					y += fontHeight;
					lasti = i+1;
				}
				else lastSpace = i;
			}
			else if ((i+1) >= buf.length())
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe)
				{
					if ((y+fontHeight)>ys) g.drawString(buf.toString().substring(lasti, lastSpace),xs,y, Graphics.TOP | Graphics.HCENTER);
					y += fontHeight;
					lasti = lastSpace+1;
				}
				g.drawString(buf.toString().substring(lasti, i),xs,y, Graphics.TOP | Graphics.HCENTER);
			}
			i++;
		}
	}


	static public void wrapperLines(Font GAME_FONT, int fontHeight, StringBuffer buf, int xe, int ye)
	{
		int i = 0,
			lasti = 0,
			lastSpace = 0,
			x = 0,
			line = 0;

		while (i < buf.length())
		{
			if (buf.charAt(i) == ' ' || buf.charAt(i) == '|')
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe || buf.charAt(i) == '|')
				{
					lasti = lastSpace+1;
					line++;
				}
				else if (buf.charAt(i) == '|')
				{
					lasti = i+1;
					line++;
				}
				else lastSpace = i;
			}
			else if ((i+1) >= buf.length())
			{
				if (GAME_FONT.stringWidth(buf.toString().substring(lasti, i)) > xe)
				{
					lasti = lastSpace+1;
					line++;
				}
				line++;
			}
			i++;
		}
		totLines = line;
		showLines = ye/fontHeight;
	}

	// Draw text with color c, with a tick border of color b, at x,y with handle h
	static public void drawThickText(Graphics g, StringBuffer buf, int x, int y, int h, int b, int c)
	{
		int j,k;
		g.setColor(b);
		for (j = -1; j < 2; j++)
		{
			for (k = -1; k < 2; k++)
			{
				if (j == 0 && k == 0) continue;
				g.drawString(buf.toString(),x+k,y+j, h);
			}
		}
		g.setColor(c);
		g.drawString(buf.toString(),x,y, h);
	}


	/*****************************************************************************************************/
	/**																									**/
	/** Ticker																							**/
	/**																									**/
	/*****************************************************************************************************/
	public static int tickerX = 0;
	public static int tickerY = 0;
	public static int tickerStartChar = 0;
	public static int tickerEndChar = 0;
	public static StringBuffer tickerBuf;

	static public void myTicker(Graphics g, Font GAME_FONT, int scrollDif, int w)
	{
		// Find the start character in the string
		tickerX += scrollDif;
		int x;
		while ((scrollDif <= 0 && tickerX + GAME_FONT.stringWidth(tickerBuf.toString().substring(0, tickerStartChar)) < 0) ||
			   (scrollDif > 0 && tickerX + GAME_FONT.stringWidth(tickerBuf.toString().substring(0, tickerStartChar)) > w))
		{
			tickerStartChar++;

			if (tickerStartChar >= tickerBuf.length())
			{
				tickerX = (scrollDif <= 0) ? w : 0;
				tickerStartChar = 0;
				tickerEndChar = 0;
			}
		}
		if (tickerStartChar > 0) tickerStartChar--;
		if (tickerEndChar < tickerStartChar) tickerEndChar = tickerStartChar;
		x = tickerX + GAME_FONT.stringWidth(tickerBuf.toString().substring(0, tickerStartChar));
		while (tickerX + GAME_FONT.stringWidth(tickerBuf.toString().substring(tickerStartChar, tickerEndChar)) < w)
		{
			tickerEndChar++;
			if (tickerEndChar >= tickerBuf.length())
			{
				tickerEndChar--;
				break;
			}
		}

		g.setColor(0x00ffffff);
		g.drawString(tickerBuf.toString().substring(tickerStartChar, tickerEndChar),x,tickerY, Graphics.TOP | Graphics.LEFT);
	}


	/*****************************************************************************************************/
	/**																									**/
	/** Record Store stuff																				**/
	/**																									**/
	/*****************************************************************************************************/
    private final static int TRUE = 0; // bytes[TRUE] = 'T'
    private final static int FALSE = 1; // bytes[FALSE] = 'F'
    private final static byte[] bytes = { 'T', 'F' };
    private static RecordStore rs = null;

	private static void openRecordStore(StringBuffer buf)
    throws RecordStoreException
	{
		try
		{
			if (rs == null)
			{
			  rs = RecordStore.openRecordStore(buf.toString(), true);
			}
		}
		catch (RecordStoreException e)
		{
		}
	}

    private static void closeRecordStore()
    throws RecordStoreException
	{
		try
		{
			if (rs != null)
			{
				rs.closeRecordStore();
				rs = null;
			}
		}
		catch (RecordStoreException e)
		{
		}
	}

    public static void getSettings(GMCanvas gm)
    {
		StringBuffer buf = new StringBuffer("SET");
		try
		{
			openRecordStore(buf);
		    if (rs.getNumRecords() == 0)	// Must be first time ever app is opened. So create some settings
		    {
				rs.addRecord(bytes, FALSE, 1);	//Default for Continue Game available = false;
				GMCanvas.options[0] = false;
				rs.addRecord(bytes, TRUE, 1);	//Default for Sound On = true;
				rs.addRecord(bytes, TRUE, 1);	//Default for detailHigh = true;

				// Hiscores
				{
					ByteArrayOutputStream 	bout = new ByteArrayOutputStream();
					DataOutputStream 		dout = new DataOutputStream(bout);
					byte[] data = null;
					dout.writeInt(0);
					for (int i = 0; i < hsNames.length; i++)
					{
						hsNames[i] = '.';
						dout.writeChar(hsNames[i]);
					}
					for (int i = 0; i < MAX_SCORES; i++)
					{
						hsScores[i] = (MAX_SCORES*1000)-(i*1000);
						dout.writeInt(hsScores[i]);
					}
					data = bout.toByteArray();
					rs.addRecord(data, 0, data.length);
					bout = null;

⌨️ 快捷键说明

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