Sunday, December 9, 2007

A Midlet application for GIF Animation

This Java tip demonstrates sample Midlet application for GIF Animation in games. This basic tip may help developers to come out with more animation in their game applications. This makes applications more lively and entertaining.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class GIFDemo extends MIDlet {

private boolean boolMotion=false;
private int iX=10,iY=60;

Display mDisplay;
Thread th;

public void destroyApp(boolean unconditional){}

public void pauseApp() {}

public void startApp() {
mDisplay = Display.getDisplay(this);

final MyCanvas can = new MyCanvas();

mDisplay.setCurrent(can);
}

}

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MyCanvas extends Canvas implements Runnable {
Image img[]=new Image[3];
public MyCanvas() {
try {
img[0]=Image.createImage("/img1.png");
img[1]=Image.createImage("/img2.png");
img[2]=Image.createImage("/img3.png");

}catch(Exception e){}

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

}

//Display GIF image
public void paint(Graphics g) {
g.drawImage(img[imgIndex],0,0,g.TOP|g.LEFT);
}

//Handling keyEvents
protected void keyPressed(int keyCode) {

}

public void run() {
while(true) {
imgIndex++;
imgIndex%=3;
try {
Thread.sleep(500);
}catch(Exception e){}
}
}

}

How to download an image from a web server

You can download an image from a web server using javax.microedition.io.HttpConnection in J2ME.

The folowing methods shows you how to do this. getImageFromUrl(...) method gets an url as a parameter and returns an javax.microedition.lcdui.Image. This is done with the help of getDataFromUrl(...) method.

public static Image getImageFromUrl(String url) {
Image img = null;

try
{
String imageData = getDataFromUrl(url);
img = Image.createImage(imageData.getBytes(), 0,
imageData.length() );
}
catch(Exception e1) {
e1.printStackTrace();
}

return img;
}

public static String getDataFromUrl(String url)
throws IOException {

StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;

long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if( len != -1) {
// Read exactly Content-Length bytes
for(int i =0 ; i <>)
if((ch = is.read()) != -1) {
b.append((char) ch);
}
} else {
//Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}

is.close();
c.close();
return b.toString();
}

How to use mobile as a network server

This Java ME tip explains use of handset (Mobile) as network Server. This tip may be helpful for game developers who are coming up with applications which work on more than one mobile device. This may help them to make one handset controlling the activities of the other.

String getResponse(String request) {
String response="";

try{
// Open a socket connection
//It is trying to connect to a server at port no 8080
SocketConnection soc=Connector.open("socket://my.com:8080");

//Open input and output streams
InputStream is = soc.openInputStream();
OutputStream os = soc.openOutputStream();

//Sending request to server
os.write(request.getBytes());

//Now read response from server one byte at a time
int ch = 0;
while(ch != -1) {
ch = is.read();
response+=(char)ch;

}

//Close connection
is.close();
os.close();
soc.close();
}catch(Exception e){}

return response;
}


void startServer() {

try {

// Open a server socket connection
// It is trying start a server at port no 8080

ServerSocketConnection serv =
(ServerSocketConnection)Connector.open("socket://:8080");

while(true) {

// Now server is passive open:waiting
SocketConnection soc = (SocketConnection) scn.acceptAndOpen();

//Open input and output streams
InputStream is = soc.openInputStream();
OutputStream os = soc.openOutputStream();

//Reading request from client
int ch = 0;
while(ch != -1) {

ch = is.read();
request+=(char)ch;
}
//Send request to server

os.write("Hello from JJKK".getBytes());


//Close connection
is.close();
os.close();
soc.close();
}

}catch(Exception e){}

}

Sending data to Servlet and getting the response

Servlets use HTTP request-reqponse protocol to send/recieve data from server. HTTPConnection interface of javax.microedition.io package is used.

Example below send any text to servlet and get response code from servlet.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;

public class MidletServlet extends MIDlet implements CommandListener {
Display display = null;
Form form = null;
TextField tb = null;
String str = null;
String url = "http://localhost:8080/servlets-examples/servlet/getText";
Command backCommand = new Command("Back", Command.BACK, 0);
Command submitCommand = new Command("Submit", Command.OK, 2);
Command exitCommand = new Command("Exit", Command.STOP, 3);
private Test test;

public MidletServlet() {}

public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
form = new Form("Request Servlet");
tb = new TextField("Please input text: ","",30,TextField.ANY );
form.append(tb);
form.addCommand(submitCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == backCommand) {
display.setCurrent(form);
} else if (c == submitCommand) {
str = tb.getString();
test = new Test(this);
test.start();
test.getServlet(str);
}
}


class Test implements Runnable {
MidletServlet midlet;
private Display display;
String text;

public Test(MidletServlet midlet) {
this.midlet = midlet;
display = Display.getDisplay(midlet);
}

public void start() {
Thread t = new Thread(this);
t.start();
}

public void run() {
StringBuffer sb = new StringBuffer();
try {
HttpConnection c = (HttpConnection) Connector.open(url);
c.setRequestProperty(
"User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");

c.setRequestProperty("Content-Language","en-US");
c.setRequestMethod(HttpConnection.POST);

DataOutputStream os =
(DataOutputStream)c.openDataOutputStream();

os.writeUTF(text.trim());
os.flush();
os.close();

// Get the response from the servlet page.
DataInputStream is =(DataInputStream)c.openDataInputStream();
//is = c.openInputStream();
int ch;
sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char)ch);
}
showAlert(sb.toString());
is.close();
c.close();
} catch (Exception e) {
showAlert(e.getMessage());
}
}
/* This method takes input from user like text and pass
to servlet */
public void getServlet(String text) {
this.text = text;
}

