clientcanvas.java

来自「一款模仿劲舞团的手机游戏」· Java 代码 · 共 238 行

JAVA
238
字号


import java.io.IOException;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * @author trojan
 * 
 * 6��ģ��:�ͻ���UI����,��ʾl�ӵĽ��
 */
public class ClientCanvas extends Canvas {

	// -------------------------------�����-----------------------------
	private MyMIDLET midlet;// �������

	private Client client;// �ͻ��˶���

	private MyCanvas iCanvas;// ��Canvas����

	private int width;// ��Ļ�Ŀ�

	private int height;// ��Ļ�ĸ�

	private Timer receiveTimer;// ���ն�ʱ��

	private String sendText;// �������

	private String receiveText;// �������

	private String[] deviceNames;// �������豸��

	private int showIndex = 0;// l�ӽ��

	private Hashtable gameInfo;// ��������Ϸ�����Ϣ

	private Hashtable firstCharacterInfo;// ����������һ�������Ϣ

	private Hashtable secondCharacterInfo;// ������������������Ϣ

	private String info;// ����һ������Ϣ,�ɵ��÷�����4,�����������һ�����Ϣ

	private final int STRING_ANCHOR = Graphics.HCENTER | Graphics.BASELINE;// ����ͼƬê��

	private int count = 0;// l�Ӽ���

	private boolean failed = false;// ��ʶl���Ƿ�ʧ��

	// ------------------------------����ֽ���-----------------------------
	//
	//
	// ------------------------------��������------------------------------------

	public ClientCanvas(MyCanvas iCanvas, String nowUsernameInformation,
			MyMIDLET midlet) {
		// TODO �Զ���ɹ��캯����
		setFullScreenMode(true);

		this.iCanvas = iCanvas;

		width = getWidth();
		height = getHeight();

		this.info = nowUsernameInformation;
		this.midlet = midlet;

		firstCharacterInfo = new Hashtable();
		secondCharacterInfo = new Hashtable();

		// �����ͻ���,l�ӷ�����,����������ͣ�ready��
		client = new Client();
	}

	public void createTimer() {
		// �������ն�ʱ��
		receiveTimer = new Timer();
		receiveTimer.schedule(new TimerReceiveClientTask(), 3000, 500);

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
	}

	// ���ͷ���,��sendText�е���ݷ��͵�������
	public void send() {
		client.setSendText(sendText);
		try {
			client.send();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	// ��ͼ����
	protected void paint(Graphics g) {
		// ���շ�����˵����
		try {
			receiveText = client.receive();
		} catch (IOException e1) {
			// TODO �Զ���� catch ��
			e1.printStackTrace();
		}

		// �������ݲ�Ϊ��
		if (receiveText != null) {
			// ����ݳ���Ϊ����,���ʾ��������յ���ready���ź�,��������Ϸ������,�������,�������Ϸ��Ϣ���������Ϣ
			if (receiveText.length() == 11) {
				showIndex = 1;
				sendText = info.substring(0, 8) + info.substring(9, 10);
				send();

				String name = receiveText.substring(0, 8);
				String app = receiveText.substring(8, 9);
				String musicPath = receiveText.substring(9, 10);
				String scenePath = receiveText.substring(10, 11);

				// �������һ�����Ϣ
				firstCharacterInfo.put("cName", info.substring(0, 8));
				firstCharacterInfo.put("cAppPath", info.substring(9, 10));

				// �������������Ϣ
				secondCharacterInfo.put("cName", name);
				secondCharacterInfo.put("cAppPath", app);

				// �����Ϸ�����Ϣ
				gameInfo = new NetDataStore(musicPath, scenePath).gameInfo();
				gameInfo.put("NetState", "client");
				gameInfo.put("gameMode", "net");
				gameInfo.put("musicVolume", String.valueOf(VolumeRecord
						.searchRecord()));
			}
			// �����Ϊ��ok��,��ʾ�������յ��ͻ��˷��͵�������Ϣ,���ͣ�ok��ȷ��l��
			if (receiveText.equals("ok")) {
				showIndex = 2;
				sendText = "ok";
				send();
			}
			receiveText = null;
		}

		if (count == 30) {
			showIndex = 3;
		}

		// ����l�ӽ��
		Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_LARGE);
		g.setFont(font);
		g.setColor(0xff000000);
		g.fillRect(0, 0, width, height);
		g.setColor(0xffffffff);
		switch (showIndex) {
		case 0:
			g.drawString("����l�ӷ�����....", width / 2, height / 2, STRING_ANCHOR);
			break;
		case 1:
			g.drawString("���ڳ�ʼ����Ϸ....", width / 2, height / 2, STRING_ANCHOR);
			break;
		case 2:
			g.drawString("��Ϸ��ʼ", width / 2, height / 2, STRING_ANCHOR);

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO �Զ���� catch ��
				e.printStackTrace();
			}

			receiveTimer.cancel();

			midlet.startNetCompetition(gameInfo, firstCharacterInfo,
					secondCharacterInfo, null, client);
			break;
		case 3:
			g.drawString("l�ӳ�ʱ", width / 2, height / 2, STRING_ANCHOR);
			g.drawString("����", width / 2 + 55, height / 2 + 90, Graphics.LEFT
					| Graphics.TOP);
			failed = true;
		}
		count++;
	}

	// ��ȡ�豸��
	public String[] getDeviceNames() {
		// ѭ�����ҷ�����,���ر������豸����ַ�����
		while (true) {
			deviceNames = client.getDeviceName();
			if (deviceNames != null) {
				break;
			}

			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO �Զ���� catch ��
				e.printStackTrace();
			}
		}
		return deviceNames;
	}

	// ��Ӧ�����¼�
	protected void keyPressed(int key) {
		if (failed == false) {
			if (key == -7) {
				midlet.getdisplay().setCurrent(iCanvas);
			}
		}
		super.keyPressed(key);
	}

	// ��ѡ�е��豸��ظ�6�?ͻ���
	public void setConnDeviceName(String connDeviceName) {
		client.setConnDeviceName(connDeviceName);
	}

	// ���ѿͻ���
	public void wakeUp() {
		client.wakeUp();
	}

	// ��ʱ��,���ڶ�ʱ�������
	class TimerReceiveClientTask extends TimerTask {
		public void run() {
			repaint();
		}
	}
}

⌨️ 快捷键说明

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