Showing posts with label j2me source code. Show all posts
Showing posts with label j2me source code. Show all posts

Splitting String/Text In J2ME

To split string/text in J2ME you can use these methods.
Include one of these method in your applications

public static String[] split (String a,String delimeter){
String c[]=new String[0];
String b=a;
while (true){
int i=b.indexOf(delimeter);
String d=b;
if (i>=0)
d=b.substring(0,i);
String e[]=new String[c.length+1];
for (int k=0;k
e[k]=c[k];
e[e.length-1]=d;
c=e;
b=b.substring(i+delimeter.length(),b.length());
if (b.length()<=0 || i<0 )
break;
} return c;
}
-----
/**
* A method for splitting a string in J2ME.
*
* @param splitStr The string to split.
* @param delimiter The characters to use as delimiters.
* @return An array of strings.
*/
public static String[] Split(String splitStr, String delimiter) {

StringBuffer token = new StringBuffer();
Vector tokens = new Vector();

// split
char[] chars = splitStr.toCharArray();
for (int i=0; i < chars.length; i++) {
if (delimiter.indexOf(chars[i]) != -1) {
// we bumbed into a delimiter
if (token.length() > 0) {
tokens.addElement(token.toString());
token.setLength(0);
}
} else {
token.append(chars[i]);
}
}
// don't forget the "tail"...
if (token.length() > 0) {
tokens.addElement(token.toString());
}

// convert the vector into an array
String[] splitArray = new String[tokens.size()];
for (int i=0; i < splitArray.length; i++) {
splitArray[i] = tokens.elementAt(i);
}
return splitArray;
}

J2ME SMS : Receiving SMS in J2ME

/*
J2ME SMS
Source code example for receiving SMS in J2ME
*
* @(#)SMSReceive.java 1.11 04/03/22
*
* Copyright (c) 1999-2004 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/

package example.sms;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

/**
* An example MIDlet displays text from an SMS MessageConnection
*/
public class SMSReceive extends MIDlet
implements CommandListener, Runnable, MessageListener {

/** user interface command for indicating Exit request. */
Command exitCommand = new Command("Exit", Command.EXIT, 2);
/** user interface command for indicating Reply request */
Command replyCommand = new Command("Reply", Command.OK, 1);
/** user interface text box for the contents of the fetched URL. */
Alert content;
/** current display. */
Display display;
/** instance of a thread for asynchronous networking and user interface. */
Thread thread;
/** Connections detected at start up. */
String[] connections;
/** Flag to signal end of processing. */
boolean done;
/** The port on which we listen for SMS messages */
String smsPort;
/** SMS message connection for inbound text messages. */
MessageConnection smsconn = null;
/** Current message read from the network. */
Message msg;
/** Address of the message's sender */
String senderAddress;
/** Alert that is displayed when replying */
Alert sendingMessageAlert;
/** Prompts for and sends the text reply */
SMSSender sender;
/** The screen to display when we return from being paused */
Displayable resumeScreen;

/**
* Initialize the MIDlet with the current display object and
* graphical components.
*/
public SMSReceive() {
smsPort = getAppProperty("SMS-Port");

display = Display.getDisplay(this);

content = new Alert("SMS Receive");
content.setTimeout(Alert.FOREVER);
content.addCommand(exitCommand);
content.setCommandListener(this);
content.setString("Receiving...");

sendingMessageAlert = new Alert("SMS", null, null, AlertType.INFO);
sendingMessageAlert.setTimeout(5000);
sendingMessageAlert.setCommandListener(this);

sender = new SMSSender(smsPort, display, content, sendingMessageAlert);

resumeScreen = content;
}

/**
* Start creates the thread to do the MessageConnection receive
* text.
* It should return immediately to keep the dispatcher
* from hanging.
*/
public void startApp() {
// SMS connection to be read.
String smsConnection = "sms://:" + smsPort;
content.setString(smsConnection);

// Open the message connection.

// if (smsconn == null) {
try {
smsconn = (MessageConnection)Connector.open(smsConnection, Connector.READ);
// smsconn.setMessageListener(this);
} catch (Throwable t) {
content.setString(t.toString());
}
// } catch (IOException ioe) {
// content.setString(ioe.toString());
// ioe.printStackTrace();
// }
// }
/*
// Initialize the text if we were started manually.
connections = PushRegistry.listConnections(true);
if (connections == null || connections.length == 0) {
content.setString("Waiting for SMS on port " + smsPort + "...");
}
done = false;
thread = new Thread(this);
thread.start();
*/
display.setCurrent(resumeScreen);
}

/**
* Notification that a message arrived.
* @param conn the connection with messages available
*/
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
done = false;
thread = new Thread(this);
thread.start();
}
}

