embedded lab file
TRANSCRIPT
-
8/14/2019 Embedded Lab File
1/19
Embedded Systems
Lab File
Made By:
Atishay Jain
10783017
Harsh Dhaka10603039
Mayank Gupta
10603055
-
8/14/2019 Embedded Lab File
2/19
Contents
Installation of j2me setup...........................................................................................2
Installation of netbeans as ide for program development and explore all the present
options.......................................................................................................................2Hands 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.
-
8/14/2019 Embedded Lab File
3/19
Installation of j2me setup
Below are the instructions to install and Setup J2ME in Windows XP SP2:
i) Install Java SDK. Double Click on Sun Java SDK installer package.
ii) Click Accept to accept the license agreement.
iii) Click Next after selecting packages to install.
iv) Click Next for directory selection.
v) Click Finish to accept the Java SDK and JRE installation.
vi) Double click on Sun Java Mobile SDK Toolkit setup.
vii) Click Accept to accept the license agreement. Click Next.
viii) Accept the location of JDK selected, if unavailable find JDK. Click next to select thedirectory
ix) Click next after update selection.
x) Unblock the firewall.
xi) Click Finish. J2ME is installed.
Installation of netbeans as ide for program development
and explore all the present options1. To install the NetBeans IDE:
2. Once you have downloaded the installer file, double-click the installer'sicon to launch the installer.
3. In the install wizard, respond to the License Agreement, then clickNext.
4. Specify an empty directory within which to install NetBeans IDE.Note: This NetBeans IDE will not disrupt the settings of your otherNetBeans installations because the IDE automatically creates a newuser directory when launched (${HOME}/.netbeans/5.5).
5. Choose the JDK you want the IDE to use from the list of suitablechoices 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.
-
8/14/2019 Embedded Lab File
4/19
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");
-
8/14/2019 Embedded Lab File
5/19
mainForm.append ("Select a text box type");
// the string elements will have no imagesImage[] imageArray = null;
types = new ChoiceGroup ("Choose type", Choice.EXCLUSIVE, textBoxLabels,imageArray);
mainForm.append (types);
// advanced optionsString[] 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]) {
-
8/14/2019 Embedded Lab File
6/19
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) import javax.microedition.lcdui.*; & import javax.microedition.midlet.MIDlet;: Import J2Me packages to run.
ii) MIDlet: Root class of a J2ME Application. All J2Me applications extend this.iii) 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 aCommandListener. A CommandListener monitors user events with a screen andcauses the appropriate code to execute based on the current event. Commands thatshow in the bottom bar include OK, EXIT etc.
Functions Used:
-
8/14/2019 Embedded Lab File
7/19
-
8/14/2019 Embedded Lab File
8/19
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 commandprivate 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();}}
}
-
8/14/2019 Embedded Lab File
9/19
Write a program to create a set of Radio Buttons and
Select a Desired One.import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import 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 voidstartApp() {
display.setCurrent(list);
}
public voidpauseApp() {
}
public voiddestroyApp(boolean unconditional) {
}
public voidcommandAction(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);
-
8/14/2019 Embedded Lab File
10/19
list.removeCommand(submit);
} else if (command == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
A set of check boxes.
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Form;import javax.microedition.lcdui.StringItem;
import 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);
}
-
8/14/2019 Embedded Lab File
11/19
public voidstartApp() {
display.setCurrent(form);
}
public voidpauseApp() {
}
public voiddestroyApp(boolean unconditional) {
}
public voidcommandAction(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 java.io.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import 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
-
8/14/2019 Embedded Lab File
12/19
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 voidstartApp() {
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 voidcommandAction(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
-
8/14/2019 Embedded Lab File
13/19
private static final int MAX_TEXT_SIZE = 64;
// The TextBox
protectedTextBox textBox;
// The MIDlet's Display object
protectedDisplay display;
// Flag indicating first call of startApp
protected boolean started;
protected voidstartApp() {
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 voidpauseApp() {
}
-
8/14/2019 Embedded Lab File
14/19
protected voiddestroyApp(boolean unconditional) {
}
}
Draw an outlined rectangle and filled rectangle.
Filled Rectangle
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import 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 voidstartApp() {
display.setCurrent(canvas);
}
protected voidpauseApp() {
}
protected voiddestroyApp(boolean unconditional) {
}
public voidexitMIDlet() {
destroyApp(true);
notifyDestroyed();
}
}
class MyCanvas extends Canvas implements CommandListener {
-
8/14/2019 Embedded Lab File
15/19
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 voidpaint(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 voidcommandAction(Command command, Displayable displayable) {
if (command == exit) {
filledRectangleExample.exitMIDlet();
}
}
}
Rectangle Boundary
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import 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);
}
-
8/14/2019 Embedded Lab File
16/19
protected voidstartApp() {
display.setCurrent(canvas);
}
protected voidpauseApp() {
}
protected voiddestroyApp(boolean unconditional) {
}
public voidexitMIDlet() {
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 voidpaint(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 voidcommandAction(Command command, Displayable displayable) {
if (command == exit) {
rectangleExample.exitMIDlet();
}
}}
-
8/14/2019 Embedded Lab File
17/19
Draw an outlined circle and filled Circle.
Outline only
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import 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 voidstartApp() {
display.setCurrent(canvas);
}
protected voidpauseApp() {
}
protected voiddestroyApp(boolean unconditional) {
}
public voidexitMIDlet() {
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);
-
8/14/2019 Embedded Lab File
18/19
setCommandListener(this);
}
protected voidpaint(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 voidcommandAction(Command command, Displayable displayable) {
if (command == exit) {
arcExample.exitMIDlet();
}
}
}
Filled Arc
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import 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 voidstartApp() {
display.setCurrent(canvas);}
protected voidpauseApp() {
}
protected voiddestroyApp(boolean unconditional) {
}
-
8/14/2019 Embedded Lab File
19/19
public voidexitMIDlet() {
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 voidpaint(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 voidcommandAction(Command command, Displayable displayable) {
if (command == exit) {
arcFilledExample.exitMIDlet();
}}
}