Affichage des articles dont le libellé est code source. Afficher tous les articles
Affichage des articles dont le libellé est code source. Afficher tous les articles

lundi 19 novembre 2007

Envoyer un sms en j2me


Voici une Midlet qui permet l'envoi et la réception de sms.

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

public class SMSMIDlet
extends MIDlet
implements CommandListener, Runnable {
private Sender mSender = null;
private Thread mReceiver = null;
private Command mExitCommand = new Command("Exit", Command.EXIT, 2);
private Command mRedCommand = new Command("Send Red", Command.SCREEN, 1);
private Command mBlueCommand = new Command("Send Blue", Command.SCREEN, 1);
private Display mDisplay = null;
protected ImageItem mColorSquare = null;
protected Image [] mImages = new Image[2];
protected Image waitImage = null;
private String mPort = "1234";
private TextField mNumberEntry= null;
private Form mForm = null;
private String mSenderAddress = null;
public SMSMIDlet() {
mSender = Sender.getInstance();
}
public void commandAction(javax.microedition.lcdui.Command c,
javax.microedition.lcdui.Displayable d) {
if (c == mExitCommand) {
if (!mSender.isSending()) {
destroyApp(true);
notifyDestroyed();
}
} else if (c == mRedCommand) {
String dest = mNumberEntry.getString();
if (dest.length() > 0)
mSender.sendMsg(dest, mPort, "red");
} else if (c == mBlueCommand) {
String dest = mNumberEntry.getString();
if (dest.length() > 0)
mSender.sendMsg(dest, mPort, "blue");
}
}

protected void destroyApp(boolean param) {
try {
mEndNow = true;
conn.close();
} catch (IOException ex) {
System.out.println("destroyApp caught: ");
ex.printStackTrace();
}
}
protected void pauseApp() {
}
protected void startApp() {
if (mForm == null) {
mForm = new Form("SMSMIDlet");
mNumberEntry = new TextField("Connect to:",
null, 256, TextField.PHONENUMBER);
try {
mImages[0] = Image.createImage("/red.png");
mImages[1] = Image.createImage("/blue.png");
waitImage = Image.createImage("/wait.png");
} catch (Exception ex) {
System.out.println("startApp caught: ");
ex.printStackTrace();
}
mColorSquare = new ImageItem(null, waitImage,ImageItem.
LAYOUT_DEFAULT, "colored square");
mForm.append(mNumberEntry);
mForm.append(mColorSquare);
mForm.addCommand(mExitCommand);
mForm.addCommand(mRedCommand);
mForm.addCommand(mBlueCommand);
mForm.setCommandListener(this);
}
Display.getDisplay(this).setCurrent(mForm);
startReceive();
}
private void startReceive() {
if (mReceiver != null)
return;

// Start receive thread
mReceiver = new Thread(this);
mReceiver.start();
}
private boolean mEndNow = false;
private MessageConnection conn = null;
public void run() {
Message msg = null;
String msgReceived = null;
conn = null;
mEndNow = false;
/** Check for sms connection. */
try {
conn = (MessageConnection) Connector.open("sms://:" + mPort);
msg = conn.receive();
while ((msg != null) && (!mEndNow)) {
if (msg instanceof TextMessage) {
msgReceived = ((TextMessage)msg).getPayloadText();
if (msgReceived.equals("red")) {
Display.getDisplay(this).callSerially(new SetRed());
} else if (msgReceived.equals("blue")) {
Display.getDisplay(this).callSerially(new SetBlue());
}
}
msg = conn.receive();
}
} catch (IOException e) {
// Normal exit when connection is closed
}
}
class SetRed implements Runnable {
Display disp = null;
public void run() {
mColorSquare.setImage(mImages[0]);
}
}
class SetBlue implements Runnable {
public void run() {
mColorSquare.setImage(mImages[1]);
}
}
}

Comment tester l'envoi de SMS :


Vous pouvez facilement tester SMSMIDlet, ou toute application SMS, à l'aide d'un utilitaire appelé le WMA présent dans WTK 2.x. .Pour accéder à la console de WMA, sélectionnez "files" puis "utilities" . Cliquez ensuite la console WMA, puis sélectionnez "launch".

