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

📄 canvas_stageselect.java

📁 05年游戏学院一个学生写的项目
💻 JAVA
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
// Download by http://www.codefans.net
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class Canvas_StageSelect extends GameCanvas
{
	MIDlet_Main mainMIDlet;
	Graphics g;
	Thread t;
	static boolean isThreadCancel;
	Font font=Font.getFont(0,0,16);
	short selRectX=(short) ((Class_Camera.width-font.stringWidth(Class_Map.name))/2-2);
	short selRectY=20;
	//选择框变化记数
	private byte rectChangeCount;
	
	public Canvas_StageSelect(MIDlet_Main mid)
	{
		super(false);
		setFullScreenMode(true);
		mainMIDlet=mid;
		g=getGraphics();
		save();
		load();
		render();
		t=new Thread()
		{
			public void run() 
			{
				if(!isThreadCancel)
				{
					long start=System.currentTimeMillis();
					render();
					long end=System.currentTimeMillis();
					short time=(short)(end-start);
					try {
						if(time<100)
							Thread.sleep(100-time);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					mainMIDlet.dis.callSerially(this);
				}
			}
		};
		t.start();
	}
	
	//处理选择框
	public void showSelRect()
	{
		if(rectChangeCount%2==0)
		{
			g.setColor(255,255,255);
			g.drawRoundRect(selRectX,selRectY,font.stringWidth(Class_Map.name)+4,font.getHeight(),2,2);
		}
	}
	
	public void render()
	{
		//选择框变化记数
		rectChangeCount++;
		g.setFont(font);
		g.setColor(50,125,255);
		g.fillRect(0,0,Class_Camera.width,Class_Camera.height);
		showSelRect();
		for(byte i=1;i<Class_Map.IDMax+2;i++)
		{
			g.setColor(130,245,255);
			g.drawString("第"+i+"关",(Class_Camera.width-font.stringWidth("第"+i+"关"))/2,i*20,0);
		}
		g.drawString("确定",5,Class_Camera.height-20,0);
		g.drawString("返回菜单",Class_Camera.width-60,Class_Camera.height-20,0);
		flushGraphics();
	}
	
	protected void keyPressed(int keyCode)
	{
		switch(keyCode)
		{
			case -1:
			case 50:
			{
				if(selRectY==20)
					selRectY=(short) (Class_Map.IDMax*20+20);
				else 
					selRectY-=20;
				break;
			}
			
			case -2:
			case 56:
			{
				if(selRectY==(short) (Class_Map.IDMax*20+20))
					selRectY=20;
				else 
					selRectY+=20;
				break;
			}

			case -5:
			case -6:
			case 53:
			{
				Class_Map.ID=(byte) (selRectY/20-1);
				Canvas_Main.isThreadCancel=false;
				mainMIDlet.dis.setCurrent(new Canvas_Main(mainMIDlet));
				isThreadCancel=true;
				System.gc();
				break;
			}
			
			case -7:
				Canvas_Menu.isThreadCancel=false;
				mainMIDlet.dis.setCurrent(new Canvas_Menu(mainMIDlet));
				isThreadCancel=true;
				break;
		}
	}
	
	public static void save()
	{
		try {
			RecordStore rs=RecordStore.openRecordStore("关卡",true);
			ByteArrayOutputStream bo=new ByteArrayOutputStream();
			DataOutputStream out=new DataOutputStream(bo);
			out.writeByte(Class_Map.IDMax);
			
			byte[] save=bo.toByteArray();
			if(rs.getNumRecords()>0)
				rs.setRecord(1,save,0,save.length);
			else
				rs.addRecord(save,0,save.length);
			out.close();
			rs.closeRecordStore();
			
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void load()
	{
		try {
			RecordStore rs=RecordStore.openRecordStore("关卡",false);
			byte[] load=rs.getRecord(1);
			ByteArrayInputStream bi=new ByteArrayInputStream(load);
			DataInputStream in=new DataInputStream(bi);
			Class_Map.IDMax=in.readByte();
			in.close();
			rs.closeRecordStore();
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

⌨️ 快捷键说明

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