/** Message reading thread. */
public void run() {
/** Check for sms connection. */
try {
msg = smsconn.receive();
if (msg != null) {
senderAddress = msg.getAddress();
content.setTitle("From: " + senderAddress);
if (msg instanceof TextMessage) {
content.setString(((TextMessage)msg).getPayloadText());
} else {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage)msg).getPayloadData();
for (int i = 0; i < data.length; i++) {
int intData = (int)data & 0xFF;
if (intData < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intData));
buf.append(' ');
}
content.setString(buf.toString());
}
content.addCommand(replyCommand);
display.setCurrent(content);
}
} catch (IOException e) {
// e.printStackTrace();
}
}
/**
* Pause signals the thread to stop by clearing the thread field.
* If stopped before done with the iterations it will
* be restarted from scratch later.
*/
public void pauseApp() {
done = true;
thread = null;
resumeScreen = display.getCurrent();
}

/**
* Destroy must cleanup everything. The thread is signaled
* to stop and no result is produced.
* @param unconditional true if a forced shutdown was requested
*/
public void destroyApp(boolean unconditional) {
done = true;
thread = null;
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException e) {
// Ignore any errors on shutdown
}
}
}

/**
* Respond to commands, including exit
* @param c user interface command requested
* @param s screen object initiating the request
*/
public void commandAction(Command c, Displayable s) {
try {
if (c == exitCommand || c == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
} else if (c == replyCommand) {
reply();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* Allow the user to reply to the received message
*/
private void reply() {
// remove the leading "sms://" for diplaying the destination address
String address = senderAddress.substring(6);
String statusMessage = "Sending message to " + address + "...";
sendingMessageAlert.setString(statusMessage);
sender.promptAndSend(senderAddress);
}
}

J2ME SMS : Sending SMS in J2ME

/************************************************** ***
J2ME SMS
Sending SMS in J2ME


insert this line into JAD file:
Port-SMS: 50056


************************************************** ****/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import javax.microedition.io.*;
import java.io.*;

public class SendSMSextends MIDlet
implements CommandListener, Runnable {

private Display display;
private Command cmdExit, cmdContinue, cmdBack, cmdSend;
private Displayable prevScreen;
private TextBox tfNum, tfText;
private String port;
private Thread thread;
private Alert alert;

private final String INVALID_PHONE =
"Nomor yang dimasukkan tidak absah";

public SendSMS() {
display = Display.getDisplay(this);

port = "50056";
if(getAppProperty("Port-SMS") != null)
port = getAppProperty("Port-SMS");

cmdExit = new Command("Exit", Command.EXIT, 1);
cmdContinue = new Command("Continue", Command.SCREEN, 1);
cmdBack = new Command("Back", Command.BACK, 1);
cmdSend = new Command("Send", Command.SCREEN, 1);

alert = new Alert(null);
alert.setTimeout(3000);
}

public void startApp() {
tfNum = generatePhoneInput();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdBack) {
display.setCurrent(tfNum);
} else if (c == cmdContinue) {
if (!isValidPhoneNumber(tfNum.getString())) {
alert.setType(AlertType.ERROR);
alert.setString(INVALID_PHONE);
display.setCurrent(alert,tfNum);
} else {
tfText = generateMessageInput();
}
} else {
thread = new Thread(this);
thread.start();
}
}

public void run() {
MessageConnection conn = null;
String address= "sms://" + tfNum.getString() + ":" + port;
try {
conn = (MessageConnection) Connector.open(address);
TextMessage msg = (TextMessage)
conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setAddress(address);
msg.setPayloadText(tfText.getString());
conn.send(msg);
alert.setString("Sending to " +
address.substring(6));
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

public TextBox generatePhoneInput() {
TextBox tfPhone = new TextBox("Number?", null,
15, TextField.PHONENUMBER);
tfPhone.addCommand(cmdExit);
tfPhone.addCommand(cmdContinue);
tfPhone.setCommandListener(this);
display.setCurrent(tfPhone);
return tfPhone;
}

public TextBox generateMessageInput() {
TextBox tfMessage = new TextBox("Text Message", null,
500, TextField.ANY);
tfMessage.addCommand(cmdBack);
tfMessage.addCommand(cmdSend);
tfMessage.setCommandListener(this);
display.setCurrent(tfMessage);
return tfMessage;
}

private static boolean isValidPhoneNumber(String number) {
char[] chars = number.toCharArray();
if (chars.length == 0) {
return false;
}
int startPos = 0;
//+
if (chars[0] == '+') {
startPos = 1;
}
for (int i = startPos; i < chars.length; ++i) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
}

J2ME Game: Maze Game

*
Title: J2ME Games With MIDP2
Authors: Carol Hamer
Publisher: Apress
ISBN: 1590593820
*/



import java.util.Random;
import java.util.Vector;

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

/**
* This is the main class of the maze game.
*
* @author Carol Hamer
*/
public class Maze extends MIDlet implements CommandListener {

//----------------------------------------------------------------
// game object fields

/**
* The canvas that the maze is drawn on.
*/
private MazeCanvas myCanvas;

/**
* The screen that allows the user to alter the size parameters
* of the maze.
*/
private SelectScreen mySelectScreen;

//----------------------------------------------------------------
// command fields

/**
* The button to exit the game.
*/
private Command myExitCommand = new Command("Exit", Command.EXIT, 99);

/**
* The command to create a new maze. (This command may appear in a menu)
*/
private Command myNewCommand = new Command("New Maze", Command.SCREEN, 1);

/**
* The command to dismiss an alert error message. In MIDP 2.0
* an Alert set to Alert.FOREVER automatically has a default
* dismiss command. This program does not use it in order to
* allow backwards com
*/
private Command myAlertDoneCommand = new Command("Done", Command.EXIT, 1);

/**
* The command to go to the screen that allows the user
* to alter the size parameters. (This command may appear in a menu)
*/
private Command myPrefsCommand
= new Command("Size Preferences", Command.SCREEN, 1);

//----------------------------------------------------------------
// initialization

/**
* Initialize the canvas and the commands.
*/
public Maze() {
try {
myCanvas = new MazeCanvas(Display.getDisplay(this));
myCanvas.addCommand(myExitCommand);
myCanvas.addCommand(myNewCommand);
myCanvas.addCommand(myPrefsCommand);
myCanvas.setCommandListener(this);
} catch(Exception e) {
// if there's an error during creation, display it as an alert.
Alert errorAlert = new Alert("error",
e.getMessage(), null, AlertType.ERROR);
errorAlert.setCommandListener(this);
errorAlert.setTimeout(Alert.FOREVER);
errorAlert.addCommand(myAlertDoneCommand);
Display.getDisplay(this).setCurrent(errorAlert);
}
}

//----------------------------------------------------------------
// implementation of MIDlet

/**
* Start the application.
*/
public void startApp() throws MIDletStateChangeException {
if(myCanvas != null) {
myCanvas.start();
}
}

/**
* Clean up.
*/
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
myCanvas = null;
System.gc();
}

/**
* Does nothing since this program occupies no shared resources
* and little memory.
*/
public void pauseApp() {
}

//----------------------------------------------------------------
// implementation of CommandListener

/*
* Respond to a command issued on the Canvas.
* (reset, exit, or change size prefs).
*/
public void commandAction(Command c, Displayable s) {
if(c == myNewCommand) {
myCanvas.newMaze();
} else if(c == myAlertDoneCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
} else if(c == myPrefsCommand) {
if(mySelectScreen == null) {
mySelectScreen = new SelectScreen(myCanvas);
}
Display.getDisplay(this).setCurrent(mySelectScreen);
} else if(c == myExitCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
}
}

}



/**
* This class is the display of the game.
*
* @author Carol Hamer
*/
class MazeCanvas extends javax.microedition.lcdui.Canvas {

//---------------------------------------------------------
// static fields

/**
* color constant
*/
public static final int BLACK = 0;

/**
* color constant
*/
public static final int WHITE = 0xffffff;

//---------------------------------------------------------
// instance fields

/**
* a handle to the display.
*/
private Display myDisplay;

/**
* The data object that describes the maze configuration.
*/
private Grid myGrid;

/**
* Whether or not the currently displayed maze has
* been completed.
*/
private boolean myGameOver = false;

/**
* maze dimension: the width of the maze walls.
*/
private int mySquareSize;

/**
* maze dimension: the maximum width possible for the maze walls.
*/
private int myMaxSquareSize;

/**
* maze dimension: the minimum width possible for the maze walls.
*/
private int myMinSquareSize;

/**
* top corner of the display: x-coordiate
*/
private int myStartX = 0;

/**
* top corner of the display: y-coordinate
*/
private int myStartY = 0;

/**
* how many rows the display is divided into.
*/
private int myGridHeight;

/**
* how many columns the display is divided into.
*/
private int myGridWidth;

/**
* the maximum number columns the display can be divided into.
*/
private int myMaxGridWidth;

/**
* the minimum number columns the display can be divided into.
*/
private int myMinGridWidth;

/**
* previous location of the player in the maze: x-coordiate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myOldX = 1;

/**
* previous location of the player in the maze: y-coordinate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myOldY = 1;

/**
* current location of the player in the maze: x-coordiate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myPlayerX = 1;

/**
* current location of the player in the maze: y-coordinate
* (in terms of the coordinates of the maze grid, NOT in terms
* of the coordinate system of the Canvas.)
*/
private int myPlayerY = 1;

//-----------------------------------------------------
// gets / sets

/**
* Changes the width of the maze walls and calculates how
* this change affects the number of rows and columns
* the maze can have.
* @return the number of columns now that the the
* width of the columns has been updated.
*/
int setColWidth(int colWidth) {
if(colWidth < 2) {
mySquareSize = 2;
} else {
mySquareSize = colWidth;
}
myGridWidth = getWidth() / mySquareSize;
if(myGridWidth % 2 == 0) {
myGridWidth -= 1;
}
myGridHeight = getHeight() / mySquareSize;
if(myGridHeight % 2 == 0) {
myGridHeight -= 1;
}
myGrid = null;
return(myGridWidth);
}

/**
* @return the minimum width possible for the maze walls.
*/
int getMinColWidth() {
return(myMinSquareSize);
}

/**
* @return the maximum width possible for the maze walls.
*/
int getMaxColWidth() {
return(myMaxSquareSize);
}

/**
* @return the maximum number of columns the display can be divided into.
*/
int getMaxNumCols() {
return(myMaxGridWidth);
}

/**
* @return the width of the maze walls.
*/
int getColWidth() {
return(mySquareSize);
}

/**
* @return the number of maze columns the display is divided into.
*/
int getNumCols() {
return(myGridWidth);
}

//-----------------------------------------------------
// initialization and game state changes

/**
* Constructor performs size calculations.
* @throws Exception if the display size is too
* small to make a maze.
*/
public MazeCanvas(Display d) throws Exception {
myDisplay = d;
// a few calculations to make the right maze
// for the current display.
int width = getWidth();
int height = getHeight();
// tests indicate that 5 is a good default square size,
// but the user can change it...
mySquareSize = 5;
myMinSquareSize = 3;
myMaxGridWidth = width / myMinSquareSize;
if(myMaxGridWidth % 2 == 0) {
myMaxGridWidth -= 1;
}
myGridWidth = width / mySquareSize;
if(myGridWidth % 2 == 0) {
myGridWidth -= 1;
}
myGridHeight = height / mySquareSize;
if(myGridHeight % 2 == 0) {
myGridHeight -= 1;
}
myMinGridWidth = 15;
myMaxSquareSize = width / myMinGridWidth;
if(myMaxSquareSize > height / myMinGridWidth) {
myMaxSquareSize = height / myMinGridWidth;
}
// if the display is too small to make a reasonable maze,
// then we throw an Exception
if(myMaxSquareSize < mySquareSize) {
throw(new Exception("Display too small"));
}
}

/**
* This is called as soon as the application begins.
*/
void start() {
myDisplay.setCurrent(this);
repaint();
}

/**
* discard the current maze and draw a new one.
*/
void newMaze() {
myGameOver = false;
// throw away the current maze.
myGrid = null;
// set the player back to the beginning of the maze.
myPlayerX = 1;
myPlayerY = 1;
myOldX = 1;
myOldY = 1;
myDisplay.setCurrent(this);
// paint the new maze
repaint();
}

//-------------------------------------------------------
// graphics methods

/**
* Create and display a maze if necessary, otherwise just
* move the player. Since the motion in this game is
* very simple, it is not necessary to repaint the whole
* maze each time, just the player + erase the square
* that the player just left..
*/
protected void paint(Graphics g) {
// If there is no current maze, create one and draw it.
if(myGrid == null) {
int width = getWidth();
int height = getHeight();
// create the underlying data of the maze.
myGrid = new Grid(myGridWidth, myGridHeight);
// draw the maze:
// loop through the grid data and color each square the
// right color
for(int i = 0; i < myGridWidth; i++) {
for(int j = 0; j < myGridHeight; j++) {
if(myGrid.mySquares[i][j] == 0) {
g.setColor(BLACK);
} else {
g.setColor(WHITE);
}
// fill the square with the appropriate color
g.fillRect(myStartX + (i*mySquareSize),
myStartY + (j*mySquareSize),
mySquareSize, mySquareSize);
}
}
// fill the extra space outside of the maze
g.setColor(BLACK);
g.fillRect(myStartX + ((myGridWidth-1) * mySquareSize),
myStartY, width, height);
// erase the exit path:
g.setColor(WHITE);
g.fillRect(myStartX + ((myGridWidth-1) * mySquareSize),
myStartY + ((myGridHeight-2) * mySquareSize), width, height);
// fill the extra space outside of the maze
g.setColor(BLACK);
g.fillRect(myStartX,
myStartY + ((myGridHeight-1) * mySquareSize), width, height);
}
// draw the player (red):
g.setColor(255, 0, 0);
g.fillRoundRect(myStartX + (mySquareSize)*myPlayerX,
myStartY + (mySquareSize)*myPlayerY,
mySquareSize, mySquareSize,
mySquareSize, mySquareSize);
// erase the previous location
if((myOldX != myPlayerX) || (myOldY != myPlayerY)) {
g.setColor(WHITE);
g.fillRect(myStartX + (mySquareSize)*myOldX,
myStartY + (mySquareSize)*myOldY,
mySquareSize, mySquareSize);
}
// if the player has reached the end of the maze,
// we display the end message.
if(myGameOver) {
// perform some calculations to place the text correctly:
int width = getWidth();
int height = getHeight();
Font font = g.getFont();
int fontHeight = font.getHeight();
int fontWidth = font.stringWidth("Maze Completed");
g.setColor(WHITE);
g.fillRect((width - fontWidth)/2, (height - fontHeight)/2,
fontWidth + 2, fontHeight);
// write in red
g.setColor(255, 0, 0);
g.setFont(font);
g.drawString("Maze Completed", (width - fontWidth)/2,
(height - fontHeight)/2,
g.TOP|g.LEFT);
}
}

/**
* Move the player.
*/
public void keyPressed(int keyCode) {
if(! myGameOver) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
if((myGrid.mySquares[myPlayerX-1][myPlayerY] == 1) &&
(myPlayerX != 1)) {
myOldX = myPlayerX;
myOldY = myPlayerY;
myPlayerX -= 2;
repaint();
}
break;
case RIGHT:
if(myGrid.mySquares[myPlayerX+1][myPlayerY] == 1) {
myOldX = myPlayerX;
myOldY = myPlayerY;
myPlayerX += 2;
repaint();
} else if((myPlayerX == myGrid.mySquares.length - 2) &&
(myPlayerY == myGrid.mySquares[0].length - 2)) {
myOldX = myPlayerX;
myOldY = myPlayerY;
myPlayerX += 2;
myGameOver = true;
repaint();
}
break;
case UP:
if(myGrid.mySquares[myPlayerX][myPlayerY-1] == 1) {
myOldX = myPlayerX;
myOldY = myPlayerY;
myPlayerY -= 2;
repaint();
}
break;
case DOWN:
if(myGrid.mySquares[myPlayerX][myPlayerY+1] == 1) {
myOldX = myPlayerX;
myOldY = myPlayerY;
myPlayerY += 2;
repaint();
}
break;
}
}
}

}

/**
* This is the screen that allows the user to modify the
* width of the maze walls..
*
* @author Carol Hamer
*/
class SelectScreen extends Form
implements ItemStateListener, CommandListener {

//----------------------------------------------------------------
// fields

/**
* The "Done" button to exit this screen and return to the maze.
*/
private Command myExitCommand = new Command("Done", Command.EXIT, 1);

/**
* The gague that modifies the width of the maze walls.
*/
private Gauge myWidthGauge;

/**
* The gague that displays the number of columns of the maze.
*/
private Gauge myColumnsGauge;

/**
* A handle to the main game canvas.
*/
private MazeCanvas myCanvas;

//----------------------------------------------------------------
// initialization

/**
* Create the gagues and place them on the screen.
*/
public SelectScreen(MazeCanvas canvas) {
super("Size Preferences");
addCommand(myExitCommand);
setCommandListener(this);
myCanvas = canvas;
setItemStateListener(this);
myWidthGauge = new Gauge("Column Width", true,
myCanvas.getMaxColWidth(),
myCanvas.getColWidth());
myColumnsGauge = new Gauge("Number of Columns", false,
myCanvas.getMaxNumCols(),
myCanvas.getNumCols());
// Warning: the setLayout method does not exist in
// MIDP 1.4. If there is any chance that a target
// device will be using MIDP 1.4, comment out the
// following two lines:
//myWidthGauge.setLayout(Item.LAYOUT_CENTER);
//myColumnsGauge.setLayout(Item.LAYOUT_CENTER);
append(myWidthGauge);
append(myColumnsGauge);
}

//----------------------------------------------------------------
// implementation of ItemStateListener

/**
* Respond to the user changing the width.
*/
public void itemStateChanged(Item item) {
if(item == myWidthGauge) {
int val = myWidthGauge.getValue();
if(val < myCanvas.getMinColWidth()) {
myWidthGauge.setValue(myCanvas.getMinColWidth());
} else {
int numCols = myCanvas.setColWidth(val);
myColumnsGauge.setValue(numCols);
}
}
}

//----------------------------------------------------------------
// implementation of CommandListener

/*
* Respond to a command issued on this screen.
* (either reset or exit).
*/
public void commandAction(Command c, Displayable s) {
if(c == myExitCommand) {
myCanvas.newMaze();
}
}

}


/**
* This class contains the data necessary to draw the maze.
*
* @author Carol Hamer
*/
class Grid {

/**
* Random number generator to create a random maze.
*/
private Random myRandom = new Random();

/**
* data for which squares are filled and which are blank.
* 0 = black
* 1 = white
* values higher than 1 are used during the maze creation
* algorithm.
* 2 = the square could possibly be appended to the maze this round.
* 3 = the square's color is not yet decided, and the square is
* not close enough to be appended to the maze this round.
*/
int[][] mySquares;

//--------------------------------------------------------
// maze generation methods

/**
* Create a new maze.
*/
public Grid(int width, int height) {
mySquares = new int[width][height];
// initialize all of the squares to white except a lattice
// framework of black squares.
for(int i = 1; i < width - 1; i++) {
for(int j = 1; j < height - 1; j++) {
if((i % 2 == 1) || (j % 2 == 1)) {
mySquares[i][j] = 1;
}
}
}
// the entrance to the maze is at (0,1).
mySquares[0][1] = 1;
createMaze();
}

/**
* This method randomly generates the maze.
*/
private void createMaze() {
// create an initial framework of black squares.
for(int i = 1; i < mySquares.length - 1; i++) {
for(int j = 1; j < mySquares[i].length - 1; j++) {
if((i + j) % 2 == 1) {
mySquares[i][j] = 0;
}
}
}
// initialize the squares that can be either black or white
// depending on the maze.
// first we set the value to 3 which means undecided.
for(int i = 1; i < mySquares.length - 1; i+=2) {
for(int j = 1; j < mySquares[i].length - 1; j+=2) {
mySquares[i][j] = 3;
}
}
// Then those squares that can be selected to be open
// (white) paths are given the value of 2.
// We randomly select the square where the tree of maze
// paths will begin. The maze is generated starting from
// this initial square and branches out from here in all
// directions to fill the maze grid.
Vector possibleSquares = new Vector(mySquares.length
* mySquares[0].length);
int[] startSquare = new int[2];
startSquare[0] = getRandomInt(mySquares.length / 2)*2 + 1;
startSquare[1] = getRandomInt(mySquares[0].length / 2)*2 + 1;
mySquares[startSquare[0]][startSquare[1]] = 2;
possibleSquares.addElement(startSquare);
// Here we loop to select squares one by one to append to
// the maze pathway tree.
while(possibleSquares.size() > 0) {
// the next square to be joined on is selected randomly.
int chosenIndex = getRandomInt(possibleSquares.size());
int[] chosenSquare = (int[])possibleSquares.elementAt(chosenIndex);
// we set the chosen square to white and then
// remove it from the list of possibleSquares (i.e. squares
// that can possibly be added to the maze), and we link
// the new square to the maze.
mySquares[chosenSquare[0]][chosenSquare[1]] = 1;
possibleSquares.removeElementAt(chosenIndex);
link(chosenSquare, possibleSquares);
}
// now that the maze has been completely generated, we
// throw away the objects that were created during the
// maze creation algorithm and reclaim the memory.
possibleSquares = null;
System.gc();
}

/**
* internal to createMaze. Checks the four squares surrounding
* the chosen square. Of those that are already connected to
* the maze, one is randomly selected to be joined to the
* current square (to attach the current square to the
* growing maze). Those squares that were not previously in
* a position to be joined to the maze are added to the list
* of "possible" squares (that could be chosen to be attached
* to the maze in the next round).
*/
private void link(int[] chosenSquare, Vector possibleSquares) {
int linkCount = 0;
int i = chosenSquare[0];
int j = chosenSquare[1];
int[] links = new int[8];
if(i >= 3) {
if(mySquares[i - 2][j] == 1) {
links[2*linkCount] = i - 1;
links[2*linkCount + 1] = j;
linkCount++;
} else if(mySquares[i - 2][j] == 3) {
mySquares[i - 2][j] = 2;
int[] newSquare = new int[2];
newSquare[0] = i - 2;
newSquare[1] = j;
possibleSquares.addElement(newSquare);
}
}
if(j + 3 <= mySquares[i].length) {
if(mySquares[i][j + 2] == 3) {
mySquares[i][j + 2] = 2;
int[] newSquare = new int[2];
newSquare[0] = i;
newSquare[1] = j + 2;
possibleSquares.addElement(newSquare);
} else if(mySquares[i][j + 2] == 1) {
links[2*linkCount] = i;
links[2*linkCount + 1] = j + 1;
linkCount++;
}
}
if(j >= 3) {
if(mySquares[i][j - 2] == 3) {
mySquares[i][j - 2] = 2;
int[] newSquare = new int[2];
newSquare[0] = i;
newSquare[1] = j - 2;
possibleSquares.addElement(newSquare);
} else if(mySquares[i][j - 2] == 1) {
links[2*linkCount] = i;
links[2*linkCount + 1] = j - 1;
linkCount++;
}
}
if(i + 3 <= mySquares.length) {
if(mySquares[i + 2][j] == 3) {
mySquares[i + 2][j] = 2;
int[] newSquare = new int[2];
newSquare[0] = i + 2;
newSquare[1] = j;
possibleSquares.addElement(newSquare);
} else if(mySquares[i + 2][j] == 1) {
links[2*linkCount] = i + 1;
links[2*linkCount + 1] = j;
linkCount++;
}
}
if(linkCount > 0) {
int linkChoice = getRandomInt(linkCount);
int linkX = links[2*linkChoice];
int linkY = links[2*linkChoice + 1];
mySquares[linkX][linkY] = 1;
int[] removeSquare = new int[2];
removeSquare[0] = linkX;
removeSquare[1] = linkY;
possibleSquares.removeElement(removeSquare);
}
}

/**
* a randomization utility.
* @param upper the upper bound for the random int.
* @return a random non-negative int less than the bound upper.
*/
public int getRandomInt(int upper) {
int retVal = myRandom.nextInt() % upper;
if(retVal < 0) {
retVal += upper;
}
return(retVal);
}

}

J2ME Encryption with Bouncy Castle

import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;


public class Encryptor {

private BufferedBlockCipher cipher;
private KeyParameter key;

// inisialisasi engine kriptografi.
// array key paling sedikit 8 bytes.
public Encryptor( byte[] key ){
cipher = new PaddedBlockCipher(new CBCBlockCipher(new DESEngine() ) );
this.key = new KeyParameter( key );
}

// inisialisasi engine kriptografi.
// string paling sedikit 8 chars.
public Encryptor( String key ){
this( key.getBytes() );
}

private byte[] callCipher( byte[] data ) throws CryptoException {
int size = cipher.getOutputSize( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes( data, 0, data.length, result, 0 );
olen += cipher.doFinal( result, olen );
if( olen <>
byte[] tmp = new byte[ olen ];
System.arraycopy( result, 0, tmp, 0, olen );
result = tmp;
}
return result;
}

// enkripsi arbitrary byte array
// mengembalikan data terenkripsi dalam bentuk yang berbeda
public synchronized byte[] encrypt( byte[] data ) throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( true, key );
return callCipher( data );
}

// enkripsi string.
public byte[] encryptString( String data ) throws CryptoException {

if( data == null || data.length() == 0 ){
return new byte[0];
}
return encrypt( data.getBytes() );
}

// Dekrip arbitrary data.
public synchronized byte[] decrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( false, key );
return callCipher( data );
}

// Dekrip string
public String decryptString( byte[] data )
throws CryptoException {

if( data == null || data.length == 0 ){

return "";

}
return new String( decrypt( data ) );
}
}