Pour tester SMSMIDlet, d'abord démarrer une instance dans l' émulateur.
Quand il démarre. Notez que sur la barre de titre de l'émulateur un numéro de téléphone apparait, ce dernier commence par Un "+" , Par défaut, c'est le +5550000, C'est le numéro de l'émulateur.
Maintenant, lancez la console de WMA, Sélectionnez le numéro de l'émulateur, cliquez sur le bouton "send SMS".

mercredi 14 novembre 2007

Charger un TiledLayer en J2me


Un TiledLayer est un élément visuel composé d'une grille de cellules qui peuvent être remplis d'un ensemble de parties d'images.
Cette technique est couramment utilisée pour la réalisation de jeux java 2D sur téléphone .

Le code source suivant est celui de la classe DemoCanvas.java, pour pouvoir tester cet exemple et le faire fonctionner il faudra penser à créer une midlet qui fera appel à cette classe, je suppose que c'est assez simple donc nous n'allons pas en parler ici.

Code source J2me :



package exemple2;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.IOException;
/**
*
* @author java-Mobile
*/
public class DemoCanvas extends GameCanvas {

/** Creates a new instance of DemoCanvas */
protected DemoCanvas() {
super(true);
initLayer();
drawBackground();
}

public void initLayer() {

int backgroundWidth = 10;
int backgroundHeight = 10;
int tileWidth = 32;
int tileHeight = 32;
Image tilesImage = null;
try {
tilesImage = Image.createImage("/exemple2/modele.png");
} catch (IOException ex) {
throw new RuntimeException ("Unable to load Image - "+ex);
}
TiledLayer tiledLayer = new TiledLayer(
backgroundWidth, backgroundHeight,
tilesImage, tileWidth, tileHeight);

int[] map = {
1 , 5 , 5 , 5 , 5 , 5 , 5 , 4 , 4 , 4 ,
4 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 4 ,
4 , 5 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,
4 , 5 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,
4 , 4 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,
4 , 4 , 4 , 5 , 5 , 2 , 2 , 2 , 5 , 4 ,
1 , 1 , 3 , 3 , 5 , 5 , 2 , 2 , 5 , 4 ,
3 , 1 , 3 , 3 , 3 , 5 , 5 , 5 , 5 , 4 ,
3 , 1 , 1 , 3 , 3 , 3 , 5 , 5 , 1 , 1 ,
3 , 3 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 3 ,
3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 };
for (int y=0; y
for (int x=0; x
tiledLayer.setCell(y, x, map[y*backgroundWidth+x]);
}
}

setTiledLayer(tiledLayer);
}

public void drawBackground() {

// Get off-screen buffer
Graphics g = getGraphics();

// Clear the background
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());

// Paint the tiled layer
TiledLayer tiledLayer = getTiledLayer();
if(tiledLayer != null) {
tiledLayer.paint(g);
}
}

public TiledLayer getTiledLayer() {
return tiledLayer;
}


public void setTiledLayer(TiledLayer tiledLayer) {
this.tiledLayer = tiledLayer;
}


private TiledLayer tiledLayer;

public static final int BACKGROUND_COLOR = 0x00000000;

}

Plus d'infos sur la Class TiledLayer

mardi 13 novembre 2007

J2me comment afficher une image?

Voici le code source d'une Midlet qui permet l'affichage d'une image, l'exemple est assez simple à comprendre.

package exemple1;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

public class Affiche extends MIDlet {
private Display _display;
private Moteur _moteur;

public Affiche()
{
_moteur = new Moteur();
_display = Display.getDisplay(this);
}

class Moteur extends Canvas
{
private int _height,_zoneH;
private int _width,_zoneW;

public Moteur()
{
_height = getHeight();
_width = getWidth();
_zoneW = _width -5 ;
_zoneH = _height -5;

}

protected void paint(Graphics g)
{
//g.setColor(0);
g.fillRect(0, 0, _width, _height);
Image source;
try
{
source = Image.createImage("/exemple1/tux2.png");
}
catch (IOException e)
{
throw new RuntimeException ("Unable to load Image - "+e);
}
g.drawImage(source, _height/2, _width/2, Graphics.VCENTER|Graphics.HCENTER);

}



}

protected void keyPressed(int keyCode)
{

}

protected void keyReleased(int keyCode)
{

}
public void startApp() {
_display.setCurrent(_moteur);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Hosting