dition.io.*;
import java.io.*;
import javax.microedition.media.*;
public class getMusic
extends MIDlet {
public void startApp() {
try {
//打开网络连接
String url = "http://127.0.0.1/515game/kk.wav";
StreamConnection sc = (StreamConnection) Connector.open(url);
//获取声音
InputStream is = sc.openInputStream();
Player p1 = Manager.createPlayer(is, "audio/x-wav");
p1.start();
//关闭网络连接
sc.close();
System.out.println("sound is play");
}
catch (Exception e) {
e.printStackTrace();
}
}
public void pauseApp() {}
public void destroyApp(boolean f) {}
}
五、基于http的用户登陆系统实现
服务器端程序
checkuser.jsp 这个文件放到d:/ mygameWeb目录下面
<%
//得到用户名
String name=request.getParameter("username");
//得到密码
String pass=request.getParameter("password");
if(name.equals("ldh"))
{
if(pass.equals("zhm"))
{
out.print("welcome ");
}
else
{
out.print("pass word error");
}
}
else
{
out.print("user name error");
}
%>
客户端程序
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class logoIN
extends MIDlet
implements CommandListener, Runnable {
public Form f;
public TextField user; //用户名
public TextField pass; //密码
public Command c1;
public logoIN() {
f = new Form("传奇世界用户登陆");
user = new TextField("用户名", "", 10, TextField.ANY);
pass = new TextField("密码", "", 8, TextField.PASSWORD);
f.append(user);
f.append(pass);
c1 = new Command("确定", Command.SCREEN, 1);
f.addCommand(c1);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}
public void startApp() {}
public void pauseApp() {}
public void destroyApp(boolean f) {}
public void commandAction(Command c, Displayable dd) {
Thread t = new Thread(this);
t.start(); //启动线程连结网络
}
//完成网络请求
public void run() {
try {
//打开网络
String url = "http://127.0.0.1/515game/checkuser.jsp?username=" +
user.getString() + "&password=" + pass.getString();
//获取数据
StreamConnection sc = (StreamConnection) Connector.open(url);
InputStream is = sc.openInputStream();
int tmp = 0;
String get = "";
while ( (tmp = is.read()) != -1) {
get = get + (char) tmp;
}
Form f2 = new Form("登陆结果");
f2.append(get);
Display.getDisplay(this).setCurrent(f2);
//关闭网络
sc.close();
}
catch (Exception e) {}
}
}
J2ME网络编程以及网络游戏的实现(8)
时间:2010-05-13
六、一个网络游戏实例
下面我们通过一个网络猜价格的游戏实例来说明如何通过J2me同服务器端交换数据。
这是一个网络版商品竞猜的实 |