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

📄 cover.java

📁 很简单的J2ME源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import java.io.InputStream;import java.util.Vector;public class Cover extends GameCanvas implements Runnable {	public static final int num_persons_perframe = 3;	private static final int frame_width = 48;    public static final int frameDelay = 83;	private Thread thread = null;	private static final String[] cover_notice = {"PUSH", "START BUTTON"};	private static final String[] source_person_description = {		"A descendent of the old Han,raising an army to restore the dynasty\n-Liu Bei",		"Bearer of the mighty Black Dragon,a true scholar-warrior\n-Guan Yu",		"Wielded such power and honor,10,000 men could not overcome him\n-Zhang Fei",		"A warlord brilliant and ambitious, a leader of men,a weaver of words\n-Cao Cao",		"Unrightful possessor of the Imperial Seal,the key to the dynasty\n-Yuan Shu",		"The greatest warrior in China,but destined for treachery\n-Lu Bu"	};	private Sprite sword_sprite, person_sprite;	private Image image_title_name, image_title_sword;	private static int num_frames_person, max_running_times, screen_width, screen_height, sprite_sword_x, sprite_sword_y;	private boolean had_collided = false;	private Graphics g;	private static int game_status;	private Display display;	private static final String[] menu_options = {"New History", "Play Game", "Game Option", "Exit Game"};	private int menu_index = 0;	private DoEMIDlet DMIDlet;	public Cover(Display d, DoEMIDlet mid) {		super(true);		display = d;		DMIDlet = mid;		// 读取需要的图片		try {			image_title_name = readImage("/res/images/cover.bin", 476);			image_title_sword = readImage("/res/images/cover.bin", 0);			sword_sprite = new Sprite(image_title_sword, 176, 25);			person_sprite = new Sprite(readImage("/res/images/cover.bin", 1330), frame_width, frame_width);		} catch (Exception e) {}		num_frames_person = person_sprite.getFrameSequenceLength();		max_running_times = num_frames_person / num_persons_perframe;		game_status = Define.STATUS_STARTING_COVER;	}	public void run() {		thread = new Thread(this);        thread.start();	}	public void start() {		display.setCurrent(this);		g = getGraphics();		setFullScreenMode(true);		screen_width = getWidth();		screen_height = getHeight();		while (getGameStatus() < Define.STATUS_STARTING) {			sprite_sword_y = 49;			sprite_sword_x = screen_width;			had_collided = false;			while (game_status == Define.STATUS_STARTING_COVER) {				drawSwordCover(g);				try { thread.sleep(frameDelay); }				catch (Exception e) { }			}			System.gc();			int x = 0;			while (getGameStatus() == Define.STATUS_STARTING_WAITING) {				input();				drawCover(g);				try { thread.sleep(frameDelay); }				catch (Exception e) { }				x = x + Define.STARTING_TIMEOUT / frameDelay;				if (x > Define.STARTING_TIMEOUT) {					setGameStatus(Define.STATUS_STARTING_DESC);					break;				}			}			System.gc();			if (getGameStatus() == Define.STATUS_STARTING_DESC) {				for (int i = 0; i < max_running_times; i++) {					drawPersons(g, i);					try { thread.sleep(Define.PERDESC_TIMEOUT); }					catch (Exception e) { }				}				setGameStatus(Define.STATUS_STARTING_COVER);				System.gc();			}		}		System.gc();		while (getGameStatus() == Define.STATUS_OPTIONSETTING) {			drawMenu(g);			menuInput();			try { thread.sleep(frameDelay); }			catch (Exception e) { }		}	}	private void drawSwordCover(Graphics g) {		initDraw(g);		// 绘制标题图片		g.drawImage(image_title_name, 0, 10, Graphics.TOP|Graphics.LEFT);		// 处理精灵 - 剑		if (false == had_collided) sprite_sword_x = sprite_sword_x - 10;		else sprite_sword_x = sprite_sword_x + 5;		if (sprite_sword_x < -10) had_collided = true;		if (had_collided && sprite_sword_x >= 0) sprite_sword_x = 0;		sword_sprite.setPosition(sprite_sword_x, sprite_sword_y);		sword_sprite.paint(g);		if (had_collided && sprite_sword_x >= 0) {			sprite_sword_x = 0;			g.setColor(Define.NORMAL_FONT_COLOR);			g.setFont(Define.NORMAL_FONT_STYLE);			for (int n = 0; n < cover_notice.length; n++) g.drawString(cover_notice[n], screen_width / 2, 80 + n * Define.NORMAL_FONT_STYLE.getHeight(), Graphics.BASELINE|Graphics.HCENTER);			finishDraw();			setGameStatus(Define.STATUS_STARTING_WAITING);		}		finishDraw();	}	private void drawPersons(Graphics g, int running_id) {		initDraw(g);		for (int i = 0; i < num_persons_perframe; i++) drawPersonDescription(g, i, running_id);		finishDraw();	}	private void drawCover(Graphics g) {		initDraw(g);		// 绘制标题图片		g.drawImage(image_title_name, 0, 10, Graphics.TOP|Graphics.LEFT);		// 绘制图片 - 剑		g.drawImage(image_title_sword, 0, sprite_sword_y, Graphics.TOP|Graphics.LEFT);		g.setColor(Define.NORMAL_FONT_COLOR);		g.setFont(Define.NORMAL_FONT_STYLE);		for (int n = 0; n < cover_notice.length; n++) g.drawString(cover_notice[n], screen_width / 2, 80 + n * Define.NORMAL_FONT_STYLE.getHeight(), Graphics.BASELINE|Graphics.HCENTER);		finishDraw();	}	private void drawPersonDescription(Graphics g, int person_id, int running_id) {        int action_id = running_id * num_persons_perframe + person_id;        int descriptionY = screen_height / num_persons_perframe * person_id + 5;        int spriteX;        if (1 == (person_id % 2)) spriteX = screen_width - frame_width;        else spriteX = 0;		// 绘制人物描述        g.setColor(Define.NORMAL_FONT_COLOR);        g.setFont(Define.NORMAL_FONT_STYLE);        if (null != source_person_description[action_id]) {            Vector vector = Common.getSubsection(source_person_description[action_id], Define.NORMAL_FONT_STYLE, screen_width - frame_width, " ,.?!");            for(int j=0;j<vector.size();j++) g.drawString((String)vector.elementAt(j), ((person_id + 1) % 2) * frame_width + 2, descriptionY + 13 * j,Graphics.TOP|Graphics.LEFT);        }        person_sprite.setFrame(action_id);        person_sprite.setPosition(spriteX, descriptionY);        person_sprite.paint(g);		finishDraw();		try { thread.sleep(frameDelay * 2); }		catch (Exception e) { }    }	private void drawMenu(Graphics g) {		initDraw(g);		int string_start_x = (screen_width - getMenuWidth(menu_options)) / 2;		int string_start_y = screen_height / 3;		int string_y = 0;		String longer_string = "";		// 绘制菜单列表		for (int i = 0; i < menu_options.length; i++) {			string_y = string_start_y + (i - 1) * Define.HIGHLIGHT_FONT_STYLE.getHeight() / 2 * 3;			// 绘制游标			if (i == menu_index) {				g.setColor(Define.CURSOR_POINT_COLOR);				g.fillTriangle(string_start_x - 10, string_y, string_start_x - 5, string_y + 5, string_start_x - 10, string_y + 10);				g.setColor(Define.HIGHLIGHT_FONT_COLOR);				g.setFont(Define.HIGHLIGHT_FONT_STYLE);			}			else {				g.setColor(Define.NORMAL_FONT_COLOR);				g.setFont(Define.NORMAL_FONT_STYLE);			}			g.drawString(menu_options[i], string_start_x, string_y, Graphics.TOP|Graphics.LEFT);			if (longer_string.length() < menu_options[i].length()) longer_string = menu_options[i];		}		// 绘制菜单框		g.setColor(Define.MENU_BORDER_COLOR);		g.setStrokeStyle(Define.MENU_BORDER_STYLE);		g.drawRect(			string_start_x - 20, string_start_y - 10 - Define.HIGHLIGHT_FONT_STYLE.getHeight() ,			Define.HIGHLIGHT_FONT_STYLE.stringWidth(longer_string) + 40, string_y - string_start_y + 20 + 2 * Define.HIGHLIGHT_FONT_STYLE.getHeight()		);		// 绘制菜单框外围		paintMessageRect(g,			string_start_x - 20, string_start_y - 10 - Define.HIGHLIGHT_FONT_STYLE.getHeight(),			Define.HIGHLIGHT_FONT_STYLE.stringWidth(longer_string) + 40, string_y - string_start_y + 20 + 2 * Define.HIGHLIGHT_FONT_STYLE.getHeight(),			Define.MENU_BORDER_COLOR, Define.MENU_BORDER_STYLE);		finishDraw();	}	public void setGameStatus(int gs) {		game_status = gs;	}	public int getGameStatus() {		return game_status;	}	private void initDraw(Graphics g) {		// 填充背景        g.setColor(Define.BACKGROUND_COLOR);        g.fillRect(0, 0, screen_width, screen_height);	}	private void finishDraw() {		flushGraphics();	}	private 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);	}	private void input() {		int keyStates = getKeyStates();		if ((keyStates & GameCanvas.FIRE_PRESSED) != 0) setGameStatus(Define.STATUS_OPTIONSETTING);	}	private void menuInput() {		int keyStates = getKeyStates();		if ((keyStates & (GameCanvas.RIGHT_PRESSED | GameCanvas.DOWN_PRESSED)) != 0) {			menu_index++;			if (menu_index >= menu_options.length) menu_index = 0;		}		else if ((keyStates & (GameCanvas.LEFT_PRESSED | GameCanvas.UP_PRESSED)) != 0) {			menu_index--;			if (menu_index < 0) menu_index = menu_options.length - 1;		}		else if ((keyStates & GameCanvas.FIRE_PRESSED) != 0) {			switch (menu_index) {				case 3: setGameStatus(Define.STATUS_EXIT); break;			}		}	}	private int getMenuWidth(String[] menuOptions) {		int menuWidth = 0;		for (int i = 0; i < menuOptions.length; i++) {			int menuOptionWidth = Define.HIGHLIGHT_FONT_STYLE.stringWidth(menuOptions[i]);			if (menuOptionWidth > menuWidth) menuWidth = menuOptionWidth;		}		return menuWidth;	}	private void paintMessageRect(Graphics g, int base_x, int base_y, int base_width, int base_height, int borderColor, int borderStyle) {        // 设置边框颜色        g.setColor(borderColor);        // 设置边框风格        g.setStrokeStyle(borderStyle);        // Left        g.drawLine(base_x - 2, base_y + 5, base_x - 2, base_y - 5 + base_height);        // Center-Top        g.drawLine(base_x + 5, base_y - 2, base_x - 5 + base_width, base_y - 2);        // Right        g.drawLine(base_x + base_width + 2, base_y + 5, base_x + 2 + base_width, base_y - 5 + base_height);        // Center-Bottom        g.drawLine(base_x + 5, base_y + 2 + base_height, base_x - 5 + base_width, base_y + 2 + base_height);        // Left-Top Conner        g.drawLine(base_x - 2, base_y + 5, base_x + 5, base_y + 5);        g.drawLine(base_x + 5, base_y - 2, base_x + 5, base_y + 5);        // Right-Top Conner        g.drawLine(base_x - 5 + base_width, base_y - 2, base_x - 5 + base_width, base_y + 5);        g.drawLine(base_x - 5 + base_width, base_y + 5, base_x + 2 + base_width, base_y + 5);        // Right-Bottom Conner        g.drawLine(base_x + 2 + base_width, base_y - 5 + base_height, base_x - 5 + base_width, base_y - 5 + base_height);        g.drawLine(base_x - 5 + base_width, base_y - 5 + base_height, base_x - 5 + base_width, base_y + 2 + base_height);        // Left-Bottom Conner        g.drawLine(base_x - 2, base_y - 5 + base_height,base_x + 5, base_y - 5 + base_height);        g.drawLine(base_x + 5, base_y - 5 + base_height, base_x + 5, base_y + 2 + base_height);    }}

⌨️ 快捷键说明

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