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

📄 progressthread.java

📁 经典FC游戏《超惑星战记》的J2ME版本!!功能基本上都实现了
💻 JAVA
字号:
/**
 * <p>Title: Transpanzer</p>
 * <p>Description:
 * You cannot remove this copyright and notice.
 * You cannot use this file any part without the express permission of the author.
 * All Rights Reserved</p>
 * @author Jjyo
 * @email jjyo@163.com
 * @version 1.0.0
 */

import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class ProgressThread extends Canvas implements Runnable {
	public boolean loadProgressIsOver = false;
	private volatile Thread progressThread = null;
	private int width;
	private int height;
	private long startTime;
	private long endTime;
	private Image imSchool;
	private static final int FRAME_TIME = 200; // 为了方便资源载入的线程得到较多的执行时间,第一帧的时间为200毫秒
	public int gaugeCnt;
	private int progressInterval = 1;
	private static final int PROGRESS_END = 150; // 线程的预计执行时间为150帧
	private MIDlet midlet;

	public ProgressThread(MIDlet midlet) {
		setFullScreenMode(true);	//全屏
		loadProgressIsOver = false;	//
		this.midlet = midlet; // 主midlet的引用
		this.width = this.getWidth(); // 获得屏幕的宽度
		this.height = this.getHeight(); // 获得屏幕的高度
		try {
			imSchool=Image.createImage("/logo.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void paint(Graphics g) {
		drawGaugeScreen(g);
	}
	private void drawGaugeScreen(Graphics g) {
		g.setColor(255,255,255);
		g.fillRect(0, 0, width, height);
		g.drawImage(imSchool,(width-imSchool.getWidth())/2,(height-imSchool.getHeight())/2,0);
		g.fillArc((width-imSchool.getWidth()-1)/2, (height-imSchool.getHeight()-1)/2, imSchool.getWidth()+1, imSchool.getHeight()+1, 
				-90, -((180-270*gaugeCnt/PROGRESS_END)>0?180-270*gaugeCnt/PROGRESS_END:0));
		g.fillArc((width-imSchool.getWidth()-1)/2, (height-imSchool.getHeight()-1)/2, imSchool.getHeight()+1, imSchool.getHeight()+1, 
				270, (180-270*gaugeCnt/PROGRESS_END)>0?180-270*gaugeCnt/PROGRESS_END:0);
	}

	public void run() {
		try {
			// 得到当前的线程,以此来区分是否主线程
			Thread currentThread = Thread.currentThread(); 
			while (currentThread == progressThread) {
				// 线程循环开始的时间
				startTime = System.currentTimeMillis(); 
				// 重绘屏幕
				repaint(0, 0, width, height); 
				// 提交绘制
				serviceRepaints(); 
				// 增加进度条前进的间隔
				gaugeCnt += progressInterval; 
				if (((GetCanvas) midlet).getCanvas() != null) {
					// 如果查询构造成功,则进度条的前时间隔加大,结果使速度加快
					progressInterval += 1; 
				}
				if (gaugeCnt >= PROGRESS_END) // 结果到达了进度条的结尾
				{
					if (((GetCanvas) midlet).getCanvas() == null) {
						gaugeCnt -= progressInterval;
					} else {
						// 构造成功转换canvas,切换到主程序的canvas
						loadProgressIsOver = true; 
						Display.getDisplay(midlet).setCurrent(((GetCanvas) midlet).getCanvas());
						// 开始游戏的主循环
						((GameStart) ((GetCanvas) midlet).getCanvas()).start(); 
						break;
					}
				}
				endTime = System.currentTimeMillis();
				if ((endTime - startTime) < FRAME_TIME) {
					Thread.sleep(FRAME_TIME - (endTime - startTime));
				}
			}
		} catch (InterruptedException ie) {
			System.out.println(ie.toString());
		}
	}

	public void start() {
		progressThread = new Thread(this);
		progressThread.setPriority(Thread.MIN_PRIORITY + 1);
		progressThread.start();
	}

	public void stop() {
		progressThread = null;
	}
}

⌨️ 快捷键说明

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