View PNG Image with Thread

/*--------------------------------------------------

* ViewPngThread.java

*

* Download and view a png file. The download is

* done in the background with a separate thread

*

* 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 ViewPngThread extends MIDlet implements CommandListener

{

private Display display;

private TextBox tbMain;

private Alert alStatus;

private Form fmViewPng;

private Command cmExit;

private Command cmView;

private Command cmBack;

private static final int ALERT_DISPLAY_TIME = 3000;

Image im = null;

public ViewPngThread()

{

display = Display.getDisplay(this);

// Create the Main textbox with a maximum of 75 characters

tbMain = new TextBox("Enter url", "http://www.corej2me.com/midpbook_v1e1/ch14/bird.png", 75, 0);

// Create commands and add to textbox

cmExit = new Command("Exit", Command.EXIT, 1);

cmView = new Command("View", Command.SCREEN, 2);

tbMain.addCommand(cmExit);

tbMain.addCommand(cmView );

// Set up a listener for textbox

tbMain.setCommandListener(this);

// Create the form that will hold the png image

fmViewPng = new Form("");

// Create commands and add to form

cmBack = new Command("Back", Command.BACK, 1);

fmViewPng.addCommand(cmBack);

// Set up a listener for form

fmViewPng.setCommandListener(this);

}

public void startApp()

{

display.setCurrent(tbMain);

}

public void pauseApp()

{ }

public void destroyApp(boolean unconditional)

{ }

/*--------------------------------------------------

* Process events

*-------------------------------------------------*/

