Embedded Systems Lab File
Made By: Atishay Jain 10783017 Harsh Dhaka 10603039 Mayank Gupta 10603055
Contents
Installation of j2me setup...........................................................................................2 Installation of netbeans as ide for program development and explore all the present options.......................................................................................................................2 Hands on Various predefined examples defined in J2ME environment.......................2 Printing a simple Line “Hello World” in J2ME Environment using CLDC Toolkit..........6 Write a program to create a set of Radio Buttons and Select a Desired One.............7 Draw an outlined rectangle and filled rectangle.......................................................12 Draw an outlined circle and filled Circle...................................................................15 Draw an outlined circle and filled Circle.
Installation of j2me setup Below are the instructions to install and Setup J2ME in Windows XP SP2: i) ii) iii) iv)
v) vi) vii) viii)
ix) x) xi)
Install Java SDK. Double Click on Sun Java SDK installer package. Click Accept to accept the license agreement. Click Next after selecting packages to install. Click Next for directory selection. Click Finish to accept the Java SDK and JRE installation. Double click on Sun Java Mobile SDK Toolkit setup. Click Accept to accept the license agreement. Click Next. Accept the location of JDK selected, if unavailable find JDK. Click next to select the directory Click next after update selection. Unblock the firewall. Click Finish. J2ME is installed.
Installation of netbeans as ide for program development and explore all the present options 1. To install the NetBeans IDE:
2. Once you have downloaded the installer file, double-click the installer's icon to launch the installer. 3. In the install wizard, respond to the License Agreement, then click Next. 4. Specify an empty directory within which to install NetBeans IDE. Note: This NetBeans IDE will not disrupt the settings of your other NetBeans installations because the IDE automatically creates a new user directory when launched (${HOME}/.netbeans/5.5). 5. Choose the JDK you want the IDE to use from the list of suitable choices in the list, then click Next. 6. Verify that the installation location is correct and that you have adequate space on your system for the installation. 7. Click Next to begin the installation. 8. Click finish. Netbeans is installed.
Hands on Various predefined examples defined in J2ME environment.
Selected Program: UIDemo->TextBoxDemo.java package textbox; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; /** * The textbox demo displays a list of all the text box types and allows the * user to select a specific type of text box to try. * * @version 2.0 */ public class TextBoxDemo extends MIDlet implements CommandListener { private static final Command CMD_EXIT = new Command ("Exit", Command.EXIT, 1); private static final Command CMD_BACK = new Command ("Back", Command.BACK, 1); private static final Command CMD_SHOW = new Command ("Show", Command.SCREEN, 1); /** * The labels for the supported textboxs. */ static final String[] textBoxLabels = {"Any Character", "E-Mail", "Number", "Decimal", "Phone", "Url"}; /** * The supported textbox types. */ static final int[] textBoxTypes = { TextField.ANY, TextField.EMAILADDR, TextField.NUMERIC, TextField.DECIMAL, TextField.PHONENUMBER, TextField.URL }; private Display display; private ChoiceGroup types; private ChoiceGroup options; private Form mainForm; private boolean firstTime; public TextBoxDemo () { display = Display.getDisplay (this); firstTime = true; } protected void startApp () { if (firstTime) { mainForm = new Form ("Select a Text Box Type");
mainForm.append ("Select a text box type"); // the string elements will have no images Image[] imageArray = null; types = new ChoiceGroup ("Choose type", Choice.EXCLUSIVE, textBoxLabels, imageArray); mainForm.append (types); // advanced options String[] optionStrings = {"As Password", "Show Ticker"}; options = new ChoiceGroup ("Options", Choice.MULTIPLE, optionStrings, null); mainForm.append (options); mainForm.addCommand (CMD_SHOW); mainForm.addCommand (CMD_EXIT); mainForm.setCommandListener (this); firstTime = false; } display.setCurrent (mainForm); } protected void destroyApp (boolean unconditional) { } protected void pauseApp () { } public void commandAction (Command c, Displayable d) { if (c == CMD_EXIT) { destroyApp (false); notifyDestroyed (); } else if (c == CMD_SHOW) { int index = types.getSelectedIndex (); String title = textBoxLabels[index]; int choiceType = textBoxTypes[index]; boolean[] flags = new boolean[2]; options.getSelectedFlags (flags); if (flags[0]) { choiceType |= TextField.PASSWORD; } TextBox textBox = new TextBox (title, "", 50, choiceType); if (flags[1]) {
textBox.setTicker (new Ticker ("TextBox: " + title)); } textBox.addCommand (CMD_BACK); textBox.setCommandListener (this); display.setCurrent (textBox); } else if (c == CMD_BACK) { display.setCurrent (mainForm); } } } Important Commands: i) ii) iii)
import javax.microedition.lcdui.*; & import javax.microedition.midlet.MIDlet; : Import J2Me packages to run. MIDlet: Root class of a J2ME Application. All J2Me applications extend this. CommandListener: A Command object is used to present a user with a selection of options to choose from when a screen is displayed. Each screen must have a CommandListener. A CommandListener monitors user events with a screen and causes the appropriate code to execute based on the current event. Commands that show in the bottom bar include OK, EXIT etc.
Functions Used:
Printing a simple Line “Hello World” in J2ME Environment using CLDC Toolkit. package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloMIDlet extends MIDlet implements CommandListener { private Command exitCommand; // The exit command private Display display; // The display for this MIDlet public HelloMIDlet() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.EXIT, 0); } public void startApp() { TextBox t = new TextBox("Hello", "Hello, Thapar University-Atishay, Shreshtha, Harsh!", 256, 0); t.addCommand(exitCommand); t.setCommandListener(this); display.setCurrent(t); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } }
Write a program to create a set of Radio Buttons and Select a Desired One. import import import import import import import import
javax.microedition.lcdui.Alert; javax.microedition.lcdui.AlertType; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.List; javax.microedition.midlet.MIDlet;
public class ListRadioButtons extends MIDlet implements CommandListener { private Display display; private Command exit = new Command("Exit", Command.EXIT, 1); private Command submit = new Command("Submit", Command.SCREEN, 2); private List list = new List("Select one", List.EXCLUSIVE); public ListRadioButtons() { display = Display.getDisplay(this); list.append("Male", null); list.append("Female", null); list.addCommand(exit); list.addCommand(submit); list.setCommandListener(this); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable Displayable) { if (command == submit) { Alert alert = new Alert("Choice", list.getString(list.getSelectedIndex( )), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert);
list.removeCommand(submit); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } } }
A set of check boxes. import import import import import import import import import
javax.microedition.lcdui.Choice; javax.microedition.lcdui.ChoiceGroup; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.Form; javax.microedition.lcdui.StringItem; javax.microedition.midlet.MIDlet;
public class CheckBoxes extends MIDlet implements CommandListener { private Display display; private Form form = new Form("Movies"); private Command exit = new Command("Exit", Command.EXIT, 1); private Command process = new Command("Process", Command.SCREEN, 2); private ChoiceGroup movies = new ChoiceGroup("Select Movies You Like to See ", Choice.MULTIPLE); private int movieIndex; public CheckBoxes() { display = Display.getDisplay(this); movies.append("A", null); movies.append("B", null); movies.append("C", null); movies.append("D", null); movieIndex = form.append(movies); form.addCommand(exit); form.addCommand(process); form.setCommandListener(this); }
public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == process) { boolean picks[] = new boolean[movies.size()]; StringItem message[] = new StringItem[movies.size()]; movies.getSelectedFlags(picks); for (int i = 0; i < picks.length; i++) { if (picks[i]) { message[i] = new StringItem("", movies.getString(i) + "\n"); form.append(message[i]); } } form.delete(movieIndex); form.removeCommand(process); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } } }
A set of text boxes. import import import import
java.io.*; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class TextBox2MIDlet extends TextBoxMIDlet implements CommandListener { // Exit command private static final Command EXIT_COMMAND = new Command("Exit", Command.EXIT, 0); // OK command
private static final Command OK_COMMAND = new Command("OK", Command.OK, 0); // Clear text box content private static final Command CLEAR_COMMAND = new Command("Clear", Command.SCREEN, 1); // Reverse the content of the text box private static final Command REVERSE_COMMAND = new Command("Reverse", Command.SCREEN, 1); protected void startApp() { boolean firstTime = !started; super.startApp(); // If this is the first execution // of startApp, install commands if (firstTime) { textBox.addCommand(OK_COMMAND); textBox.addCommand(EXIT_COMMAND); textBox.addCommand(CLEAR_COMMAND); textBox.addCommand(REVERSE_COMMAND); textBox.setCommandListener(this); } } // Command implementations. public void commandAction(Command c, Displayable d) { if (c == EXIT_COMMAND) { destroyApp(true); notifyDestroyed(); } else if (c == OK_COMMAND) { System.out.println("OK pressed"); } else if (c == CLEAR_COMMAND) { textBox.setString(null); } else if (c == REVERSE_COMMAND) { String str = textBox.getString(); if (str != null) { StringBuffer sb = new StringBuffer(str); textBox.setString(sb.reverse().toString()); } } } } class TextBoxMIDlet extends MIDlet { // Maximum size of the text in the TextBox
private static final int MAX_TEXT_SIZE = 64; // The TextBox protected TextBox textBox; // The MIDlet's Display object protected Display display; // Flag indicating first call of startApp protected boolean started; protected void startApp() { if (!started) { // First time through - initialize // Get the text to be displayed String str = null; try { InputStream is = getClass().getResourceAsStream("test.txt"); InputStreamReader r = new InputStreamReader(is); char[] buffer = new char[32]; StringBuffer sb = new StringBuffer(); int count; while ((count = r.read(buffer, 0, buffer.length)) > -1) { sb.append(buffer, 0, count); } str = sb.toString(); } catch (IOException ex) { str = "Failed to load text"; } // Create the TextBox textBox = new TextBox("TextBox Example", str, MAX_TEXT_SIZE, TextField.ANY); // Create a ticker and install it Ticker ticker = new Ticker("This is a ticker..."); textBox.setTicker(ticker); // Install the TextBox as the current screen display = Display.getDisplay(this); display.setCurrent(textBox); started = true; } } protected void pauseApp() { }
protected void destroyApp(boolean unconditional) { } }
Draw an outlined rectangle and filled rectangle. Filled Rectangle import import import import import import import
javax.microedition.lcdui.Canvas; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.Graphics; javax.microedition.midlet.MIDlet;
public class FilledRectangleExample extends MIDlet { private Display display; private MyCanvas canvas; public FilledRectangleExample() { display = Display.getDisplay(this); canvas = new MyCanvas(this); } protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); } } class MyCanvas extends Canvas implements CommandListener {
private Command exit; private FilledRectangleExample filledRectangleExample; public MyCanvas(FilledRectangleExample filledRectangleExample) { this.filledRectangleExample = filledRectangleExample; exit = new Command("Exit", Command.EXIT, 1); addCommand(exit); setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255, 255, 255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(0, 0, 255); graphics.fillRect(2, 2, 20, 20); graphics.fillRoundRect(20, 20, 60, 60, 15, 45); } public void commandAction(Command command, Displayable displayable) { if (command == exit) { filledRectangleExample.exitMIDlet(); } } }
Rectangle Boundary import import import import import import import
javax.microedition.lcdui.Canvas; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.Graphics; javax.microedition.midlet.MIDlet;
public class RectangleExample extends MIDlet { private Display display; private MyCanvas canvas; public RectangleExample() { display = Display.getDisplay(this); canvas = new MyCanvas(this); }
protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); } } class MyCanvas extends Canvas implements CommandListener { private Command exit = new Command("Exit", Command.EXIT, 1); private RectangleExample rectangleExample; public MyCanvas(RectangleExample rectangleExample) { this.rectangleExample = rectangleExample; addCommand(exit); setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255, 255, 255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255, 0, 0); graphics.drawRect(2, 2, 20, 20); graphics.drawRoundRect(20, 20, 60, 60, 15, 45); } public void commandAction(Command command, Displayable displayable) { if (command == exit) { rectangleExample.exitMIDlet(); } } }
Draw an outlined circle and filled Circle. Outline only import import import import import import import
javax.microedition.lcdui.Canvas; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.Graphics; javax.microedition.midlet.MIDlet;
public class ArcExampleMIDlet extends MIDlet { private Display display; private MyCanvas canvas; public ArcExampleMIDlet() { display = Display.getDisplay(this); canvas = new MyCanvas(this); } protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); } } class MyCanvas extends Canvas implements CommandListener { private Command exit; private ArcExampleMIDlet arcExample; public MyCanvas(ArcExampleMIDlet arcExample) { this.arcExample = arcExample; exit = new Command("Exit", Command.EXIT, 1); addCommand(exit);
setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255, 255, 255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255, 0, 0); graphics.drawArc(0, 0, getWidth(), getHeight(), 180, 180); } public void commandAction(Command command, Displayable displayable) { if (command == exit) { arcExample.exitMIDlet(); } } }
Filled Arc import import import import import import import
javax.microedition.lcdui.Canvas; javax.microedition.lcdui.Command; javax.microedition.lcdui.CommandListener; javax.microedition.lcdui.Display; javax.microedition.lcdui.Displayable; javax.microedition.lcdui.Graphics; javax.microedition.midlet.MIDlet;
public class ArcFilledExample extends MIDlet { private Display display; private MyCanvas canvas; public ArcFilledExample() { display = Display.getDisplay(this); canvas = new MyCanvas(this); } protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { }
public void exitMIDlet() { destroyApp(true); notifyDestroyed(); } } class MyCanvas extends Canvas implements CommandListener { private Command exit; private ArcFilledExample arcFilledExample; public MyCanvas(ArcFilledExample arcFilledExample) { this.arcFilledExample = arcFilledExample; exit = new Command("Exit", Command.EXIT, 1); addCommand(exit); setCommandListener(this); } protected void paint(Graphics graphics) { graphics.setColor(255, 255, 255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255, 0, 0); graphics.fillArc(0, 0, getWidth(), getHeight(), 180, 180); } public void commandAction(Command command, Displayable displayable) { if (command == exit) { arcFilledExample.exitMIDlet(); } } }