PROBLEM STATEMENT
Create an application to perform the following operations on a persistent database
1. Adding Records
2. Deleting Records
3. Updating Records
CODE
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class RemoveRecordDemo extends MIDlet implements ItemStateListener, CommandListener
{
private Display display;
private Form frmMain;
private FormAdd frmAdd;
private List list = new List("All records ",List.IMPLICIT);
private Command cmdAdd, cmdExit, cmdDelete, cmdBack1,
cmdUpdate;
private Vector vecOP;
private ChoiceGroup chgOP;
protected DisplayManager displayMgr;
private RecordStore rs;
private static final String strREC_STORE_OP = "OPList";
private boolean flagSortByPriority = false,
flagShowPriority = true;
private ByteArrayInputStream istrmBytes = null;
private DataInputStream istrmDataType = null;
String strOP;
int strRecordId;
byte[] recData = new byte[250];
ByteArrayOutputStream ostrmBytes = null;
DataOutputStream ostrmDataType = null;
private RecordEnumeration e = null;
private ComparatorInt comp = null;
public RemoveRecordDemo()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
//initializing Form’s object
frmMain = new Form("");
frmAdd = new FormAdd("Add record", this);
//Initializing choice group
chgOP = new ChoiceGroup("", Choice.MULTIPLE);
vecOP = new Vector();
//Initailizing Command button
cmdAdd = new Command("Add record", Command.SCREEN, 2);
cmdDelete = new Command("Delete record", Command.SCREEN, 2);
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdBack1 = new Command("back", Command.BACK, 1);
cmdUpdate = new Command("Update record", Command.SCREEN, 2);
frmMain.addCommand(cmdBack1);
//Adding list to form
list.addCommand(cmdAdd);
list.addCommand(cmdUpdate);
list.addCommand(cmdDelete);
list.addCommand(cmdExit);
list.setCommandListener(this);
frmMain.append(chgOP);
// Adding Command Listner
frmMain.setCommandListener(this);
frmMain.setItemStateListener(this);
displayMgr = new DisplayManager(display, list);
rs = openRecStore(strREC_STORE_OP);
initInputStreams();
initOutputStreams();
initEnumeration();
writeRMS2Vector();
rebuildOPList();
}
public void startApp()
{
display.setCurrent(list);
}
public void destroyApp(boolean unconditional)
{
if (comp != null)
comp.compareIntClose();
if (e != null)
e.destroy();
try
{
if (istrmDataType != null)
istrmDataType.close();
if (istrmBytes != null)
istrmBytes.close();
if (ostrmDataType != null)
ostrmDataType.close();
if (ostrmBytes != null)
ostrmBytes.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
closeRecStore(rs);
}
public void pauseApp(){}
private void initInputStreams()
{
istrmBytes = new ByteArrayInputStream(recData);
istrmDataType = new DataInputStream(istrmBytes);
}
private void initOutputStreams()
{
ostrmBytes = new ByteArrayOutputStream();
ostrmDataType = new DataOutputStream(ostrmBytes);
}
private void initEnumeration()
{
if (flagSortByPriority)
comp = new ComparatorInt();
else
comp = null;
try
{
e = rs.enumerateRecords(null, comp, false);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
private RecordStore openRecStore(String name)
{
try
{
return RecordStore.openRecordStore(name, true);
}
catch (Exception e)
{
System.out.println(e.toString());
return null;
}
}
private void closeRecStore(RecordStore rs)
{
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
private void deleteRecStore(String name)
{
try
{
RecordStore.deleteRecordStore(name);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
protected void addOPItem(String Name, String Address, String phonenum)
{
if (strOP == "A")
{
try
{
ostrmBytes.reset();
ostrmDataType.writeUTF(Name);
ostrmDataType.writeUTF(Address);
ostrmDataType.writeUTF(phonenum);
ostrmDataType.flush();
byte[] record = ostrmBytes.toByteArray();
int recordId = rs.addRecord
(record, 0, record.length);
OPItem item = new OPItem
(Name, Address, phonenum, recordId);
vecOP.addElement(item);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (strOP == "U")
{
try
{
ostrmBytes.reset();
ostrmDataType.writeUTF(Name);
ostrmDataType.writeUTF(Address);
ostrmDataType.writeUTF(phonenum);
ostrmDataType.flush();
byte[] record = ostrmBytes.toByteArray();
rs.setRecord(strRecordId, record, 0, record.length);
OPItem item = new OPItem
(Name, Address, phonenum, strRecordId);
vecOP.addElement(item);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
writeRMS2Vector();
rebuildOPList();
}
private void writeRMS2Vector()
{
vecOP.removeAllElements();
try
{
e.rebuild();
while (e.hasNextElement())
{
istrmBytes.reset();
int id = e.nextRecordId();
rs.getRecord(id, recData, 0);
OPItem item = new OPItem(istrmDataType.readUTF(),
istrmDataType.readUTF(),
istrmDataType.readUTF(), id);
vecOP.addElement(item);
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
protected void rebuildOPList()
{
for (int i = chgOP.size(); i > 0; i--)
chgOP.delete(i - 1);
list.deleteAll();
OPItem item;
String phonenum;
String Name;
String Address;
StringBuffer strb;
for (int i = 0; i < vecOP.size(); i++)
{
item = (OPItem)vecOP.elementAt(i);
phonenum = item.getphonenum();
Name = item.getName();
Address = item.getAddress();
strb = new StringBuffer();
strb.append(Name + " " + Address + " " + phonenum);
list.append(strb.toString(), null);
chgOP.append(strb.toString(), null);
}
}
public void itemStateChanged(Item item)
{
ChoiceGroup cg;
cg = (ChoiceGroup)item;
boolean selected[] = new boolean[cg.size()];
cg.getSelectedFlags(selected);
for (int i = 0; i < cg.size(); i++)
{
if (selected[i])
{
OPItem opitem = (OPItem)vecOP.elementAt(i);
if (strOP == "D")
{
try
{
rs.deleteRecord(opitem.getRecordId());
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
if (strOP == "U")
{
displayMgr.pushDisplayable(frmAdd);
frmAdd.setTitle("Update record");
try
{
frmAdd.tfName.setString(opitem.getName()); frmAdd.tfAddress.setString(opitem.getAddress());
frmAdd.tfPhonenum.setString(opitem.getphonenum());
strRecordId = opitem.getRecordId();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
break;
}
}
writeRMS2Vector();
rebuildOPList();
}
public void commandAction(Command c, Displayable d)
{
if (c == cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
else
{
if (c == cmdAdd)
{
strOP = "A";
frmAdd.setTitle("Add record");
frmAdd.tfName.setString("");
frmAdd.tfAddress.setString("");
frmAdd.tfPhonenum.setString("");
displayMgr.pushDisplayable(frmAdd);
}
else if (c == cmdDelete)
{
strOP = "D";
frmMain.setTitle("Delete record");
displayMgr.pushDisplayable(frmMain);
}
else if (c == cmdUpdate)
{
strOP = "U";
frmMain.setTitle("Update record");
displayMgr.pushDisplayable(frmMain);
}
else if (c == cmdBack1)
{
displayMgr.popDisplayable();
}
}
}
}
class DisplayManager extends Stack
{
// Reference to Display object
private Display display;
// Main displayable for MIDlet
private Displayable mainDisplayable;
private Alert alStackError; // Alert for error conditions
/* Display manager constructor */
public DisplayManager(Display display, Displayable mainDisplayable)
{
// Only one display object per midlet, this is it
this.display = display;
this.mainDisplayable = mainDisplayable;
// Create an alert displayed when an error occurs
alStackError = new Alert("Displayable Stack Error");
alStackError.setTimeout(Alert.FOREVER); // Modal
}
public void pushDisplayable(Displayable newDisplayable)
{
//System.out.println("pushDisplayable");
push(display.getCurrent());
display.setCurrent(newDisplayable);
}
/*Return to the main displayable object of MIDlet*/
public void home()
{
while (elementCount > 1)
pop();
display.setCurrent(mainDisplayable);
}
/* Pop displayable from stack and set as active */
public void popDisplayable()
{
// If the stack is not empty, pop next displayable
if (empty() == false)
display.setCurrent((Displayable)pop());
else
// On error show an alert
// Once acknowldeged, set 'mainDisplayable' as active
display.setCurrent(alStackError, mainDisplayable);
}
}
class FormAdd extends Form implements CommandListener
{
private Command cmBack,cmSave;
protected TextField tfName;
protected TextField tfAddress;
protected TextField tfPhonenum;
//protected ChoiceGroup cgPriority;
private RemoveRecordDemo midlet;
public FormAdd(String title, RemoveRecordDemo midlet)
{
// Call the Form constructor
super(title);
// Save reference to MIDlet so we can access
// the display manager class and rms
this.midlet = midlet;
//Initailizing Command buttons
cmSave = new Command("Save", Command.SCREEN, 1);
cmBack = new Command("Back", Command.BACK, 2);
// Create textfield for entering todo items
tfName = new TextField
("Name", null, 15, TextField.ANY);
tfAddress = new TextField
("Address", null, 250, TextField.ANY);
tfPhonenum = new TextField
("Phonenum", null, 15, TextField.PHONENUMBER);
// Create choicegroup and append options (no images)
//cgPriority = new ChoiceGroup("Priority", Choice.EXCLUSIVE);
// Add buttons to form
addCommand(cmSave);
addCommand(cmBack);
append(tfName);
append(tfAddress);
append(tfPhonenum);
setCommandListener(this);
}
public void commandAction(Command c, Displayable s)
{
if (c == cmSave)
{
// Add a new todo item
/* Notice we bump priority by 1. This is because the choicegroup entries start at zero. We would like the records in the rms to store priorities starting at 1. Thus, if a user requests to display priorities on the todo list, the highest priority is 1 (not zero)*/
midlet.addOPItem(tfName.getString(), tfAddress.getString(), tfPhonenum.getString());
}
// Any other event and we go back to the main form...
// Pop the last displayable off the stack
midlet.displayMgr.popDisplayable();
}
}
class ComparatorInt implements RecordComparator
{
// Read from a specified byte array
private byte[] record = new byte[10];
// Read Java data types from the above byte array
private ByteArrayInputStream strmBytes = null;
private DataInputStream strmDataType = null;
public void compareIntClose()
{
try
{
if (strmBytes != null)
strmBytes.close();
if (strmDataType != null)
strmDataType.close();
}
catch (Exception e)
{}
}
public int compare(byte[] rec1, byte[] rec2)
{
int x1, x2;
try
{
//If either record is larger than our buffer, reallocate
int maxsize = Math.max(rec1.length, rec2.length);
if (maxsize > record.length)
record = new byte[maxsize];
// Read record #1
// We want the priority which is first "field"
strmBytes = new ByteArrayInputStream(rec1);
strmDataType = new DataInputStream(strmBytes);
x1 = strmDataType.readInt();
// Read record #2
strmBytes = new ByteArrayInputStream(rec2);
strmDataType = new DataInputStream(strmBytes);
x2 = strmDataType.readInt();
// Compare record #1 and #2
if (x1 == x2)
return RecordComparator.EQUIVALENT;
else if (x1 < x2)
return RecordComparator.PRECEDES;
else
return RecordComparator.FOLLOWS;
}
catch (Exception e)
{
return RecordComparator.EQUIVALENT;
}
}
}
class OPItem
{
private String phonenum;
private String Name;
private String Address;
private int recordId;
public OPItem(String Name,String Address, String phonenum, int recordId)
{
this.Name = Name;
this.Address = Address;
this.phonenum = phonenum;
this.recordId = recordId;
}
public String getphonenum()
{
return phonenum;
}
public void setphonenum(String phonenum)
{
// setphonenum
this.phonenum = phonenum;
}
public String getAddress()
{
//getAddress
return Address;
}
public void setAddress(String Address)
{
// setAddress
this.Address = Address;
}
public String getName()
{
// getName
return Name;
}
public void setName(String Name)
{
// setName
this.Name = Name;
}
public int getRecordId()
{
// getRecordId
return recordId;
}
}
Showing posts with label J2ME. Show all posts
Showing posts with label J2ME. Show all posts
Wednesday, November 18, 2009
To Demonstrate Searching the word
PROBLEM STATEMET
Create an application in j2me to demonstrate that will search for a word present in the given paragraph with 2 options:
a. case sensitive
b. case insensitive
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SearchMidlet extends MIDlet implements CommandListener
{
private Display display;
private Form frm1, frm2;
private TextField txtfsearch;
private Command cmdquit;
private Command cmdback;
private Command cmdsearch;
private String strSearch;
private String strFind;
private String strAlert = "The Searched word position is ";
private ChoiceGroup chgsearchcase;
private Alert alert;
private int fromIndex = 0;
private boolean flag = true;
public SearchMidlet()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
//initializing Form’s object
frm1 = new Form("MIDP");
frm2 = new Form("Search Utility");
strSearch = "The J2ME Wireless Toolkit is a set of tools that makes it possible to createapplications for mobile phones and other wireless devices. Although it is based on the Mobile Information Device Profile (MIDP) 2.0, the J2ME Wireless Toolkit also supports a handful of optional packages, making it a widely capable development toolkit.";
//Initailizing TextField’s objects.
txtfsearch = new TextField("Search : ", "", 12, TextField.ANY);
//Initailizing save Command button
cmdquit = new Command("Quit", Command.EXIT, 0);
cmdback = new Command("Back", Command.EXIT, 1);
cmdsearch = new Command("Search", Command.SCREEN, 0);
chgsearchcase = new ChoiceGroup
("Search Case :", Choice.EXCLUSIVE);
//Adding Choice Group to form
chgsearchcase.append("Case Sensetive", null);
chgsearchcase.append("Case Insensetive", null);
//Initailizing Alert object
alert = new Alert
("Search Alert", "", null, AlertType.CONFIRMATION);
//Adding TextFeilds and Commands to from
frm1.addCommand(cmdquit);
frm1.addCommand(cmdsearch);
frm1.append(strSearch);
frm2.addCommand(cmdsearch);
frm2.addCommand(cmdback);
frm2.append(txtfsearch);
frm2.append(chgsearchcase);
frm2.append("");
frm2.append("");
alert.addCommand(cmdsearch);
alert.addCommand(cmdback);
// Adding Command Listner
frm1.setCommandListener(this);
frm2.setCommandListener(this);
alert.setCommandListener(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display.setCurrent(frm1);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if(command == cmdquit)
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
else if(command == cmdsearch)
{
// Indicating the object to be Displayed
if(displayable==frm1)
display.setCurrent(frm2);
}
else if(displayable == frm2)
{
strFind = txtfsearch.getString();
searchUtility();
}
else if(displayable == alert)
{
searchUtility();
}
}
else if(command == cmdback)
{
if(displayable == frm2)
{
display.setCurrent(frm1);
}
else if(displayable == alert)
{
display.setCurrent(frm2); strAlert =
""; flag = true;
}
}
}
public void searchUtility()
{
if(chgsearchcase.getSelectedIndex() == 0)
{
if(flag)
{
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else
fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());
if(fromIndex != -1)
{
strAlert += String.valueOf(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
else
frm2.append(strFind + " does not found.");
}
else
{
if(flag)
{
strFind = strFind.toLowerCase();
strSearch = strSearch.toLowerCase();
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else
fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());
if(fromIndex != -1)
{
strAlert += String.valueOf
(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
else
frm2.append(strFind + " does not found.");
}
}
}
Create an application in j2me to demonstrate that will search for a word present in the given paragraph with 2 options:
a. case sensitive
b. case insensitive
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class SearchMidlet extends MIDlet implements CommandListener
{
private Display display;
private Form frm1, frm2;
private TextField txtfsearch;
private Command cmdquit;
private Command cmdback;
private Command cmdsearch;
private String strSearch;
private String strFind;
private String strAlert = "The Searched word position is ";
private ChoiceGroup chgsearchcase;
private Alert alert;
private int fromIndex = 0;
private boolean flag = true;
public SearchMidlet()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
//initializing Form’s object
frm1 = new Form("MIDP");
frm2 = new Form("Search Utility");
strSearch = "The J2ME Wireless Toolkit is a set of tools that makes it possible to createapplications for mobile phones and other wireless devices. Although it is based on the Mobile Information Device Profile (MIDP) 2.0, the J2ME Wireless Toolkit also supports a handful of optional packages, making it a widely capable development toolkit.";
//Initailizing TextField’s objects.
txtfsearch = new TextField("Search : ", "", 12, TextField.ANY);
//Initailizing save Command button
cmdquit = new Command("Quit", Command.EXIT, 0);
cmdback = new Command("Back", Command.EXIT, 1);
cmdsearch = new Command("Search", Command.SCREEN, 0);
chgsearchcase = new ChoiceGroup
("Search Case :", Choice.EXCLUSIVE);
//Adding Choice Group to form
chgsearchcase.append("Case Sensetive", null);
chgsearchcase.append("Case Insensetive", null);
//Initailizing Alert object
alert = new Alert
("Search Alert", "", null, AlertType.CONFIRMATION);
//Adding TextFeilds and Commands to from
frm1.addCommand(cmdquit);
frm1.addCommand(cmdsearch);
frm1.append(strSearch);
frm2.addCommand(cmdsearch);
frm2.addCommand(cmdback);
frm2.append(txtfsearch);
frm2.append(chgsearchcase);
frm2.append("");
frm2.append("");
alert.addCommand(cmdsearch);
alert.addCommand(cmdback);
// Adding Command Listner
frm1.setCommandListener(this);
frm2.setCommandListener(this);
alert.setCommandListener(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display.setCurrent(frm1);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if(command == cmdquit)
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
else if(command == cmdsearch)
{
// Indicating the object to be Displayed
if(displayable==frm1)
display.setCurrent(frm2);
}
else if(displayable == frm2)
{
strFind = txtfsearch.getString();
searchUtility();
}
else if(displayable == alert)
{
searchUtility();
}
}
else if(command == cmdback)
{
if(displayable == frm2)
{
display.setCurrent(frm1);
}
else if(displayable == alert)
{
display.setCurrent(frm2); strAlert =
""; flag = true;
}
}
}
public void searchUtility()
{
if(chgsearchcase.getSelectedIndex() == 0)
{
if(flag)
{
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else
fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());
if(fromIndex != -1)
{
strAlert += String.valueOf(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
else
frm2.append(strFind + " does not found.");
}
else
{
if(flag)
{
strFind = strFind.toLowerCase();
strSearch = strSearch.toLowerCase();
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else
fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());
if(fromIndex != -1)
{
strAlert += String.valueOf
(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
else
frm2.append(strFind + " does not found.");
}
}
}
To Demonstrate Geometric Figure
PROBLEM STATEMENT
Create an application in j2me to demonstrate different events shapes, circle, square, rectangle, triangle on canvas.
CODE
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class practical8 extends MIDlet implements CommandListener
{
Display display;
public mycanvas can;
public practical8()
{
display=Display.getDisplay(this);
can=new mycanvas(this);
display.setCurrent(can);
}
public void startApp()
{
display.setCurrent(can);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
destroyApp(false);
notifyDestroyed();
}
public void finishp()
{
// Indicating the object to be Displayed
notifyDestroyed();
}
public void commandAction(Command c, Displayable d)
{
}
}
class mycanvas extends Canvas implements CommandListener
{
//declearing & Initailizing Command buttons
private Command cmdexit;
practical8 p;
int i;
String str1="Press 1 for Square";
String str2="Press 2 for Rectangle";
String str3="Press 3 for Traingle";
String str4="Press 4 for Circle";
public mycanvas(practical8 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit=new Command("EXIT",Command.EXIT,1);
//Adding Command buttons to the Canvas
addCommand(cmdexit);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xff000000);
g.drawString(str1,50,230,Graphics.BOTTOM|Graphics.LEF);
g.drawString(str2,50,241,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str3,50,252,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str4,50,263,Graphics.BOTTOM|Graphics.LEFT);
if(i==1)
{
g.setColor(0xff0000);
g.drawRect(getWidth()/4,getWidth()/4,getWidth()/4,
getHeight()/5);
repaint();
}
if(i==2)
{
g.setColor(0xff0000);
g.drawRect(getHeight()/4, getWidth()/2,(getHeight()/4)+2,
(getWidth()/2)+2);
repaint();
}
if(i==4)
{
g.setColor(0xff0000);
g.drawArc(getWidth()/4,getWidth()/4,getWidth()/4,
getWidth()/4,360,-360);
repaint();
}
if(i==3)
{
g.setColor(0xff0000);
g.drawLine(30,getHeight()/2,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/4,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/2,30,getHeight()/4);
repaint();
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.finishp();
}
repaint();
}
protected void keyPressed(int key)
{
try
{
switch ( key )
{
case KEY_NUM1:
i=1;
break;
case KEY_NUM2:
i=2;
break;
case KEY_NUM3:
i=3;
break;
case KEY_NUM4:
i=4;
break;
}
}
catch(Exception e){}
repaint();
}
}
Create an application in j2me to demonstrate different events shapes, circle, square, rectangle, triangle on canvas.
CODE
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class practical8 extends MIDlet implements CommandListener
{
Display display;
public mycanvas can;
public practical8()
{
display=Display.getDisplay(this);
can=new mycanvas(this);
display.setCurrent(can);
}
public void startApp()
{
display.setCurrent(can);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
destroyApp(false);
notifyDestroyed();
}
public void finishp()
{
// Indicating the object to be Displayed
notifyDestroyed();
}
public void commandAction(Command c, Displayable d)
{
}
}
class mycanvas extends Canvas implements CommandListener
{
//declearing & Initailizing Command buttons
private Command cmdexit;
practical8 p;
int i;
String str1="Press 1 for Square";
String str2="Press 2 for Rectangle";
String str3="Press 3 for Traingle";
String str4="Press 4 for Circle";
public mycanvas(practical8 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit=new Command("EXIT",Command.EXIT,1);
//Adding Command buttons to the Canvas
addCommand(cmdexit);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xff000000);
g.drawString(str1,50,230,Graphics.BOTTOM|Graphics.LEF);
g.drawString(str2,50,241,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str3,50,252,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str4,50,263,Graphics.BOTTOM|Graphics.LEFT);
if(i==1)
{
g.setColor(0xff0000);
g.drawRect(getWidth()/4,getWidth()/4,getWidth()/4,
getHeight()/5);
repaint();
}
if(i==2)
{
g.setColor(0xff0000);
g.drawRect(getHeight()/4, getWidth()/2,(getHeight()/4)+2,
(getWidth()/2)+2);
repaint();
}
if(i==4)
{
g.setColor(0xff0000);
g.drawArc(getWidth()/4,getWidth()/4,getWidth()/4,
getWidth()/4,360,-360);
repaint();
}
if(i==3)
{
g.setColor(0xff0000);
g.drawLine(30,getHeight()/2,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/4,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/2,30,getHeight()/4);
repaint();
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.finishp();
}
repaint();
}
protected void keyPressed(int key)
{
try
{
switch ( key )
{
case KEY_NUM1:
i=1;
break;
case KEY_NUM2:
i=2;
break;
case KEY_NUM3:
i=3;
break;
case KEY_NUM4:
i=4;
break;
}
}
catch(Exception e){}
repaint();
}
}
To Demonstrate Key Pressed
PROBLEM STATEMENT
Create an application in j2me to demonstrate Different picture by pressing selected button.
CODE
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class practical7 extends MIDlet
{
//Declaring Display’s object
Display display;
//Declaring mycanvas’s object
public mycanvas can;
String s;
public practical7()
{
// Indicating the object to be Displayed
display=Display.getDisplay(this);
//Initailizing mycanvas object.
can=new mycanvas(this);
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void startApp()
{
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional)
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
public void exitMIDlet()
{
// Indicating the object to be Displayed
destroyApp(true);
notifyDestroyed();
}
}
class mycanvas extends Canvas implements CommandListener
{
private Command exit;
private static Image image;
private practical7 prac7;
public mycanvas (practical7 prac7)
{
this.prac7 = prac7;
//Initailizing exit Command button
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
// Adding Command Listner
setCommandListener(this);
try
{
// create the image and assign it to the variable
image of the type Image
image = Image.createImage("/0.png");
}
catch(Exception ex)
{
//message get displayed if the image is not get assigned properly.
System.out.println("Image not found : " + ex);
}
}
protected void paint(Graphics g)
{
repaint();
// Drawing a WHTE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
try
{
//displaying the image at the center of the screen
g.drawImage(image, 100,100,g.TOP|g.HCENTER);
}
catch (Exception e){}
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
prac7.exitMIDlet();
}
}
protected void keyPressed(int key)
{
try
{
//capture user response when key pressed & accordingly display the images
switch ( key )
{
case KEY_NUM1:
image = Image.createImage("/1.png");
break;
case KEY_NUM2:
image = Image.createImage("/2.png");
break;
case KEY_NUM3:
image = Image.createImage("/3.png");
break;
case KEY_NUM4:
image = Image.createImage("/4.png");
break;
case KEY_NUM5:
image = Image.createImage("/5.png");
break;
case KEY_NUM6:
image = Image.createImage("/6.png");
break;
case KEY_NUM7:
image = Image.createImage("/7.png");
break;
case KEY_NUM8:
image = Image.createImage("/8.png");
break;
case KEY_NUM9:
image = Image.createImage("/9.png");
break;
case KEY_NUM0:
image = Image.createImage("/2.png");
break;
}
}
catch(Exception e){}
repaint();
}
}
Create an application in j2me to demonstrate Different picture by pressing selected button.
CODE
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class practical7 extends MIDlet
{
//Declaring Display’s object
Display display;
//Declaring mycanvas’s object
public mycanvas can;
String s;
public practical7()
{
// Indicating the object to be Displayed
display=Display.getDisplay(this);
//Initailizing mycanvas object.
can=new mycanvas(this);
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void startApp()
{
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional)
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
public void exitMIDlet()
{
// Indicating the object to be Displayed
destroyApp(true);
notifyDestroyed();
}
}
class mycanvas extends Canvas implements CommandListener
{
private Command exit;
private static Image image;
private practical7 prac7;
public mycanvas (practical7 prac7)
{
this.prac7 = prac7;
//Initailizing exit Command button
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
// Adding Command Listner
setCommandListener(this);
try
{
// create the image and assign it to the variable
image of the type Image
image = Image.createImage("/0.png");
}
catch(Exception ex)
{
//message get displayed if the image is not get assigned properly.
System.out.println("Image not found : " + ex);
}
}
protected void paint(Graphics g)
{
repaint();
// Drawing a WHTE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
try
{
//displaying the image at the center of the screen
g.drawImage(image, 100,100,g.TOP|g.HCENTER);
}
catch (Exception e){}
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
prac7.exitMIDlet();
}
}
protected void keyPressed(int key)
{
try
{
//capture user response when key pressed & accordingly display the images
switch ( key )
{
case KEY_NUM1:
image = Image.createImage("/1.png");
break;
case KEY_NUM2:
image = Image.createImage("/2.png");
break;
case KEY_NUM3:
image = Image.createImage("/3.png");
break;
case KEY_NUM4:
image = Image.createImage("/4.png");
break;
case KEY_NUM5:
image = Image.createImage("/5.png");
break;
case KEY_NUM6:
image = Image.createImage("/6.png");
break;
case KEY_NUM7:
image = Image.createImage("/7.png");
break;
case KEY_NUM8:
image = Image.createImage("/8.png");
break;
case KEY_NUM9:
image = Image.createImage("/9.png");
break;
case KEY_NUM0:
image = Image.createImage("/2.png");
break;
}
}
catch(Exception e){}
repaint();
}
}
To demonstrate different “INPUT BOXES”
PROBLEM STATEMENT
Write a program in J2ME to demonstrate different input boxes.
1.Edit Box(Text Box)
2.Buttons
3.Radio Buttons
4.Check Box
5.List Box.
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Practical3 extends MIDlet implements CommandListener
{
private Command exit,next;
private TextField tb1;
private Form frm1;
private Display display;
private ChoiceGroup rb;
private ChoiceGroup cb;
private List list;
private Alert alert;
private String[] options={"Option A","Option B"};
public Practical3()
{
next=new Command("NEXT",Command.SCREEN,0);
exit=new Command("Exit",Command.SCREEN,0);
frm1=new Form("Practical3");
tb1=new TextField("Enter Name","",30,TextField.ANY);
rb=new ChoiceGroup("Option",Choice.EXCLUSIVE);
rb.append("Male",null);
rb.append("Female",null);
cb=new ChoiceGroup("Option",Choice.MULTIPLE);
cb.append("Red",null);
cb.append("Green",null);
list=new List("Menu:",List.IMPLICIT,options,null);
list.addCommand(next);
list.setCommandListener(this);
frm1.append(tb1);
frm1.append(rb);
frm1.append(cb);
frm1.addCommand(exit);
frm1.setCommandListener(this);
public void startApp()
{
display=Display.getDisplay(this);
display.setCurrent(list);
}
public void pauseApp(){ }
public void destroyApp(boolean b){ }
public void commandAction(Command c,Displayable d)
{
if(c==next)
{
alert=new Alert("Next Form","Moving To Next Form",null,null);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,frm1);
}
if(c==exit)
{
destroyApp(true);
notifyDestroyed();
}
}
}
Write a program in J2ME to demonstrate different input boxes.
1.Edit Box(Text Box)
2.Buttons
3.Radio Buttons
4.Check Box
5.List Box.
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Practical3 extends MIDlet implements CommandListener
{
private Command exit,next;
private TextField tb1;
private Form frm1;
private Display display;
private ChoiceGroup rb;
private ChoiceGroup cb;
private List list;
private Alert alert;
private String[] options={"Option A","Option B"};
public Practical3()
{
next=new Command("NEXT",Command.SCREEN,0);
exit=new Command("Exit",Command.SCREEN,0);
frm1=new Form("Practical3");
tb1=new TextField("Enter Name","",30,TextField.ANY);
rb=new ChoiceGroup("Option",Choice.EXCLUSIVE);
rb.append("Male",null);
rb.append("Female",null);
cb=new ChoiceGroup("Option",Choice.MULTIPLE);
cb.append("Red",null);
cb.append("Green",null);
list=new List("Menu:",List.IMPLICIT,options,null);
list.addCommand(next);
list.setCommandListener(this);
frm1.append(tb1);
frm1.append(rb);
frm1.append(cb);
frm1.addCommand(exit);
frm1.setCommandListener(this);
public void startApp()
{
display=Display.getDisplay(this);
display.setCurrent(list);
}
public void pauseApp(){ }
public void destroyApp(boolean b){ }
public void commandAction(Command c,Displayable d)
{
if(c==next)
{
alert=new Alert("Next Form","Moving To Next Form",null,null);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,frm1);
}
if(c==exit)
{
destroyApp(true);
notifyDestroyed();
}
}
}
Bouncing Ball
PROBLEM STATEMENT
Create an application to demonstrate Bouncing Ball, create a menu for the above with 2 options: start and stop to pause and resume the bouncing ball.
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BouncingBallMidlet extends MIDlet {
//Declaring BouncingBallCanvas object
private BouncingBallCanvas bbc;
//Declaring Display object
private Display display;
public BouncingBallMidlet()
{
//Initailizing BouncingBallCanvas object.
bbc = new BouncingBallCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(bbc);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
public class BouncingBallCanvas extends Canvas implements CommandListener, Runnable
{
BouncingBallMidlet bbm;
Display display;
Command quitCommand;
Command startCommand;
Command pauseCommand;
//Declaring Random’s object
Random rand;
//Declaring Thread’s object
Thread t;
int stopFlag;
int x, y;
int dx, dy;
int r, gr, b;
public BouncingBallCanvas()
{
}
public BouncingBallCanvas(BouncingBallMidlet bbm)
{
this.bbm = bbm;
// Indicating the object to be Displayed
display = Display.getDisplay(bbm);
//Initailizing Random’s object.
rand = new Random();
//Initailizing Command buttons
quitCommand = new Command("Quit", Command.EXIT, 0);
startCommand = new Command("Start", Command.SCREEN, 0);
pauseCommand = new Command("Pause", Command.STOP, 0);
//Adding Command to current form
addCommand(quitCommand);
addCommand(pauseCommand);
// Adding Command Listner
setCommandListener(this);
//Initailizing Thread’s object.
t = new Thread(this);
stopFlag = 0;
dx = 10;
dy = 10;
x = 8;
y = 12;
//Generating the Random number in the range of 0 to 255 and assigning it to the variable.
r = gr = b = Math.abs(rand.nextInt()%255);
//Starting the Thread t
t.start();
}
protected void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(r, gr, b);
g.fillArc(x , y, 20, 20, 0, 360);
if (x > getWidth()-23 || x < 8)
{
dx = -dx;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
if (y > getHeight()-21 || y < 12)
{
dy = -dy;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
x += dx;
y += dy;
}
//Condition to get and check the user input
Command getCurrentCommand()
{
switch(stopFlag)
{
case 0:
return pauseCommand;
case 1:
return startCommand;
default:
return startCommand;
}
}
public void commandAction(Command command, Displayabl displayable)
{
addCommand(getCurrentCommand());
if(command == quitCommand)
{
bbm.exitButton();
}
else if(command == startCommand)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(startCommand);
addCommand(pauseCommand);
t.start();
}
else if(command == pauseCommand)
{
stopFlag = 1;
t = null;
this.removeCommand(pauseCommand);
addCommand(startCommand);
}
}
public void run()
{
for( ; ; )
{
try
{
repaint();
//Thread is sleep for 170 mill second
Thread.sleep(170);
if(stopFlag == 1)
break;
}
catch(InterruptedException ie)
{
}
}
}
}
Create an application to demonstrate Bouncing Ball, create a menu for the above with 2 options: start and stop to pause and resume the bouncing ball.
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BouncingBallMidlet extends MIDlet {
//Declaring BouncingBallCanvas object
private BouncingBallCanvas bbc;
//Declaring Display object
private Display display;
public BouncingBallMidlet()
{
//Initailizing BouncingBallCanvas object.
bbc = new BouncingBallCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(bbc);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
public class BouncingBallCanvas extends Canvas implements CommandListener, Runnable
{
BouncingBallMidlet bbm;
Display display;
Command quitCommand;
Command startCommand;
Command pauseCommand;
//Declaring Random’s object
Random rand;
//Declaring Thread’s object
Thread t;
int stopFlag;
int x, y;
int dx, dy;
int r, gr, b;
public BouncingBallCanvas()
{
}
public BouncingBallCanvas(BouncingBallMidlet bbm)
{
this.bbm = bbm;
// Indicating the object to be Displayed
display = Display.getDisplay(bbm);
//Initailizing Random’s object.
rand = new Random();
//Initailizing Command buttons
quitCommand = new Command("Quit", Command.EXIT, 0);
startCommand = new Command("Start", Command.SCREEN, 0);
pauseCommand = new Command("Pause", Command.STOP, 0);
//Adding Command to current form
addCommand(quitCommand);
addCommand(pauseCommand);
// Adding Command Listner
setCommandListener(this);
//Initailizing Thread’s object.
t = new Thread(this);
stopFlag = 0;
dx = 10;
dy = 10;
x = 8;
y = 12;
//Generating the Random number in the range of 0 to 255 and assigning it to the variable.
r = gr = b = Math.abs(rand.nextInt()%255);
//Starting the Thread t
t.start();
}
protected void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(r, gr, b);
g.fillArc(x , y, 20, 20, 0, 360);
if (x > getWidth()-23 || x < 8)
{
dx = -dx;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
if (y > getHeight()-21 || y < 12)
{
dy = -dy;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
x += dx;
y += dy;
}
//Condition to get and check the user input
Command getCurrentCommand()
{
switch(stopFlag)
{
case 0:
return pauseCommand;
case 1:
return startCommand;
default:
return startCommand;
}
}
public void commandAction(Command command, Displayabl displayable)
{
addCommand(getCurrentCommand());
if(command == quitCommand)
{
bbm.exitButton();
}
else if(command == startCommand)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(startCommand);
addCommand(pauseCommand);
t.start();
}
else if(command == pauseCommand)
{
stopFlag = 1;
t = null;
this.removeCommand(pauseCommand);
addCommand(startCommand);
}
}
public void run()
{
for( ; ; )
{
try
{
repaint();
//Thread is sleep for 170 mill second
Thread.sleep(170);
if(stopFlag == 1)
break;
}
catch(InterruptedException ie)
{
}
}
}
}
To demonstrate “TIMER”
PROBLEM STATEMENT
Create an application to display text moving across the screen ad change the background along the moving text.
CODE
//MovingBannerMidlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MovingBannerMidlet extends MIDlet {
//Declaring object/s
MovingBannerCanvas mbc;
Display display;
public MovingBannerMidlet()
{
//Initailizing MovingBannerCanvas object.
mbc = new MovingBannerCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(mbc);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void exitButton()
{
// Notifying that the object is Destroyed
destroyApp(false);
notifyDestroyed();
}
}
//MovingBannerCanvas.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class MovingBannerCanvas extends Canvas implements CommandListener, Runnable {
//Declaring Objects and variables
MovingBannerMidlet mbm;
Display display;
Command cmdquit;
Command cmdstart;
Command cmdpause;
Random random;
String strmessage = " University Department of Information Technology ";
Thread t = null;
int stopFlag, x, y, z, x1, y1, z1;
public MovingBannerCanvas() { }
public MovingBannerCanvas(MovingBannerMidlet mbm)
{
this.mbm = mbm;
//Initailizing Objects and variables
display = Display.getDisplay(mbm);
cmdquit = new Command("Quit", Command.EXIT, 0);
cmdstart = new Command("Start", Command.SCREEN, 0);
cmdpause = new Command("Pause", Command.STOP, 1);
addCommand(cmdquit);
addCommand(cmdpause);
//Adding Command Listener
setCommandListener(this);
// initializing Random variable
random = new Random();
// initializing Thread to get process the current object
t = new Thread(this);
stopFlag = 0;
//Starting the execution of the thread process
t.start();
}
protected void paint(Graphics g)
{
g.setColor(x = Math.abs(random.nextInt()%255), y = Math.abs(random.nextInt()%255), z = Math.abs(random.nextInt()%255));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Math.abs(x1 = random.nextInt()%255), y1 = Math.abs(random.nextInt()%255), z1 = Math.abs(random.nextInt()%255));
if(x == x1 | y == y1 | z == z1)
{
g.setColor(Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255));
}
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString(strmessage, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER );
}
Command getCurrentCommand(){
switch(stopFlag)
{
case 0:
return cmdpause;
case 1:
return cmdstart;
default:
return cmdstart;
}
}
public void commandAction(Command command, Displayable displayable)
{
addCommand(getCurrentCommand());
if(command == cmdquit)
{
bm.exitButton();
}
else if(command == cmdstart)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(cmdstart);
addCommand(cmdpause);
t.start();
}
else if(command == cmdpause)
{
stopFlag = 1;
t = null;
this.removeCommand(cmdpause);
addCommand(cmdstart);
}
}
public void run()
{
char ch;
for( ; ; )
{
try
{
Rpaint();
Thread.sleep(250);
ch = strmessage.charAt(0);
strmessage = strmessage.substring(1, strmessage.length());
strmessage += ch;
if(stopFlag == 1)
break;
} catch(InterruptedException ie) {
}
}
}
}
Create an application to display text moving across the screen ad change the background along the moving text.
CODE
//MovingBannerMidlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MovingBannerMidlet extends MIDlet {
//Declaring object/s
MovingBannerCanvas mbc;
Display display;
public MovingBannerMidlet()
{
//Initailizing MovingBannerCanvas object.
mbc = new MovingBannerCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(mbc);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void exitButton()
{
// Notifying that the object is Destroyed
destroyApp(false);
notifyDestroyed();
}
}
//MovingBannerCanvas.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class MovingBannerCanvas extends Canvas implements CommandListener, Runnable {
//Declaring Objects and variables
MovingBannerMidlet mbm;
Display display;
Command cmdquit;
Command cmdstart;
Command cmdpause;
Random random;
String strmessage = " University Department of Information Technology ";
Thread t = null;
int stopFlag, x, y, z, x1, y1, z1;
public MovingBannerCanvas() { }
public MovingBannerCanvas(MovingBannerMidlet mbm)
{
this.mbm = mbm;
//Initailizing Objects and variables
display = Display.getDisplay(mbm);
cmdquit = new Command("Quit", Command.EXIT, 0);
cmdstart = new Command("Start", Command.SCREEN, 0);
cmdpause = new Command("Pause", Command.STOP, 1);
addCommand(cmdquit);
addCommand(cmdpause);
//Adding Command Listener
setCommandListener(this);
// initializing Random variable
random = new Random();
// initializing Thread to get process the current object
t = new Thread(this);
stopFlag = 0;
//Starting the execution of the thread process
t.start();
}
protected void paint(Graphics g)
{
g.setColor(x = Math.abs(random.nextInt()%255), y = Math.abs(random.nextInt()%255), z = Math.abs(random.nextInt()%255));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Math.abs(x1 = random.nextInt()%255), y1 = Math.abs(random.nextInt()%255), z1 = Math.abs(random.nextInt()%255));
if(x == x1 | y == y1 | z == z1)
{
g.setColor(Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255));
}
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString(strmessage, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER );
}
Command getCurrentCommand(){
switch(stopFlag)
{
case 0:
return cmdpause;
case 1:
return cmdstart;
default:
return cmdstart;
}
}
public void commandAction(Command command, Displayable displayable)
{
addCommand(getCurrentCommand());
if(command == cmdquit)
{
bm.exitButton();
}
else if(command == cmdstart)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(cmdstart);
addCommand(cmdpause);
t.start();
}
else if(command == cmdpause)
{
stopFlag = 1;
t = null;
this.removeCommand(cmdpause);
addCommand(cmdstart);
}
}
public void run()
{
char ch;
for( ; ; )
{
try
{
Rpaint();
Thread.sleep(250);
ch = strmessage.charAt(0);
strmessage = strmessage.substring(1, strmessage.length());
strmessage += ch;
if(stopFlag == 1)
break;
} catch(InterruptedException ie) {
}
}
}
}
To Demonstrate Multiple Forms
PROBLEM STATEMENT
Create an application to draw simple text and perform various operations:
1. Change the Background Color.
2. Change the Text Color (Foreground).
3. Changing the Font Style
4. Change the Font Size of displayed text.
CODE
import javax.microedition.
midlet.*;
import javax.microedition.lcdui.*;
public class Pracs1 extends MIDlet implements CommandListener
{
//Declearing Display object
private Display display;
//Declearing Command objects
private Command cmdexit;
private Command cmdchange;
private Command cmdfontcolor;
private Command cmdfontsize;
private Command cmdfontstyle;
//Declearing Form object
private Form frmMain;
//Declearing TextField object
public TextField txt1;
//Declearing back(User Define Class) object
private back b;
//Declearing size(User define Class) object
private size s;
//Declearing style(User Define Class) object
private style st;
//Declearing Fcolor(User Define Class) object
private Fcolor f;
public Pracs1()
{
//Initailizing Display object
display= Display.getDisplay(this);
//Initailizing TextField object
txt1=new TextField("Enter a valid string to be displayed","",40,TextField.ANY);
//Initailizing Command buttons
cmdchange= new Command("Change BG Color",Command.SCREEN,1);
cmdexit= new Command("Exit", Command.EXIT,2);
cmdfontcolor= new Command(" Change FontColor",Command.SCREEN,2);
cmdfontsize= new Command("Change FontSize", Command.SCREEN,2);
cmdfontstyle= new Command("Change FontStyle", Command.SCREEN,2);
//Initailizing Form
frmMain= new Form("Practical 1");
//Adding Commands to frmf1
frmMain.addCommand(cmdexit);
frmMain.addCommand(cmdfontcolor);
frmMain.addCommand(cmdfontsize);
frmMain.addCommand(cmdfontstyle);
frmMain.addCommand(cmdchange);
//Adding TextField to frmMain
frmMain.append(txt1);
//Setting the CommandListener to form f1
frmMain.setCommandListener(this);
b= new back(this);
s= new size (this);
st= new style(this);
f= new Fcolor(this);
}
public void startApp() throws MIDletStateChangeException
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void pauseApp()
{
}
public void destroyApp (boolean unconditional)
{
}
public void dgoback()
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void commandAction (Command c, Displayable d)
{
/*
Setting the object to be Displayed as per the user input */
if (c==cmdfontcolor)
{
display.setCurrent(f);
}
else if (c==cmdfontstyle)
{
display.setCurrent(st);
}
else if ( c==cmdexit)
{
// Notifying that the object is Destroyed
dgoback();
}
else if (c==cmdchange)
{
display.setCurrent(b);
}
else if(c==cmdfontsize)
{
display.setCurrent(s);
}
}
}
class back extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Commands buttons
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
//Declaring Pracs1 object
private Pracs1 p;
public back(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
// Drawing a Blank White Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
//Drawing the String on the Canvas
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
//Drawing a Rectangle for the Background as per use choice
switch(i)
{
case 1:
// Drawing a RED Rectangle for the Background
g.setColor(255,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);;
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
// Drawing a BLUE Rectangle for the Background
g.setColor(0,255,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
// Drawing a GREEN Rectangle for the Background
g.setColor(0,0,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class Fcolor extends Canvas implements CommandListener
{
private int i;
private Command cmdexit;
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
private Pracs1 p;
public Fcolor(Pracs1 p)
{
this.p= p;
//Initializing Exit Command buttons
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
switch(i)
{
case 1:
g.setColor(255,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,255,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,255);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class style extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdbold= new Command("BOLD", Command.SCREEN,1);
private Command cmditalic= new Command("ITALIC", Command.SCREEN,1);
private Command cmdunderlined= new Command("UNDERLINED", Command.SCREEN,1);
private Command cmdplain= new Command("PLAIN", Command.SCREEN,1);
//Delearing Pracs1 object
private Pracs1 p;
public style(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdbold);
addCommand(cmditalic);
addCommand(cmdunderlined);
addCommand(cmdplain);
//Adding Command Listener
setCommandListener(this);
}
public void paint (Graphics g)
{
// Drawing BLACK & WHITE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT STYLE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_ITALIC,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_UNDERLINED,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 4:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdbold)
{
i=1;
repaint();
}
else if (c==cmditalic)
{
i=2;
repaint();
}
else if (c==cmdunderlined)
{
i=3;
repaint();
}
else if (c==cmdplain)
{
i=4;
repaint();
}
}
}
class size extends Canvas implements CommandListener
{
private int i;
//Declaring exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdlarge= new Command("LARGE", Command.SCREEN,0);
private Command cmdmedium= new Command("MEDIUM", Command.SCREEN,0);
private Command cmdsmall= new Command("SMALL", Command.SCREEN,0);
//Declaring Pracs1 objects
private Pracs1 p;
public size(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdlarge);
addCommand(cmdmedium);
addCommand(cmdsmall);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT SIZE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
// * Setting the Background Color
g.setColor(0,0,0);
// Setting the FONT SIZE as per user choice
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdlarge)
{
i=1;
repaint();
}
else if (c==cmdmedium)
{
i=2;
repaint();
}
else if (c==cmdsmall)
{
i=3;
repaint();
}
}
}
Create an application to draw simple text and perform various operations:
1. Change the Background Color.
2. Change the Text Color (Foreground).
3. Changing the Font Style
4. Change the Font Size of displayed text.
CODE
import javax.microedition.
midlet.*;
import javax.microedition.lcdui.*;
public class Pracs1 extends MIDlet implements CommandListener
{
//Declearing Display object
private Display display;
//Declearing Command objects
private Command cmdexit;
private Command cmdchange;
private Command cmdfontcolor;
private Command cmdfontsize;
private Command cmdfontstyle;
//Declearing Form object
private Form frmMain;
//Declearing TextField object
public TextField txt1;
//Declearing back(User Define Class) object
private back b;
//Declearing size(User define Class) object
private size s;
//Declearing style(User Define Class) object
private style st;
//Declearing Fcolor(User Define Class) object
private Fcolor f;
public Pracs1()
{
//Initailizing Display object
display= Display.getDisplay(this);
//Initailizing TextField object
txt1=new TextField("Enter a valid string to be displayed","",40,TextField.ANY);
//Initailizing Command buttons
cmdchange= new Command("Change BG Color",Command.SCREEN,1);
cmdexit= new Command("Exit", Command.EXIT,2);
cmdfontcolor= new Command(" Change FontColor",Command.SCREEN,2);
cmdfontsize= new Command("Change FontSize", Command.SCREEN,2);
cmdfontstyle= new Command("Change FontStyle", Command.SCREEN,2);
//Initailizing Form
frmMain= new Form("Practical 1");
//Adding Commands to frmf1
frmMain.addCommand(cmdexit);
frmMain.addCommand(cmdfontcolor);
frmMain.addCommand(cmdfontsize);
frmMain.addCommand(cmdfontstyle);
frmMain.addCommand(cmdchange);
//Adding TextField to frmMain
frmMain.append(txt1);
//Setting the CommandListener to form f1
frmMain.setCommandListener(this);
b= new back(this);
s= new size (this);
st= new style(this);
f= new Fcolor(this);
}
public void startApp() throws MIDletStateChangeException
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void pauseApp()
{
}
public void destroyApp (boolean unconditional)
{
}
public void dgoback()
{
//Setting the object to be Displayed
display.setCurrent(frmMain);
}
public void commandAction (Command c, Displayable d)
{
/*
Setting the object to be Displayed as per the user input */
if (c==cmdfontcolor)
{
display.setCurrent(f);
}
else if (c==cmdfontstyle)
{
display.setCurrent(st);
}
else if ( c==cmdexit)
{
// Notifying that the object is Destroyed
dgoback();
}
else if (c==cmdchange)
{
display.setCurrent(b);
}
else if(c==cmdfontsize)
{
display.setCurrent(s);
}
}
}
class back extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Commands buttons
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
//Declaring Pracs1 object
private Pracs1 p;
public back(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
// Drawing a Blank White Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
//Drawing the String on the Canvas
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
//Drawing a Rectangle for the Background as per use choice
switch(i)
{
case 1:
// Drawing a RED Rectangle for the Background
g.setColor(255,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);;
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
// Drawing a BLUE Rectangle for the Background
g.setColor(0,255,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
// Drawing a GREEN Rectangle for the Background
g.setColor(0,0,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class Fcolor extends Canvas implements CommandListener
{
private int i;
private Command cmdexit;
private Command cmdRED = new Command("RED", Command.SCREEN, 1);
private Command cmdGREEN = new Command("GREEN", Command.SCREEN, 1);
private Command cmdBLUE = new Command("BLUE", Command.SCREEN, 1);
private Pracs1 p;
public Fcolor(Pracs1 p)
{
this.p= p;
//Initializing Exit Command buttons
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command button to the frmf1
addCommand(cmdexit);
addCommand(cmdRED);
addCommand(cmdGREEN);
addCommand(cmdBLUE);
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
switch(i)
{
case 1:
g.setColor(255,0,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,255,0);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,255);
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdRED)
{
i=1;
repaint();
}
else if (c==cmdGREEN)
{
i=2;
repaint();
}
else if (c==cmdBLUE)
{
i=3;
repaint();
}
}
}
class style extends Canvas implements CommandListener
{
private int i;
//Declaring Exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdbold= new Command("BOLD", Command.SCREEN,1);
private Command cmditalic= new Command("ITALIC", Command.SCREEN,1);
private Command cmdunderlined= new Command("UNDERLINED", Command.SCREEN,1);
private Command cmdplain= new Command("PLAIN", Command.SCREEN,1);
//Delearing Pracs1 object
private Pracs1 p;
public style(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdbold);
addCommand(cmditalic);
addCommand(cmdunderlined);
addCommand(cmdplain);
//Adding Command Listener
setCommandListener(this);
}
public void paint (Graphics g)
{
// Drawing BLACK & WHITE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT STYLE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_ITALIC,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_UNDERLINED,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 4:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdbold)
{
i=1;
repaint();
}
else if (c==cmditalic)
{
i=2;
repaint();
}
else if (c==cmdunderlined)
{
i=3;
repaint();
}
else if (c==cmdplain)
{
i=4;
repaint();
}
}
}
class size extends Canvas implements CommandListener
{
private int i;
//Declaring exit Command button
private Command cmdexit;
//Initailizing Command buttons
private Command cmdlarge= new Command("LARGE", Command.SCREEN,0);
private Command cmdmedium= new Command("MEDIUM", Command.SCREEN,0);
private Command cmdsmall= new Command("SMALL", Command.SCREEN,0);
//Declaring Pracs1 objects
private Pracs1 p;
public size(Pracs1 p)
{
this.p= p;
//Initailizing exit Command button
cmdexit= new Command("Back", Command.EXIT,0);
//Adding Command buttons to the frmf1
addCommand(cmdexit);
addCommand(cmdlarge);
addCommand(cmdmedium);
addCommand(cmdsmall);
// Adding Command Listner
setCommandListener(this);
}
protected void paint (Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
/*
* Setting the Background Color
* Setting the FONT SIZE as per user choice
* Drawing the String on the Canvas
*/
switch(i)
{
case 1:
// * Setting the Background Color
g.setColor(0,0,0);
// Setting the FONT SIZE as per user choice
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_LARGE));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 2:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
case 3:
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL));
g.drawString(p.txt1.getString(),120,150,Graphics.TOP|Graphics.RIGHT);
break;
default:
break;
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.dgoback();
}
else if (c==cmdlarge)
{
i=1;
repaint();
}
else if (c==cmdmedium)
{
i=2;
repaint();
}
else if (c==cmdsmall)
{
i=3;
repaint();
}
}
}
Subscribe to:
Posts (Atom)