public void commandAction(Command c, Displayable s)

{

// If the Command button pressed was "Exit"

if (c == cmExit)

{

destroyApp(false);

notifyDestroyed();

}

else if (c == cmView)

{

// Show alert indicating we are starting a download.

// This alert is NOT modal, it appears for

// approximately 3 seconds (see ALERT_DISPLAY_TIME)

showAlert("Downloading", false, tbMain);

// Create an instance of the class that will

// download the file in a separate thread

Download dl = new Download(tbMain.getString(), this);

// Start the thread/download

dl.start();

}

else if (c == cmBack)

{

display.setCurrent(tbMain);

}

}

/*--------------------------------------------------

* Called by the thread after attempting to download

* an image. If the parameter is 'true' the download

* was successful, and the image is shown on a form.

* If parameter is 'false' the download failed, and

* the user is returned to the textbox.

*

* In either case, show an alert indicating the

* the result of the download.

*-------------------------------------------------*/

public void showImage(boolean flag)

{

// Download failed...

if (flag == false)

{

// Alert followed by the main textbox

showAlert("Download Failure", true, tbMain);

}

else // Successful download...

{

ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);

// If there is already an image, set (replace) it

if (fmViewPng.size() != 0)

fmViewPng.set(0, ii);

else // Append the image to the empty form

fmViewPng.append(ii);

// Alert followed by the form holding the image

showAlert("Download Successful", true, fmViewPng);

}

}

