您的位置: 莱芜资讯网 > 娱乐

J2ME网络编程以及网络游戏的实现

发布时间:2019-09-13 19:08:45

二、从Web获取文字信息

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import java.io.*;

import javax.microedition.lcdui.*;

public class getHttp

extends MIDlet {

public void startApp() {

try {

//打开网络连接

String url = "http://127.0.0.1/515game/myweb.html";

StreamConnection sc = (StreamConnection) Connector.open(url);

//读取数据

InputStream is = sc.openInputStream();

int tmp = 0;

String get = "";

while (tmp != -1) { //-1代表结束

tmp = is.read();

get = get + (char) tmp;

}

is.close();

Form f = new Form(url);

//解决中文问题

String chinese = new String(get.getBytes("iso8859-1"), "utf-8");

f.append(chinese);

Display.getDisplay(this).setCurrent(f);

//关闭网络连接

sc.close();

}

catch (Exception e) {}

}

public void pauseApp() {}

public void destroyApp(boolean f) {}

}

三、从Web获取图片信息

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import java.io.*;

public class testPic

extends MIDlet {

public void startApp() {

try {

//打开网络连接

String url = "http://127.0.0.1/515game/back0.png";

StreamConnection sc = (StreamConnection) Connector.open(url);

//获取图片

InputStream is = sc.openInputStream();

Image im = Image.createImage(is);//该方法为MIDP 2.0方法

Form f = new Form(url);

f.append(im);

Display.getDisplay(this).setCurrent(f);

//关闭连接

sc.close();

}

catch (Exception e) {}

}

public void pauseApp() {}

public void destroyApp(boolean f) {}

}

四、从Web获取多媒体信息

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

import javax.microedition.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同服务器端交换数据。

这是一个网络版商品竞猜的实例,客户端输入商品价格,在服务器端负责游戏逻辑的处理。

服务器端代码:

Guess.jsp 这个文件放到d:/ mygameWeb目录下面

<%

String sjg=request.getParameter("jg");

int jg=(int)(Math.random()*10);

int userjg=Integer.parseInt(sjg);

if(userjg>jg)

{

out.println("sorry da le");

}

if(userjg<jg)

{

out.println("sorry xiao le");

}

if(userjg==jg)

{

out.println("right");

}

%>

J2me客户端代码:

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import java.io.*;

public class GuessGame

extends MIDlet

implements CommandListener, Runnable {

public Form f, f2;

public TextField tf1;

public Display d;

public Command c1, c2;

public GuessGame() {

f = new Form("商品竞猜");

f2 = new Form("竞猜结果");

c2 = new Command("返回", Command.SCREEN, 1);

f2.addCommand(c2);

f2.setCommandListener(this);

tf1 = new TextField("请输入商品价格1-9", "", 1, TextField.NUMERIC);

f.append(tf1);

c1 = new Command("确定", Command.SCREEN, 1);

f.addCommand(c1);

f.setCommandListener(this);

d = Display.getDisplay(this);

}

public void startApp() {

d.setCurrent(f);

}

public void pauseApp() {}

public void destroyApp(boolean f) {}

public void commandAction(Command c, Displayable d) {

if (c == c1) { //启动网络请求

(new Thread(this)).start();

}

if (c == c2) {

this.d.setCurrent(f);

}

}

public void run() {

try {

//打开网络连接

String url = "http://127.0.0.1/515game/Guess.jsp?jg="+ tf1.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);

}

for (int i = 0; i < f2.size(); i++) {

f2.delete(i);

}

f2.append(get);

d.setCurrent(f2);

//关闭网络连接

sc.close();

}

catch (Exception e) {}

}

}查看本文来源

二个月婴儿感冒症状
血栓如何治疗
经常拉肚子要怎么调理
小孩发烧如何退烧快
猜你会喜欢的
猜你会喜欢的