/* Display Error On screen*/
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
};
}
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class getText extends HttpServlet {

public void init() {
}

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

DataInputStream in =
new DataInputStream((InputStream)request.getInputStream());

String text = in.readUTF();
String message;
try {
message = "100 ok";
} catch (Throwable t) {
message = "200 " + t.toString();
}
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
in.close();
out.close();
out.flush();
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

doPost(request,response);
}
}

How to use GET or POST to communicate with a Java servlet

This example shows how to use GET or POST to communicate with a Java servlet.

/*--------------------------------------------------
* GetNpost.java
*
* Use GET or POST to communicate with a Java servlet.
* The servlet will search a database for the balance
* of an account.
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class GetNpost extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object
private Form fmMain; // The main form
private Alert alError; // Alert to error message
private Command cmGET; // Request method GET
private Command cmPOST; // Request method Post
private Command cmExit; // Command to exit the MIDlet
private TextField tfAcct; // Get account number
private TextField tfPwd; // Get password
private StringItem siBalance;// Show account balance
private String errorMsg = null;
public GetNpost()
{
display = Display.getDisplay(this);

// Create commands
cmGET = new Command("GET", Command.SCREEN, 2);
cmPOST = new Command("POST", Command.SCREEN, 3);
cmExit = new Command("Exit", Command.EXIT, 1);

// Textfields
tfAcct = new TextField("Account:", "", 5, TextField.NUMERIC);
tfPwd = new TextField("Password:", "", 10, TextField.ANY | TextField.PASSWORD);

// Balance string item
siBalance = new StringItem("Balance: $", "");

// Create Form, add commands & componenets, listen for events
fmMain = new Form("Account Information");
fmMain.addCommand(cmExit);
fmMain.addCommand(cmGET);
fmMain.addCommand(cmPOST);
fmMain.append(tfAcct);
fmMain.append(tfPwd);
fmMain.append(siBalance);
fmMain.setCommandListener(this);
}

public void startApp()
{
display.setCurrent(fmMain);
}

public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }

public void commandAction(Command c, Displayable s)
{
if (c == cmGET || c == cmPOST)
{
try
{
if (c == cmGET)
lookupBalance_withGET();
else
lookupBalance_withPOST();
}
catch (Exception e)
{
System.err.println("Msg: " + e.toString());
}
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}

/*--------------------------------------------------
* Access servlet using GET
*-------------------------------------------------*/
private void lookupBalance_withGET() throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;
boolean ret = false;

// Data is passed at the end of url for GET
String url = "http://www.mycgiserver.com/servlet/corej2me.GetNpostServlet" + "?" +
"account=" + tfAcct.getString() + "&" +
"password=" + tfPwd.getString();
try
{
http = (HttpConnection) Connector.open(url);
//----------------
// Client Request
//----------------
// 1) Send request method
http.setRequestMethod(HttpConnection.GET);
// 2) Send header information - none
// 3) Send body/data - data is at the end of URL

//----------------
// Server Response
//----------------
iStrm = http.openInputStream();
// Three steps are processed in this method call
ret = processServerResponse(http, iStrm);
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (http != null)
http.close();
}

// Process request failed, show alert
if (ret == false)
showAlert(errorMsg);
}

