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){}

}