📄 slugsmidlet.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: SlugsMIDlet.java,v 1.1 2003/06/03 00:55:31 zull Exp $ */package com.sun.j2me.blueprints.slugs.client;import com.sun.j2me.blueprints.slugs.shared.*;import com.sun.j2me.blueprints.slugs.shared.Item;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*;import javax.microedition.io.*;/** * This class implements a generic multiplayer * client harness. The class of the actual * visualization component, as well as the * IP address for the server are read from * the midlet's descriptor file. * * <p> * The error handling in this class is very * rudimentary. For a production quality game, * an error dialog needs to be presented to * the user when things go wrong. */public class SlugsMIDlet extends MIDlet implements Runnable, EventClient, CommandListener { private static final String SCREEN_CLASS = "ScreenClass"; private static final String SERVER_IP = "ServerIP"; private Command exit; private Screen screen; private DataInputStream in; private DataOutputStream out; private StreamConnection conn; private int frame_no; private Frame frame; private boolean done; /** * Start the application */ public void startApp() { int player_id; player_id = 0; exit = new Command("Exit", Command.EXIT, 1); try { conn = (StreamConnection) Connector.open("socket://" + getAppProperty(SERVER_IP) + ":" + Server.PORT); in = new DataInputStream(conn.openInputStream()); out = new DataOutputStream(conn.openOutputStream()); } catch (IOException e) { closeConnection(); System.out.println("*** could not connect to server: " + e); destroyApp(true); } frame = new Frame(null, 0, 0); frame_no = -1; try { Message msg = new Message(Message.ALL_FRAMES, Message.SIGNUP); msg.archive(out); System.out.println("sent sign-up msg"); msg = Message.createFromStream(in); if (msg.getType() != Message.SIGNUP_ACK) { System.out.println("*** could not sign up: " + msg); destroyApp(true); } player_id = msg.getBody()[0]; System.out.println("received sign-up ack, id = " + player_id); } catch (IOException e) { System.out.println("*** could not sign up with server: " + e); destroyApp(true); } try { screen = (Screen) Class.forName(getAppProperty(SCREEN_CLASS)).newInstance(); } catch (Exception e) { System.out.println("*** could not create screen: " + e); destroyApp(true); } screen.initialize(this, player_id); screen.setCommandListener(this); screen.addCommand(exit); done = false; Thread thread = new Thread(this); thread.start(); Display.getDisplay(this).setCurrent(screen); } /** * Pause the application */ public void pauseApp() {} private void closeConnection() { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (conn != null) { conn.close(); } } catch (IOException e) { System.out.println(e); } } /** * Destroy the application */ public void destroyApp(boolean unc) { System.out.println("MidpTestClient.destroyApp()"); Message msg = new Message(Message.ALL_FRAMES, Message.SIGNOFF); try { msg.archive(out); } catch (IOException e) { /* ignore */ } closeConnection(); Display.getDisplay(this).setCurrent(null); } /** * Handle soft button events */ public void commandAction(Command cmd, Displayable s) { if (cmd == exit) { done = true; destroyApp(true); notifyDestroyed(); } } /** * Handle status events from the visualization component */ public void handleEvent(int id, int data) { Message msg; synchronized (this) { msg = new Message(frame_no, id, data); } try { msg.archive(out); } catch (IOException e) { System.out.println("*** could not send message: " + msg); } } private void handleStatus(Message msg) { Item item; synchronized (this) { int id = msg.getFrameNumber(); if (id != frame_no + 1) { System.out.println("+++ message id sequence broken:"); System.out.println("+++ expected " + (frame_no + 1)); System.out.println("+++ got " + id); System.out.println(); } frame.initFromIntegerArray(msg.getBody()); frame_no = id; } for (int i = 0; i < frame.getItemCount(); i++) { item = frame.getItemAt(i); screen.update(item.getStatus(), item.getX(), item.getY()); } screen.repaint(); } private void handleError() { Message msg = new Message(frame_no, Message.SIGNOFF); try { msg.archive(out); } catch (IOException e) { /* ignore */ } } private void handleUnknown(Message msg) { System.out.println("received unknown message: " + msg); } /** * Main loop */ public void run() { Message msg; while (!done) { try { msg = Message.createFromStream(in); } catch (IOException e) { System.out.println("cant read message from stream"); continue; } switch (msg.getType()) { case Message.SERVER_STATUS: handleStatus(msg); break; case Message.ERROR: handleError(); break; default: handleUnknown(msg); break; } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -