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

📄 bluemidlet.java

📁 欢迎使用蓝牙联网坦克大战
💻 JAVA
字号:
/*
 * Bluetooth Multiplayer Games Framework
 * Author: Francesco Panciroli (email fif0302@iperbole.bologna.it)
 * Copyright (C) 2006  Francesco Panciroli
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package newpackage;

import java.util.Vector;

import javax.bluetooth.UUID;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

import log4j2me.util.FormAppender;

import org.apache.log4j.Category;

// Note: could be an abstract class, but then you need a concrete class for testing.
public class BlueMIDlet
//public abstract class BlueMIDlet
    extends MIDlet
    implements CommandListener, BlueEventHandler
{
	/** Constants for the choice of the game mode */
	public static final int STAND_ALONE_MODE = 0;
	public static final int SERVER_MODE = 1;
	public static final int CLIENT_MODE = 2;
	
	static Category log = Category.getInstance("panci.bluemgf.BlueMIDlet");

    protected Form logForm;
    protected Command logBackCommand;
    protected Displayable logBackDisplayable;

    protected BlueConfig configForm;

    /** millisec to wait before calling a connection.receive() */
    protected int waitBeforeReceiveData = 0;
    
    /** connection protocol to use (SPP o L2CAP) */
    protected int connectionProtocol = BlueConfig.PROTOCOL_L2CAP;

    /** Describes this service */
    protected UUID serviceUUID = new UUID("739f79a68d64c1093b820e674510b90a", false);

    /** lifetime in millisec of any alert dialog */
    static final int ALERT_TIMEOUT = 2000;

    /** Soft button for exiting. */
    private final Command EXIT_CMD = new Command("Exit", Command.EXIT, 2);

    /** Soft button for launching a client or sever. */
    private final Command OK_CMD = new Command("Ok", Command.OK, 1);

    /** A list of menu items */
    private static final String[] elements = { "Stand Alone", "Server", "Client", "Configuration" };

    /** A menu list instance */
    private final List menu = new List("Choose game mode", List.IMPLICIT,
            elements, null);

    /** The GUI part of the server. */
    private BlueServerGUI blueServerGUI;

    /** The GUI part of the client. */
    private BlueClientGUI blueClientGUI;

    /** The connections established */
    protected Vector connections;
    
    /** number of players, including the server: 0 = any number */
    protected int numberOfPlayers = 0;
    
    /** game mode chosen: stand-alone, server, client */
    protected int gameMode;

    public BlueMIDlet() {
    	initLog();
    	init();
    }

    private synchronized void init() {
    	connections = new Vector();
    	
    	configForm = new BlueConfig("Configuration", this);
    	
    	menu.addCommand(EXIT_CMD);
        menu.addCommand(OK_CMD);
        menu.setCommandListener(this);
    }

    private synchronized void initLog() {
		// Initialization of the form containing the logs
		Vector vFormAppenders=log4j2me.util.AppPropertyConfigurator.config(this);		
		//Look for the first FormAppender according form appender name which is set in JAD file or properties.
		for(int i=0; vFormAppenders.size()>i;i++){
			String title=((FormAppender)vFormAppenders.elementAt(i)).getTitle();
			if((null!=title)&&(title.equals("Log"))){
				logForm = ((FormAppender)vFormAppenders.elementAt(i)).getForm();
				break;
			}
		}
		log.debug("logForm = " + logForm);
		if (logForm != null) {
			logBackCommand = new Command("Back", Command.BACK, 1);
			logForm.addCommand(logBackCommand);
			logForm.setCommandListener(this);
		}
    }

    public void startApp() {
        Display.getDisplay(this).setCurrent(menu);
    }

    public void pauseApp() {
    }


    public void destroyApp(boolean unconditional) {
    }

    public void quit() {
        destroyApp(false);
        notifyDestroyed();
    }

	public int getNumberOfPlayers() {
		return numberOfPlayers;
	}

	public void setNumberOfPlayers(int numberOfPlayers) {
		this.numberOfPlayers = numberOfPlayers;
	}

	/**
     * Responds to commands issued on "client or server" form.
     *
     * @param c command object source of action
     * @param d screen object containing actioned item
     */
    public void commandAction(Command c, Displayable d) {
    	//log.debug("Called BlueMIDlet.commandAction with parameters " + c + ", " + d);
    	//log.debug("Command description: " + c.getLabel() + " - " + c.getCommandType());
    	//log.debug("menu.getSelectedIndex() = " + menu.getSelectedIndex());
        if (c == EXIT_CMD) {
            destroyApp(true);
            notifyDestroyed();
            return;
        } else if (c == logBackCommand) {
    		Display.getDisplay(this).setCurrent(logBackDisplayable);
        } else {
    		log.debug("waitBeforeReceiveData = " + waitBeforeReceiveData);
    		log.debug("connectionProtocol = " + connectionProtocol);
	        switch (menu.getSelectedIndex()) {
	        case 0:
	        	handleEvent(BlueEventHandler.EVENT_STANDALONE, null);
	            break;
	        case 1:
	        	if (blueServerGUI == null)
	        		blueServerGUI = new BlueServerGUI(this);
	       		showBlueServerGUI();
	            break;
	        case 2:
	        	if (blueClientGUI == null)
	            	blueClientGUI = new BlueClientGUI(this);
	       		showBlueClientGUI();
	            break;
	        case 3:
	    		Display.getDisplay(this).setCurrent(configForm);
	            break;
	        default:
	            log.error("commandAction: Unexpected choice...");
	            break;
	        }
        }
    }

	private void showBlueServerGUI() {
		Display.getDisplay(this).setCurrent(blueServerGUI);
	}

	private void showBlueClientGUI() {
		Display.getDisplay(this).setCurrent(blueClientGUI);
	}

	public UUID getServiceUUID() {
		return serviceUUID;
	}


	public void setServiceUUID(UUID serviceUUID) {
		this.serviceUUID = serviceUUID;
	}

	public void setServiceUUIDAsString(String serviceUUIDString) {
		this.serviceUUID = new UUID(serviceUUIDString, false);
	}

	public void backToMenu() {
		waitBeforeReceiveData = configForm.getWaitBeforeReceiveData();
		connectionProtocol = configForm.getConnectionProtocol();
		Display.getDisplay(this).setCurrent(menu);
	}
	
	public void completeServerInitialization(boolean isBTReady) {
		if (isBTReady) {
			// nothing to do here
		} else {
			// something wrong
			Alert al = new Alert("Error", "Can't inititialize bluetooth", null,
					AlertType.ERROR);
			al.setTimeout(ALERT_TIMEOUT);
			Display.getDisplay(this).setCurrent(al, menu);
		}
	}
	
	//Note: handleEvent could be abstract, but then you need a concrete class for testing.
	//public abstract void handleEvent(String event, Object param1);
	public void handleEvent(String event, Object param1){
		log.debug("Called handleEvent with parameters " + event + ", " + param1);
	}

	public int getGameMode() {
		return gameMode;
	}

	public void setGameMode(int modalitaGioco) {
		this.gameMode = modalitaGioco;
	}

	public boolean isLogAvailable() {
		return (logForm != null);
	}
	
	public int getWaitBeforeReceiveData() {
		return waitBeforeReceiveData;
	}
	
	public int getConnectionProtocol() {
		return connectionProtocol;
	}
	
	/**
	 * Show the form with logs.
	 * 
	 * @param logBackDisplayable the Displayable to show when the BACK button is pressed
	 */
	public void showLog(Displayable logBackDisplayable) {
		//log.debug("showLog: logForm = " + logForm);
		if (isLogAvailable()) {
			this.logBackDisplayable = logBackDisplayable;
			Display.getDisplay(this).setCurrent(logForm);
		}
	}
    
	/**
	 * Stop the server.
	 *
	 */
	public void stopBlueServer() {
		blueServerGUI.destroy();
	}
}

⌨️ 快捷键说明

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