Kamis, 01 Januari 2009

Contoh Program User Login

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class cthForm extends MIDlet implements CommandListener
{
Form form;
Display display;
TextField user;
TextField password;
Command cancel;
Command ok;
Alert txtpesan;

public cthForm()
{
user = new TextField("UserName:", "", 10, TextField.ANY);
password = new TextField("Password:", "", 10, TextField.PASSWORD);
form = new Form("Sign in");
cancel = new Command("Cancel", Command.CANCEL, 2);
ok = new Command("Login", Command.OK, 2);
}

public void startApp()
{
display = Display.getDisplay(this);
form.append(user);
form.append(password);
form.addCommand(cancel);
form.addCommand(ok);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp() {}

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

public void validateUser(String name, String password)
{
if (name.equals("ima") && password.equals("111"))
{
txtpesan=new Alert("Alert!!");
txtpesan.setString("Selamat Datang");
display.setCurrent(txtpesan);
}
else
{
error();
}
}



public void error()
{
Alert error = new Alert("Login Incorrect", "Login Anda Gagal!!!", null, AlertType.ERROR);
error.setTimeout(Alert.FOREVER);
user.setString("");
password.setString("");
display.setCurrent(error, form);
}

public void commandAction(Command c, Displayable d)
{
if(c==cancel)
{
destroyApp(true);
}
else if(c==ok)
{
validateUser(user.getString(), password.getString());
}
}
}

Contoh Program List Implicit

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class cthImplicit extends MIDlet implements CommandListener
{
private List menupil;
private Command cmdExit;
private Display display;

public cthImplicit(){
display=Display.getDisplay(this);

menupil=new List("Pilihan",List.IMPLICIT);
menupil.append("VIP",null);
menupil.append("Bisnis",null);
menupil.append("Ekonomi",null);
menupil.append("Keluar",null);
menupil.setCommandListener(this);
}

public void startApp(){
display.setCurrent(menupil);
}

public void pauseApp(){}
public void destroyApp(boolean unconditional){}

public void commandAction(Command c, Displayable d){
if(menupil.isSelected(3)){
destroyApp(false);
notifyDestroyed();
}
}
}

Contoh Program Text Box

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class cthTextBox extends MIDlet implements CommandListener
{
Display display;
Alert txtpesan;
Command cmdExit;
Command cmdOk;
TextField txtnama;
TextField txtpass;
Form Form;

public cthTextBox() {}
public void startApp()
{
Form = new Form("UserLogin");
txtnama = new TextField("Nama","",50,TextField.ANY);
txtpass = new TextField("Password","",50,TextField.PASSWORD);
cmdExit=new Command("Exit", Command.EXIT,0);
cmdOk=new Command("Ok", Command.OK,0);
Form.addCommand(cmdExit);
Form.addCommand(cmdOk);
Form.append(txtnama);
Form.append(txtpass);
display=Display.getDisplay(this);
display.setCurrent(Form);
Form.setCommandListener(this);
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable d)
{
if(c==cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
else if(c==cmdOk)
{
txtpesan=new Alert("Pesan");
txtpesan.setString("Data Telah Disimpan");
display.setCurrent(txtpesan);
}
}
}

Contoh Program Canvas

MIDLET

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class MenuCanvas extends MIDlet implements CommandListener
{
private Display display;
private Command cmdExit;
private Canvas keyCanvas;
public void startApp()
{
display=Display.getDisplay(this);
cmdExit=new Command("EXIT",Command.EXIT,1);
keyCanvas=new KeyCanvas();
keyCanvas.addCommand(cmdExit);
keyCanvas.setCommandListener(this);
display.setCurrent(keyCanvas);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}

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

CANVAS

import javax.microedition.lcdui.*;

public class KeyCanvas extends Canvas {
private Font mFont;
private String mMessage = "[Press keys]";
private int x=41;
private int y=115;
private static int w,h;

public KeyCanvas() {
mFont = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
}

public void paint(Graphics g) {
w = getWidth();
h = getHeight();

// Clear the Canvas.
g.setGrayScale(255);
g.fillRect(0, 0, w - 1, h - 1);
g.setGrayScale(0);
g.drawRect(0, 0, w - 1, h - 1);

g.setFont(mFont);

g.setColor(0,0,200);
g.fillRect(0,0,w,h);
g.setColor(255,255,255);
g.drawRoundRect(x,y,150,70,40,40);
g.drawString("Tugas J2ME Key Canvas",(x+17),(y+28),g.TOP|g.LEFT);
}

protected void keyPressed(int keyCode) {

int gameAction = getGameAction(keyCode);
switch(gameAction) {
case UP:
y=y-8;
if(y<=0)
y=0;
break;
case DOWN:
y=y+8;
if(y>=218)
y=218;
break;
case LEFT:
x=x-8;
if(x<=0)
x=0;
break;
case RIGHT:
x=x+8;
if(x>=89)
x=89;
break;
}
repaint();
}
}


Contoh Program ChoiceGroup

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class cthChoiceGroup extends MIDlet implements CommandListener
{

private Display display;
private List list;
private Form form;
private ChoiceGroup menu;

private String[] daftarMenu = {
"Pilihan Pertama",
"Pilihan Kedua",
"Pilihan Ketiga",
"Pilihan Keempat",
};

private final Command cmdKeluar= new Command ("Keluar", Command.EXIT,1);
private final Command cmdOK= new Command ("OK", Command.OK,1);
private final Command cmdKembali= new Command ("Kelmbali", Command.BACK,1);

public cthChoiceGroup() {
display = Display.getDisplay(this);
form = new Form("Contoh ChoiceGroup");
form.addCommand(cmdKembali);
form.setCommandListener(this);
}

public void startApp() {
list = new List("Contoh ChoiceGroup", Choice.IMPLICIT);
list.append("Tipe EXCLUSIVE", null);
list.append("Tipe MULTIPLE", null);
list.append("Tipe POPUP", null);
list.addCommand(cmdKeluar);
list.addCommand(cmdOK);
list.setCommandListener(this);

display.setCurrent(list);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){}

public void commandAction (Command c, Displayable s){
if (c==cmdKeluar){
destroyApp(false);
notifyDestroyed();
} else if (c==cmdKembali){
display.setCurrent(list);
} else {
menu = null;
switch (list.getSelectedIndex()) {
case 0 : {
menu = new ChoiceGroup("Tipe EXCLUSIVE", Choice.EXCLUSIVE, daftarMenu, null);
break;
}
case 1 : {
menu = new ChoiceGroup("Tipe MULTIPLE", Choice.MULTIPLE, daftarMenu, null);
break;
}
case 2 : {
menu = new ChoiceGroup("Tipe POPUP", Choice.POPUP, daftarMenu, null);
break;
}
}
form.deleteAll();
form.append(menu);
display.setCurrent(form);
}
}
}

Contoh Program Buku Alamat

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

public class bukualamat extends MIDlet implements CommandListener
{
private Display display = null; // Deklarasi variabel untuk Display.
Command search = null; // Deklarasi variabel untuk Button Search.
Command quit = null; // Deklarasi variabel untuk Button keluar.
Command delete = null; // Deklarasi variabel untuk Button hapus.
Command addnow = null; // Deklarasi variabel untuk Button tambah.
Command mainmenu = null; // Deklarasi variabel untuk Button kembali ke menu utama.
List menu = null; // Deklarasi variabel untuk Menu List
Form ui_form = null;
StringItem si = null;
TextField name = null; // TextField untuk Nama
TextField phone = null; // TextField untuk No Telp

RecordStore recordStore = null; // Deklarasi Database
public bukualamat()
{
display = Display.getDisplay(this); // Inisialisasi Display.
quit = new Command("Keluar",Command.SCREEN,3); // Inisialisasi Button..
search = new Command("Cari",Command.SCREEN,2); // Inisialisasi Button..
delete = new Command("Hapus",Command.SCREEN,2); // Inisialisasi Button..
addnow = new Command("Tambah",Command.SCREEN,2); // Inisialisasi Button..
mainmenu = new Command("Utama",Command.SCREEN,2); // Inisialisasi Button..
// Inisialisasi Record Store
try
{
recordStore = RecordStore.openRecordStore("buku", true);
}
catch(RecordStoreException rse)
{
rse.printStackTrace();
}
}


public void startApp()
{
menu = new List("Buku Alamat...",List.IMPLICIT);
menu.append("1. Cari Data",null);
menu.append("2. Tambah Data",null);
menu.append("3. Hapus Data",null);
menu.append("4. Keluar",null);
menu.setCommandListener(this);
display.setCurrent(menu);
}
// Tampilan Layar
void searchScreen()
{
ui_form = new Form("Mencari Alamat");
name = new TextField("Nama yang dicari..","",50,0);
ui_form.append(name);
ui_form.addCommand(search); // Menambahkan Button ui_form.
ui_form.addCommand(quit); // Menambahkan Button ui_form.
ui_form.setCommandListener(this); // Action Listener..

display.setCurrent(ui_form);
}
void addScreen()
{
ui_form = new Form("Tambah Data..");
name = new TextField("Nama ..","",50,0);
ui_form.append(name);
phone = new TextField("No. Telp.. ","",50,0);
ui_form.append(phone);
ui_form.addCommand(addnow); // Menambahkan Button ui_form
ui_form.addCommand(quit); // Menambahkan Button ui_form
ui_form.setCommandListener(this); // Action Listener..
display.setCurrent(ui_form);
}
// GUI untuk Delete Screen.......
void deleteScreen()
{
ui_form = new Form("Hapus Alamat");
name = new TextField("Nama Yang Akan DiHapus..","",50,0);
ui_form.append(name);
ui_form.addCommand(delete); // Menambahkan Button ui_form
ui_form.addCommand(quit); // Menambahkan Button ui_form
ui_form.setCommandListener(this); // Action Listener..
display.setCurrent(ui_form);
}
public void pauseApp()
{
menu = null;
}
public void destroyApp(boolean unconditional)
{
menu = null;
notifyDestroyed(); // Destroy...
}
public void commandAction(Command c, Displayable d) // Event untuk Button..
{
if ( c == quit ) // Apakah Keluar yang dipilih
{
try
{
close();
}
catch (RecordStoreException rse)
{
rse.printStackTrace();
}
destroyApp(true);
}
else if (c == search) // Jika yang dipilih tombol search
{
String temp_search = name.getString();
search_add(temp_search);
}
else if (c == mainmenu) // Kembali ke menu Utama
{
startApp();
}
else if (c == delete) // Jika Memilih menu delete
{
String temp_delete = name.getString();
delete_add(temp_delete);
}
else if (c == addnow) // Jiak menu tambah di pilih
{
String temp_name = name.getString();
String temp_phone = phone.getString();
alamat_add(temp_name, temp_phone);
}
else
{
List down = (List)display.getCurrent();
switch(down.getSelectedIndex())
{
case 0: searchScreen();break;
case 1: addScreen();break;
case 2: deleteScreen();break;
case 3: destroyApp(true);break;
}
}
}
void search_add(String alamat) // Fungsi Cari
{
String temp = " ";
String notelp;
String nama;
int size = alamat.length();
try
{
RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
ui_form = new Form("Cari Data.");
while(re.hasNextElement())
{
String name1 = new String(re.nextRecord());
try
{
nama = name1.substring(2,name1.indexOf("?"));
}
catch (Exception ef)
{
nama = "check";
}
String check_name = nama.substring(0,size);
if (check_name.equals(alamat))
{
try
{
notelp = name1.substring(name1.indexOf("?")+1);
}
catch (Exception e)
{
notelp = "";
}
temp = temp +"\nNama.."+nama+"\nPhone.."+notelp;
}
}
if (temp.equals(" "))
{
temp = "Nama tersebut tidak ditemukan...";
}
ui_form.append(temp);
ui_form.addCommand(quit); // Tambahkan Button pada ui_form.
ui_form.addCommand(mainmenu); // Tambahkan Button pada ui_form.
ui_form.setCommandListener(this); // Action Listener..
display.setCurrent(ui_form);
}
catch (RecordStoreNotOpenException rsnoe)
{
rsnoe.printStackTrace();
}
catch (InvalidRecordIDException irid)
{
irid.printStackTrace();
}
catch (RecordStoreException rse)
{
rse.printStackTrace();
}
}
void delete_add(String alamat) // Fungsi delete....
{
String temp = " ";
String notelp;
String nama;
int i = 1;
int del_id = 0;
try
{
RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
ui_form = new Form("Hapus Data..");
while(re.hasNextElement())
{
String name1 = new String(re.nextRecord());
try
{
nama = name1.substring(2,name1.indexOf("?"));
}
catch (Exception ef)
{
nama = "check";
}
if (nama.equals(alamat))
{
del_id = i;
}
i++;
}
if (del_id != 0)
{
recordStore.deleteRecord(del_id);
temp = "Record telah terhapus...";
}
else
{
temp = "Data Tidak Ada di List...";
}
}
catch(Exception e)
{
}
ui_form.append(temp);
ui_form.addCommand(quit); // Tambahkan Button pada ui_form.
ui_form.addCommand(mainmenu); // Tambahkan Button pada ui_form.
ui_form.setCommandListener(this); // Action Listener..
display.setCurrent(ui_form);
}
void alamat_add(String alamat, String phone) // Fungsi tambah alamat
{
String data = alamat+"?"+phone; // ? (batas/pemisah antara nama dan nomor)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try
{
outputStream.writeUTF(data);
byte[] b = baos.toByteArray();
recordStore.addRecord(b,0, b.length);
ui_form = new Form("Sukses.....");
ui_form.append("Sebuah Data Telah Ditambahkan");
ui_form.addCommand(quit); // Tambahkan Button pada ui_form.
ui_form.addCommand(mainmenu); // Tambahkan Button pada ui_form.
ui_form.setCommandListener(this); // Action Listener..
display.setCurrent(ui_form);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (RecordStoreException rse)
{
rse.printStackTrace();
}
}
public void close() throws RecordStoreNotOpenException,RecordStoreException
{
recordStore.closeRecordStore();
}
}

Contoh Program Bentuk Geometri

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

public class BentukGeometri extends MIDlet implements CommandListener
{
private Display display;
private List list;
private Canvas canvas;

private final Command cmdExit = new Command("Exit", Command.EXIT, 1);
private final Command cmdOK = new Command("OK", Command.OK, 1);
private final Command cmdBack = new Command("Back", Command.BACK, 1);

public BentukGeometri()
{
display = Display.getDisplay(this);
list = new List("Bentuk Geometri", Choice.EXCLUSIVE);
list.append("Segiempat", null);
list.append("Kerucut", null);
list.append("Lingkaran", null);
list.append("Setengah Lingkaran", null);
list.append("Elips", null);

list.addCommand(cmdExit);
list.addCommand(cmdOK);
list.setCommandListener(this);
}

public void startApp()
{
display.setCurrent(list);
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s)
{
if (c == cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmdBack)
{
display.setCurrent(list);
}
else
{
switch (list.getSelectedIndex())
{
case 0 :
{
canvas = (Segiempat) (new Segiempat(this));
break;
}
case 1 :
{
canvas = (Kerucut) (new Kerucut(this));
break;
}
case 2 :
{
canvas = (Lingkaran) (new Lingkaran(this));
break;
}
case 3 :
{
canvas = (SetengahLingkaran) (new SetengahLingkaran(this));
break;
}
case 4 :
{
canvas = (elips) (new elips(this));
break;
}
}
canvas.addCommand(cmdBack);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}
}


class Segiempat extends Canvas
{
private BentukGeometri midlet;

public Segiempat(BentukGeometri midlet)
{
this.midlet = midlet;
}

public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawRect(55, 55, 60, 60);
g.setColor(255, 0, 0);
g.fillRect(56, 56, 59, 59);
}
};


class Kerucut extends Canvas
{
private BentukGeometri midlet;

public Kerucut(BentukGeometri midlet)
{
this.midlet = midlet;
}

public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawArc(60, 60, 60, 150, 60, 60);
g.setColor(0, 255, 0);
g.fillArc(60, 60, 60, 150, 60, 60);
}
};


class Lingkaran extends Canvas
{
private BentukGeometri midlet;

public Lingkaran(BentukGeometri midlet)
{
this.midlet = midlet;
}

public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 255);
g.fillArc(55, 55, 60, 60, 0 , 360);
}
};


class SetengahLingkaran extends Canvas
{
private BentukGeometri midlet;

public SetengahLingkaran(BentukGeometri midlet)
{
this.midlet = midlet;
}

public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 255);
g.fillArc(55, 55, 60, 60, 0 , 180);
}
};
class elips extends Canvas
{
private BentukGeometri midlet;

public elips(BentukGeometri midlet)
{
this.midlet = midlet;
}

public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawRoundRect(40, 40, 40, 80, 80 , 80);
g.setColor(255, 0, 0);
g.fillRoundRect(40, 40, 40, 80, 80 , 80);
}
};

}

Postingan Lebih Baru Beranda