You are on page 1of 14

Image Class

Program to display image using Image Class

package hello;

importjava.io.IOException;
importjavax.microedition.lcdui.*;
importjavax.microedition.midlet.*;
public class Midlet extends MIDlet implements CommandListener
{
private Display d;
private Form f;
private Command exit;
private Image img;
privateImageItemimt;
private Alert a;
publicMidlet()
{
d=Display.getDisplay(this);
exit=new Command("Exit",Command.EXIT,1);
f=new Form("Images");
f.addCommand(exit);
f.setCommandListener(this);
try
{
img=Image.createImage("/sachin.png");
imt=new ImageItem(null,img,ImageItem.LAYOUT_CENTER|ImageItem.LAYOUT_NEWLINE_BEFORE|
ImageItem.LAYOUT_NEWLINE_BEFORE,"Sachin");
f.append(imt);
}
catch(java.io.IOException error)
{
a=new Alert("Error","Cant load the images",null,null);
a.setTimeout(Alert.FOREVER);
a.setType(AlertType.ERROR);
d.setCurrent(a);

}
}
public void startApp() {
d.setCurrent(f);
}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}

public void commandAction(Command c, Displayable d) {


if(c==exit)
{
destroyApp(false);
notifyDestroyed();
}
}
}

2)Prograrm to Create Login Form

package hello;

importjavax.microedition.midlet.MIDlet;
importjavax.microedition.lcdui.*;

public class Midlet extends MIDlet implements CommandListener{


private Display display;
privateTextFielduserName,password;
public Form form;
private Command login,cancel;
private Image img1,img2;

publicMidlet() {
form = new Form("Sign in");
userName = new TextField("LoginID:", "", 30, TextField.ANY);
password = new TextField("Password:", "", 30, TextField.PASSWORD);
cancel = new Command("Cancel", Command.CANCEL, 2);
login = new Command("Login", Command.OK, 2);
try{
img1= Image.createImage("/sachin.png");
img2=Image.createImage("/fail.png");

}catch(Exception e){
System.out.println(e.getMessage());
}
}

public void startApp() {


display = Display.getDisplay(this);
form.append(userName);
form.append(password);
form.addCommand(cancel);
form.addCommand(login);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp() {}


public void destroyApp(boolean unconditional) {
notifyDestroyed();
}

public void validateUser(String name, String password) {


if (name.equals("prabu") &&password.equals("xx")) {
showMsg();
} else {
tryAgain();
}
}

public void showMsg() {


Alert success = new Alert("Login Successfully",
"Your Login Process is completed!",
img1, AlertType.INFO);
success.setImage(img1);
userName.setString("");
password.setString("");
display.setCurrent(success, form);
}

public void tryAgain() {


Alert error = new Alert("Login Incorrect", "Please try again", img2, AlertType.ERROR);
error.setTimeout(900);
error.setImage(img2);
userName.setString("");
password.setString("");
display.setCurrent(error, form);
}

public void commandAction(Command c, Displayable d) {


String label = c.getLabel();
if(label.equals("Cancel")) {
destroyApp(true);
} else if(label.equals("Login")) {
validateUser(userName.getString(), password.getString());
}
}
}

3)ChoiceGroup or List Class


importjava.io.IOException;
importjavax.microedition.lcdui.*;
importjavax.microedition.midlet.*;

public class Midlet extends MIDlet implements CommandListener{


private Display display;
private List list;
private Command exit, next;
private Image car, airplane, hotel, mobile, cartoon;
String[] stringElements = {"Aero plane", "Car", "Hotel", "Mobile"};

publicMidlet(){
try{
airplane = Image.createImage("/airoplane.png");
car = Image.createImage("/car.png");
hotel = Image.createImage("/hotel.png");
mobile = Image.createImage("/mobile.png");
cartoon = Image.createImage("/sachin.png");
}catch(Exception e){
System.err.println(e.getMessage());
}
}

public void startApp() {


display = Display.getDisplay(this);
Image[] imageElements = {airplane, car, hotel, mobile};
list = new List("List + Image", List.IMPLICIT,
stringElements, imageElements);

next = new Command("Select", Command.SCREEN, 0);


exit = new Command("Exit", Command.EXIT, 0);
list.addCommand(next);
list.addCommand(exit);
list.setCommandListener(this);
display.setCurrent(list);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional){


notifyDestroyed();
}

public void commandAction(Command c, Displayable s){


int index = list.getSelectedIndex();
if (c == next || c == List.SELECT_COMMAND) {
Alert alert = new Alert("Selected", "You have selected: " + list.getString(index) + ".", cartoon,
AlertType.INFO);
display.setCurrent(alert, list);
display.setCurrent(alert, list);
} else if(c == exit){
destroyApp(true);
}
}
}
4) Alarm Application or Birthday Reminder