/*--------------------------------------------------
* Access servlet using POST
*-------------------------------------------------*/
private void lookupBalance_withPOST() throws IOException
{
HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
boolean ret = false;
// Data is passed as a separate stream for POST (below)
String url = "http://www.mycgiserver.com/servlet/corej2me.GetNpostServlet";
try
{
http = (HttpConnection) Connector.open(url);
oStrm = http.openOutputStream();
//----------------
// Client Request
//----------------
// 1) Send request type
http.setRequestMethod(HttpConnection.POST);
// 2) Send header information. Required for POST to work!
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// If you experience connection/IO problems, try
// removing the comment from the following line
// http.setRequestProperty("Connection", "close");

// 3) Send data/body
// Write account number
byte data[] = ("account=" + tfAcct.getString()).getBytes();
oStrm.write(data);
// Write password
data = ("&password=" + tfPwd.getString()).getBytes();
oStrm.write(data);
// For 1.0.3 remove flush command
// See the note at the bottom of this file
// oStrm.flush();

//----------------
// Server Response
//----------------
iStrm = http.openInputStream();
// Three steps are processed in this method call
ret = processServerResponse(http, iStrm);
}
finally
{
// Clean up
if (iStrm != null)
iStrm.close();
if (oStrm != null)
oStrm.close();
if (http != null)
http.close();
}

// Process request failed, show alert
if (ret == false)
showAlert(errorMsg);
}

/*--------------------------------------------------
* Process a response from a server
*-------------------------------------------------*/
private boolean processServerResponse(HttpConnection http, InputStream iStrm) throws IOException
{
//Reset error message
errorMsg = null;
// 1) Get status Line
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
// 2) Get header information - none
// 3) Get body (data)
int length = (int) http.getLength();
String str;
if (length != -1)
{
byte servletData[] = new byte[length];
iStrm.read(servletData);
str = new String(servletData);
}
else // Length not available...
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);

str = new String(bStrm.toByteArray());
bStrm.close();
}
// Update the string item on the display
siBalance.setText(str);
return true;
}
else
// Use message from the servlet
errorMsg = new String( http.getResponseMessage());

return false;
}

/*--------------------------------------------------
* Show an Alert
*-------------------------------------------------*/
private void showAlert(String msg)
{
// Create Alert, use message returned from servlet
alError = new Alert("Error", msg, null, AlertType.ERROR);

// Set Alert to type Modal
alError.setTimeout(Alert.FOREVER);

// Display the Alert. Once dismissed, display the form
display.setCurrent(alError, fmMain);
}
}

/*
The call to flush() uses a feature of HTTP 1.1 that allows data to be
sent in smaller loads. When calling flush() or sending a large
amount of data (in version 1.0.3) chunked encoding is used.

If you are using an HTTP 1.0 server or proxy server the chunked
transfer may cause problems.

You can avoid the chunking behavior with small transactions by just
calling close() where you were using flush(). For larger transactions
you need to buffer your output so a single write() and close() are
issued to the output stream.
*/


/*--------------------------------------------------
* GetNpostServlet.java
*
* Show how GET and POST from client can access and
* process the same data.
* Account information is maintained in a database
* (connecting with jdbc)
*
* Table: acctInfo
* Columns:
* account - integer
* password - varchar
* balance - integer
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
//package corej2me; // Required for mycgiserver.com

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class GetNpostServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Same code appears in doPost()
// Shown both places to emphasize that data is received thru
// different means (environment variable vs stream),
// yet processed the same inside the servlet
String acct = req.getParameter("account"),
pwd = req.getParameter("password");

String balance = accountLookup(acct, pwd);

if (balance == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to locate account.");
return;
}

res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Same code appears in doGet()
// Shown both places to emphasize that data is received thru
// different means (stream vs environment variable),
// yet processed the same inside the servlet
String acct = req.getParameter("account"),
pwd = req.getParameter("password");

String balance = accountLookup(acct, pwd);

if (balance == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to locate account.");
return;
}
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}

/*--------------------------------------------------
* Lookup bank account balance in database
*-------------------------------------------------*/
private String accountLookup(String acct, String pwd)
{
Connection con = null;
Statement st = null;
StringBuffer msgb = new StringBuffer("");

try
{
// These will vary depending on your server/database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:acctInfo");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select balance from acctInfo where account = " +
acct + "and password = '" + pwd + "'");
if (rs.next())
return rs.getString(1);
else
return null;
}
catch (Exception e)
{
return e.toString();
}
}
/*--------------------------------------------------
* Information about servlet
*-------------------------------------------------*/
public String getServletInfo()
{
return "GetNpostServlet 1.0 - John W. Muchow - www.corej2me.com";
}
}