首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

软件资讯设计 工具 系统 开发 安全 办公 陶吧 IT教育 Vista频道 | 下载中心酷我音乐盒 腾讯QQ
天极网 > 开发频道>J2ME网络编程以及网络游戏的实现

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

2006-09-18 15:03作者:蒋涛出处:BLOG责任编辑:方舟

  二、从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) {}
}
}

关注此文的读者还看过:

返回开发频道首页

共2页。 上一页12

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

软件下载

热门
推荐

网友关注

软件
资料
游戏

装机推荐

文章排行

本周
本月
最新更新
天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络