package mobileapplication1;
importjava.util.*;
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.microedition.media.*;
public class Midlet extends MIDlet implements ItemStateListener, CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private Command cmSnooze; // Start the timer
private Command cmReset; // Reset to current date/time
private Command cmExit; // Exit the MIDlet
privateDateFielddfSnoozeTime; // How long to snooze
privateintdateIndex; // Index of the DateField on the Form
private Date currentTime; // Current time...changes when pressing reset
private Timer tmSnooze; // The timer - keeps track of system time
privateSnoozeTimerttSnooze; // Called by the timer
privatebooleandateOK = false;
private Image img;// Was the user input valid?

publicMidlet()
{
display = Display.getDisplay(this);
// The main form
fmMain = new Form("When to sound the alarm:");
// Save today's date
currentTime = new Date();

// DateField with todays date as a default


dfSnoozeTime = new DateField("", DateField.DATE_TIME);
dfSnoozeTime.setDate(currentTime);
// All the commands/buttons
cmSnooze = new Command("Snooze", Command.SCREEN, 1);
cmReset = new Command("Reset", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.EXIT, 1);

// Add to form and listen for events


dateIndex= fmMain.append(dfSnoozeTime);
fmMain.addCommand(cmSnooze);
fmMain.addCommand(cmReset);
fmMain.addCommand(cmExit);
fmMain.setCommandListener(this);
fmMain.setItemStateListener(this);
try

{
img=Image.createImage("a.png");
}
catch(Exception e)
{

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

public void destroyApp(boolean unconditional)


{}
public void itemStateChanged(Item item)
{
if (item == dfSnoozeTime)
{
// If the user selected date and/or time that is earlier
// than today, set a flag. We are using getTime()
// method of the Date class, which returns the # of
// milliseconds since January 1, 1970
if (dfSnoozeTime.getDate().getTime() <currentTime.getTime())
dateOK = false;
else
dateOK = true;
}
}

public void commandAction(Command c, Displayable s)


{
if (c == cmSnooze)
{
if (dateOK == false)
{
Alert al = new Alert("Unable to set alarm", "Please choose another date and time.", null, null);
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.ERROR);
display.setCurrent(al);
}
else
{
// Create a new timer
tmSnooze = new Timer();
ttSnooze = new SnoozeTimer();
// Amount of time to delay
long amount = dfSnoozeTime.getDate().getTime() - currentTime.getTime();
tmSnooze.schedule(ttSnooze,amount);

// Remove the commands


fmMain.removeCommand(cmSnooze);
fmMain.removeCommand(cmReset);

// Remove the DateField


fmMain.delete(dateIndex);

// Change the Form message


fmMain.setTitle("Snoozing...");
}
}
else if (c == cmReset)
{
// Reset to the current date/time
dfSnoozeTime.setDate(currentTime = new Date());
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
// Handle the timer task
private class SnoozeTimer extends TimerTask
{
public final void run()
{
try
{
Alert a=new Alert("Happy Birthday");
a.setTimeout(Alert.FOREVER);
a.setImage(img);
Player player1 = Manager.createPlayer(getClass().getResourceAsStream("/c.wav"), "audio/x-
wav");
player1.start();
display.setCurrent(a);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

5)ChoiceGroup Class
package mobileapplication11;

import java.io.*;
importjavax.microedition.midlet.*;
importjavax.microedition.media.*;
importjavax.microedition.lcdui.*;

public class Midlet extends MIDlet implements ItemStateListener,


CommandListener{
private Display display;
private Form form;
private Command exit;
privateChoiceGroup choice;
private Player player1, player2;

public void startApp(){


try {
player1 = Manager.createPlayer(getClass().getResourceAsStream(
"/a.wav"), "audio/x-wav");
player2 = Manager.createPlayer(getClass().getResourceAsStream(
"/b.wav"), "audio/x-wav");
} catch(MediaException e) {
e.printStackTrace();
}catch(IOExceptionioe){
ioe.printStackTrace();
}
display = Display.getDisplay(this);
choice = new ChoiceGroup("List of RingTones", Choice.EXCLUSIVE);
choice.append("Airtel", null);
choice.append("Aadhivasi", null);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Playing song");
form.append(choice);
form.addCommand(exit);
form.setCommandListener(this);
form.setItemStateListener(this);
display.setCurrent(form);
}

public void pauseApp(){}

public void destroyApp(boolean a){


notifyDestroyed();
}

public void commandAction(Command c, Displayable s){


String label = c.getLabel();
if(label.equals("Exit")){
destroyApp(true);
}
}

public void itemStateChanged(Item item){


switch(choice.getSelectedIndex()){
case 0:
try{
player1.start();
if(player2 != null) player2.stop();
}catch(MediaException e) {
e.printStackTrace();
}
break;

case 1:
try{
player2.start();

if(player1 != null) player1.stop();


}catch(MediaException e) {
e.printStackTrace();
}
break;
}
}
}

6) Program to test the device is colour modal or not

package mobileapplication13;

importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;

public class Midlet extends MIDlet implements CommandListener {


private Display disp;
private Command quit;
TextBox t;
publicMidlet()
{
disp=Display.getDisplay(this);
quit=new Command("QUIT",Command.SCREEN,0);
String message=null;
if(disp.isColor())
{
message="Color Modal";
}
else
{
message="Black and white Modal";
}
t=new TextBox("Color Modal Test",message,40,0);

t.addCommand(quit);
t.setCommandListener(this);
}

public void startApp() {


disp.setCurrent(t);
}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}

public void commandAction(Command C,Displayable d)


{
if(C==quit)
{
destroyApp(true);
notifyDestroyed();
}

}
}
7) Simple HelloWorldMidlet

package mobileapplication14;

importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;

public class Midlet extends MIDlet implements CommandListener


{
TextBox t;
Display d;
Command cancel;

public void startApp() {


d=Display.getDisplay(this);
t= new TextBox("MCA","Hello World",40,0);
cancel= new Command("Quit",Command.SCREEN,0);
t.setCommandListener(this);
t.addCommand(cancel);
d.setCurrent(t);

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}
public void commandAction(Command c,Displayable dis)
{
if(c==cancel)
{
destroyApp(false);
notifyDestroyed();
}
}
}

8)Ticker Class
package tic;

importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;

public class Midlet extends MIDlet implements CommandListener {


private Display disp;
private Command quit,submit;
private List lis;
private Ticker tic;
private final String s1;
private final String s2;

publicMidlet()
{
disp=Display.getDisplay(this);
quit=new Command("QUIT",Command.SCREEN,0);
submit=new Command("SUBMIT",Command.SCREEN,1);
lis=new List("Stock",Choice.EXCLUSIVE);
s1=new String("21.0 19.0");
s2=new String("76.0,90.0");
tic=new Ticker(s1);
lis.append("Stock1",null);
lis.append("Stock2",null);
lis.addCommand(quit);
lis.addCommand(submit);
lis.setCommandListener(this);
lis.setTicker(tic);

public void startApp() {


disp.setCurrent(lis);
}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}

public void commandAction(Command C,Displayable d)


{
if(C==quit)
{
destroyApp(true);
}
else if(C==submit)
{
if(lis.getSelectedIndex()==0)
{
tic.setString(s1);
}
else
{
tic.setString(s2);
}
}
}
}

9) Record Management System

importjavax.microedition.rms.*;
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
import javax.microedition.io.*;

public class Midlet extends MIDlet


{
privateRecordStorers = null;
static final String REC_STORE = "rms1";

publicMidlet()
{
openRecStore(); // Create the record store

// Write a few records and read them back


writeRecord("developerWorks");
writeRecord("www.skcet.ac.in/developerWorks");
readRecords();

closeRecStore(); // Close record store

public void destroyApp(boolean unconditional)


{
}

public void startApp()


{
// There is no user interface, go ahead and shutdown
destroyApp(false);
notifyDestroyed();
}

public void pauseApp()


{
}

public void openRecStore()


{
try
{
// The second parameter indicates that the record store
// should be created if it does not exist
rs = RecordStore.openRecordStore(REC_STORE, true);
}
catch (Exception e)
{
db(e.toString());
}
}

public void closeRecStore()


{
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
db(e.toString());
}
}

public void writeRecord(String str)


{
byte[] rec = str.getBytes();

try
{
rs.addRecord(rec, 0, rec.length);
}
catch (Exception e)
{
db(e.toString());
}
}

public void readRecords()


{
try
{
byte[] recData = new byte[10];
intlen;

for (int i = 1; i <= rs.getNumRecords(); i++)


{
// Re-allocate if necessary
if (rs.getRecordSize(i) >recData.length)
recData = new byte[rs.getRecordSize(i)];

len = rs.getRecord(i, recData, 0);


System.out.println("Record #" + i + ": " + new String(recData, 0, len));
System.out.println("------------------------------");
}
}
catch (Exception e)
{
db(e.toString());
}
}

/*--------------------------------------------------
* Simple message to console for debug/errors
*-------------------------------------------------*/
private void db(String str)
{
System.err.println("Msg: " + str);
}

You might also like