/*--------------------------------------------------

* Show an alert with the parameters determining

* the type (modal or not) and the displayable to

* show after the alert is dismissed

*-------------------------------------------------*/

public void showAlert(String msg, boolean modal, Displayable displayable)

{

// Create alert, add text, associate a sound

alStatus = new Alert("Status", msg, null, AlertType.INFO);

// Set the alert type

if (modal)

alStatus.setTimeout(Alert.FOREVER);

else

alStatus.setTimeout(ALERT_DISPLAY_TIME);

// Show the alert, followed by the displayable

display.setCurrent(alStatus, displayable);

}

}

/*--------------------------------------------------

* Class - Download

*

* Download an image file in a separate thread

*-------------------------------------------------*/

class Download implements Runnable

{

private String url;

private ViewPngThread MIDlet;

private boolean downloadSuccess = false;

public Download(String url, ViewPngThread MIDlet)

{

this.url = url;

this.MIDlet = MIDlet;

}

/*--------------------------------------------------

* Download the image

*-------------------------------------------------*/

public void run()

{

try

{

getImage(url);

}

catch (Exception e)

{

System.err.println("Msg: " + e.toString());

}

}

/*--------------------------------------------------

* Create and start the new thread

*-------------------------------------------------*/

public void start()

{

Thread thread = new Thread(this);

try

{

thread.start();

}

catch (Exception e)

{

}

}

/*--------------------------------------------------

* Open connection and download png into a byte array.

*-------------------------------------------------*/

private void getImage(String url) throws IOException

{

ContentConnection connection = (ContentConnection) Connector.open(url);

// * There is a bug in MIDP 1.0.3 in which read() sometimes returns

// an invalid length. To work around this, I have changed the

// stream to DataInputStream and called readFully() instead of read()

// InputStream iStrm = connection.openInputStream();

DataInputStream iStrm = connection.openDataInputStream();

ByteArrayOutputStream bStrm = null;

Image im = null;

try

{

// ContentConnection includes a length method

byte imageData[];

int length = (int) connection.getLength();

if (length != -1)

{

imageData = new byte[length];

// Read the png into an array

// iStrm.read(imageData);

iStrm.readFully(imageData);

}

else // Length not available...

{

bStrm = new ByteArrayOutputStream();

int ch;

while ((ch = iStrm.read()) != -1)

bStrm.write(ch);

imageData = bStrm.toByteArray();

}

// Create the image from the byte array

im = Image.createImage(imageData, 0, imageData.length);

}

finally

{

// Clean up

if (connection != null)

connection.close();

if (iStrm != null)

iStrm.close();

if (bStrm != null)

bStrm.close();

}

// Return to the caller the status of the download

if (im == null)

MIDlet.showImage(false);

else

{

MIDlet.im = im;

MIDlet.showImage(true);

}

}

}

 

Design by Blogger Buster | Distributed by Blogging Tips