Rabu, 18 Juli 2012
PROGRAM DAFTAR KLASEMEN SEMENTARA LIGA INGGRIS (J2ME dengan RMS)
Program aplikasi yang penulis buat ini merupakan
program aplikasi yang menggunakan J2ME (Java2 Micro Edition) dengan RMS (Record
Management System). Pada dasarnya fungsi dari RMS ini sama dengan database, RMS
itu sendiri merupakan salah satu fasilitas yang berguna untuk menyimpan semua
informasi kedalam memori non-volatile (memori tetap). Jadi, semua data yang
telah kita simpan tidak akan hilang sampai kita sendiri yang menghapusnya.
Baik, langsung saja ke topik inti kita.
Pertama-tama bukalah projek baru pada NetBeans. Apabila anda belum memilikinya,
bisa download disini!.
Cara membuat projek baru :
1. Klik "new project" atau Ctrl+Shift+N
2. Pada "Choosen project" pilih
"Mobility" lalu klik "Next"
3. Pada "Projek Name" tulislah
"JavaMobilePerbaungan2"
4. Hilangkan centang pada "Create Hello
MIDlet" lalu klik finish
Cara membuat file baru :
1. Klik "new file" atau Ctrl + N
2. Pada "Categories" pilih MIDP lalu
klik "Next"
3. Pada MIDlet name tulislah
dengan"KlasemenSementara" lalu klik "Finish"
Selanjutnya copykan semua Listing Program dibawah ini
kedalam file KlasemenSementara.java tersebut. Untuk menampilkan gambar pada program
aplikasi ini maka copykan gambar diatas lalu tempatkan pada
C:\Users\"Sesuaikan dengan Nama Komputer Anda"\Documents\NetBeansProjects\JavaMobilePerbaungan2\src.
Setelah selesai maka gambar secara otomatis akan tampil pada saat anda akan
menjalankan program ini.
Listing Program "KlasemenSementara.java"
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class KlasemenSementara extends MIDlet implements CommandListener {
Display D;
Form fr1, fr3, fr4, fr5;
List ls;
Alert alert;
RecordStore recordstore = null;
RecordEnumeration recordEnumeration = null;
Command Lihat = new Command("Lihat Data", Command.SCREEN, 1);
Command TambahData = new Command("Tambah Data", Command.SCREEN, 2);
Command Hapus = new Command("Hapus Data", Command.SCREEN, 1);
Command Keluar = new Command("Keluar", Command.EXIT, 1);
Command Simpan = new Command("Simpan", Command.SCREEN, 1);
Command Back = new Command("Kembali", Command.BACK, 1);
Command Cancel = new Command("Kembali", Command.BACK, 1);
TextField txtPeringkat = new TextField("Peringkat: ", "", 30, TextField.PHONENUMBER);
TextField txtNamaClub = new TextField("Nama Club:", "", 30, TextField.ANY);
TextField txtJumlahPertandingan = new TextField("Jumlah Pertandingan:", "", 30, TextField.PHONENUMBER);
TextField txtMenang = new TextField("Menang:", "", 30, TextField.PHONENUMBER);
TextField txtKalah = new TextField("Kalah:", "", 30, TextField.PHONENUMBER);
TextField txtSeri = new TextField("Seri:", "", 30, TextField.PHONENUMBER);
TextField txtTotal = new TextField("Total Nilai:", "", 30, TextField.PHONENUMBER);
int recID, currentTask;
String keyword;
public KlasemenSementara() {
fr1 = new Form(" Klasemen Sementara Liga Inggris");
try {
Image img = Image.createImage("/java mobile perbaungan.jpg");
ImageItem image = new ImageItem("", img, Item.LAYOUT_CENTER, "");
fr1.append(image);
} catch (Exception e) {
e.printStackTrace();
}
fr1.addCommand(TambahData);
fr1.addCommand(Lihat);
fr1.addCommand(Keluar);
fr1.setCommandListener(this);
fr3 = new Form("Tambahkan Data ");
fr3.addCommand(Cancel);
fr3.addCommand(Simpan);
fr3.append(txtPeringkat);
fr3.append(txtNamaClub);
fr3.append(txtJumlahPertandingan);
fr3.append(txtMenang);
fr3.append(txtKalah);
fr3.append(txtSeri);
fr3.append(txtTotal);
fr3.setCommandListener(this);
}
public void startApp() {
if (D == null) {
D = Display.getDisplay(this);
D.setCurrent(fr1);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public class Banding implements RecordComparator {
private byte[] comparatorInputData = new byte[800];
private ByteArrayInputStream comparatorInputStream = null;
private DataInputStream comparatorInputDataType = null;
public int compare(byte[] record1, byte[] record2) {
int record1int, record2int;
try {
comparatorInputStream = new ByteArrayInputStream(record1);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data1 = comparatorInputDataType.readUTF();
comparatorInputStream = new ByteArrayInputStream(record2);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data2 = comparatorInputDataType.readUTF();
int comparison = data1.compareTo(data2);
if (comparison == 0) {
return RecordComparator.EQUIVALENT;
} else if (comparison < 0) {
return RecordComparator.PRECEDES;
} else {
return RecordComparator.FOLLOWS;
}
} catch (Exception error) {
return RecordComparator.EQUIVALENT;
}
}
public void compareClose() {
try {
if (comparatorInputStream != null) {
comparatorInputStream.close();
}
if (comparatorInputDataType != null) {
comparatorInputDataType.close();
}
} catch (Exception error) {
}
}
}
public void commandAction(Command c, Displayable d) {
try {
recordstore = RecordStore.openRecordStore("KlasemenSementaraRecords", true);
} catch (Exception error) {
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
if (c == Keluar) {
destroyApp(true);
notifyDestroyed();
}
if (c == Back) {
D.setCurrent(ls);
}
if (c == Cancel) {
D.setCurrent(fr1);
}
if (c == TambahData) {
txtPeringkat.setString("");
txtNamaClub.setString("");
txtJumlahPertandingan.setString("");
txtMenang.setString("");
txtKalah.setString("");
txtSeri.setString("");
txtTotal.setString("");
D.setCurrent(fr3);
}
if (c == Simpan) {
try {
String outputPeringkat = txtPeringkat.getString();
String outputNamaClub = txtNamaClub.getString();
String outputJumlahPertandingan = txtJumlahPertandingan.getString();
String outputMenang= txtMenang.getString();
String outputKalah = txtKalah.getString();
String outputSeri = txtSeri.getString();
String outputTotal = txtTotal.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputPeringkat);
outputDataStream.writeUTF(outputNamaClub);
outputDataStream.writeUTF(outputJumlahPertandingan);
outputDataStream.writeUTF(outputMenang);
outputDataStream.writeUTF(outputKalah);
outputDataStream.writeUTF(outputSeri);
outputDataStream.writeUTF(outputTotal);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
D.setCurrent(fr1);
} catch (Exception error) {
D.setCurrent(alert);
}
}
if (c == Hapus) {
int index = 1 + ls.getSelectedIndex();
try {
if (currentTask == 2) {
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.deleteRecord(datapointer);
}
}
D.setCurrent(fr1);
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
if (c == Lihat) {
try {
ls = new List("Klasemen Sementara Liga Inggris", List.IMPLICIT);
ls.addCommand(Cancel);
ls.setCommandListener(this);
currentTask = 2;
byte[] byteInputData = new byte[800];
String InputPeringkat, inputNamaClub,inputJumlahPertandingan,inputMenang,inputKalah
,inputSeri,inputTotal= null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
while (recordEnumeration.hasNextElement()) {
recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
InputPeringkat= inputDataStream.readUTF();
inputNamaClub = inputDataStream.readUTF();
inputJumlahPertandingan = inputDataStream.readUTF();
inputMenang = inputDataStream.readUTF();
inputKalah = inputDataStream.readUTF();
inputSeri = inputDataStream.readUTF();
inputTotal = inputDataStream.readUTF();
ls.append(InputPeringkat+". "+inputNamaClub+ "", null);
inputStream.reset();
}
D.setCurrent(ls);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
if (c == List.SELECT_COMMAND) {
int index = 1 + ls.getSelectedIndex();
try {
fr4 = new Form("Keterangan:");
fr4.addCommand(Back);
fr4.addCommand(Hapus);
fr4.setCommandListener(this);
byte[] byteInputData = new byte[800];
String InputPeringkat, inputNamaClub,inputJumlahPertandingan,inputMenang,inputKalah
,inputSeri,inputTotal= null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 2) {
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.getRecord(datapointer, byteInputData, 0);
recID = datapointer;
InputPeringkat= inputDataStream.readUTF();
inputNamaClub = inputDataStream.readUTF();
inputJumlahPertandingan = inputDataStream.readUTF();
inputMenang= inputDataStream.readUTF();
inputKalah = inputDataStream.readUTF();
inputSeri = inputDataStream.readUTF();
inputTotal = inputDataStream.readUTF();
StringItem stringPeringkat= new StringItem("Peringkat: ", InputPeringkat, Item.PLAIN);
StringItem stringNamaClub = new StringItem("Nama Club: ", inputNamaClub, Item.PLAIN);
StringItem stringJumlahPertandingan = new StringItem("Jumlah Pertandingan: ", inputJumlahPertandingan, Item.PLAIN);
StringItem stringMenang= new StringItem("Menang: ", inputMenang, Item.PLAIN);
StringItem stringKalah = new StringItem("Kalah: ", inputKalah, Item.PLAIN);
StringItem stringSeri = new StringItem("Seri: ", inputSeri, Item.PLAIN);
StringItem stringTotal = new StringItem("Total Nilai: ", inputTotal, Item.PLAIN);
fr4.append(stringPeringkat);
fr4.append(stringNamaClub);
fr4.append(stringJumlahPertandingan);
fr4.append(stringMenang);
fr4.append(stringKalah);
fr4.append(stringSeri);
fr4.append(stringTotal);
inputStream.reset();
}
}
D.setCurrent(fr4);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
try {
recordstore.closeRecordStore();
} catch (Exception error) {
alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
}
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class KlasemenSementara extends MIDlet implements CommandListener {
Display D;
Form fr1, fr3, fr4, fr5;
List ls;
Alert alert;
RecordStore recordstore = null;
RecordEnumeration recordEnumeration = null;
Command Lihat = new Command("Lihat Data", Command.SCREEN, 1);
Command TambahData = new Command("Tambah Data", Command.SCREEN, 2);
Command Hapus = new Command("Hapus Data", Command.SCREEN, 1);
Command Keluar = new Command("Keluar", Command.EXIT, 1);
Command Simpan = new Command("Simpan", Command.SCREEN, 1);
Command Back = new Command("Kembali", Command.BACK, 1);
Command Cancel = new Command("Kembali", Command.BACK, 1);
TextField txtPeringkat = new TextField("Peringkat: ", "", 30, TextField.PHONENUMBER);
TextField txtNamaClub = new TextField("Nama Club:", "", 30, TextField.ANY);
TextField txtJumlahPertandingan = new TextField("Jumlah Pertandingan:", "", 30, TextField.PHONENUMBER);
TextField txtMenang = new TextField("Menang:", "", 30, TextField.PHONENUMBER);
TextField txtKalah = new TextField("Kalah:", "", 30, TextField.PHONENUMBER);
TextField txtSeri = new TextField("Seri:", "", 30, TextField.PHONENUMBER);
TextField txtTotal = new TextField("Total Nilai:", "", 30, TextField.PHONENUMBER);
int recID, currentTask;
String keyword;
public KlasemenSementara() {
fr1 = new Form(" Klasemen Sementara Liga Inggris");
try {
Image img = Image.createImage("/java mobile perbaungan.jpg");
ImageItem image = new ImageItem("", img, Item.LAYOUT_CENTER, "");
fr1.append(image);
} catch (Exception e) {
e.printStackTrace();
}
fr1.addCommand(TambahData);
fr1.addCommand(Lihat);
fr1.addCommand(Keluar);
fr1.setCommandListener(this);
fr3 = new Form("Tambahkan Data ");
fr3.addCommand(Cancel);
fr3.addCommand(Simpan);
fr3.append(txtPeringkat);
fr3.append(txtNamaClub);
fr3.append(txtJumlahPertandingan);
fr3.append(txtMenang);
fr3.append(txtKalah);
fr3.append(txtSeri);
fr3.append(txtTotal);
fr3.setCommandListener(this);
}
public void startApp() {
if (D == null) {
D = Display.getDisplay(this);
D.setCurrent(fr1);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public class Banding implements RecordComparator {
private byte[] comparatorInputData = new byte[800];
private ByteArrayInputStream comparatorInputStream = null;
private DataInputStream comparatorInputDataType = null;
public int compare(byte[] record1, byte[] record2) {
int record1int, record2int;
try {
comparatorInputStream = new ByteArrayInputStream(record1);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data1 = comparatorInputDataType.readUTF();
comparatorInputStream = new ByteArrayInputStream(record2);
comparatorInputDataType = new DataInputStream(comparatorInputStream);
String data2 = comparatorInputDataType.readUTF();
int comparison = data1.compareTo(data2);
if (comparison == 0) {
return RecordComparator.EQUIVALENT;
} else if (comparison < 0) {
return RecordComparator.PRECEDES;
} else {
return RecordComparator.FOLLOWS;
}
} catch (Exception error) {
return RecordComparator.EQUIVALENT;
}
}
public void compareClose() {
try {
if (comparatorInputStream != null) {
comparatorInputStream.close();
}
if (comparatorInputDataType != null) {
comparatorInputDataType.close();
}
} catch (Exception error) {
}
}
}
public void commandAction(Command c, Displayable d) {
try {
recordstore = RecordStore.openRecordStore("KlasemenSementaraRecords", true);
} catch (Exception error) {
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
if (c == Keluar) {
destroyApp(true);
notifyDestroyed();
}
if (c == Back) {
D.setCurrent(ls);
}
if (c == Cancel) {
D.setCurrent(fr1);
}
if (c == TambahData) {
txtPeringkat.setString("");
txtNamaClub.setString("");
txtJumlahPertandingan.setString("");
txtMenang.setString("");
txtKalah.setString("");
txtSeri.setString("");
txtTotal.setString("");
D.setCurrent(fr3);
}
if (c == Simpan) {
try {
String outputPeringkat = txtPeringkat.getString();
String outputNamaClub = txtNamaClub.getString();
String outputJumlahPertandingan = txtJumlahPertandingan.getString();
String outputMenang= txtMenang.getString();
String outputKalah = txtKalah.getString();
String outputSeri = txtSeri.getString();
String outputTotal = txtTotal.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputPeringkat);
outputDataStream.writeUTF(outputNamaClub);
outputDataStream.writeUTF(outputJumlahPertandingan);
outputDataStream.writeUTF(outputMenang);
outputDataStream.writeUTF(outputKalah);
outputDataStream.writeUTF(outputSeri);
outputDataStream.writeUTF(outputTotal);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
D.setCurrent(fr1);
} catch (Exception error) {
D.setCurrent(alert);
}
}
if (c == Hapus) {
int index = 1 + ls.getSelectedIndex();
try {
if (currentTask == 2) {
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.deleteRecord(datapointer);
}
}
D.setCurrent(fr1);
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
if (c == Lihat) {
try {
ls = new List("Klasemen Sementara Liga Inggris", List.IMPLICIT);
ls.addCommand(Cancel);
ls.setCommandListener(this);
currentTask = 2;
byte[] byteInputData = new byte[800];
String InputPeringkat, inputNamaClub,inputJumlahPertandingan,inputMenang,inputKalah
,inputSeri,inputTotal= null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
while (recordEnumeration.hasNextElement()) {
recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
InputPeringkat= inputDataStream.readUTF();
inputNamaClub = inputDataStream.readUTF();
inputJumlahPertandingan = inputDataStream.readUTF();
inputMenang = inputDataStream.readUTF();
inputKalah = inputDataStream.readUTF();
inputSeri = inputDataStream.readUTF();
inputTotal = inputDataStream.readUTF();
ls.append(InputPeringkat+". "+inputNamaClub+ "", null);
inputStream.reset();
}
D.setCurrent(ls);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
if (c == List.SELECT_COMMAND) {
int index = 1 + ls.getSelectedIndex();
try {
fr4 = new Form("Keterangan:");
fr4.addCommand(Back);
fr4.addCommand(Hapus);
fr4.setCommandListener(this);
byte[] byteInputData = new byte[800];
String InputPeringkat, inputNamaClub,inputJumlahPertandingan,inputMenang,inputKalah
,inputSeri,inputTotal= null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 2) {
Banding comparator = new Banding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
}
for (int i = 1; i <= recordEnumeration.numRecords(); i++) {
int datapointer = recordEnumeration.nextRecordId();
if (i == index) {
recordstore.getRecord(datapointer, byteInputData, 0);
recID = datapointer;
InputPeringkat= inputDataStream.readUTF();
inputNamaClub = inputDataStream.readUTF();
inputJumlahPertandingan = inputDataStream.readUTF();
inputMenang= inputDataStream.readUTF();
inputKalah = inputDataStream.readUTF();
inputSeri = inputDataStream.readUTF();
inputTotal = inputDataStream.readUTF();
StringItem stringPeringkat= new StringItem("Peringkat: ", InputPeringkat, Item.PLAIN);
StringItem stringNamaClub = new StringItem("Nama Club: ", inputNamaClub, Item.PLAIN);
StringItem stringJumlahPertandingan = new StringItem("Jumlah Pertandingan: ", inputJumlahPertandingan, Item.PLAIN);
StringItem stringMenang= new StringItem("Menang: ", inputMenang, Item.PLAIN);
StringItem stringKalah = new StringItem("Kalah: ", inputKalah, Item.PLAIN);
StringItem stringSeri = new StringItem("Seri: ", inputSeri, Item.PLAIN);
StringItem stringTotal = new StringItem("Total Nilai: ", inputTotal, Item.PLAIN);
fr4.append(stringPeringkat);
fr4.append(stringNamaClub);
fr4.append(stringJumlahPertandingan);
fr4.append(stringMenang);
fr4.append(stringKalah);
fr4.append(stringSeri);
fr4.append(stringTotal);
inputStream.reset();
}
}
D.setCurrent(fr4);
inputStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
try {
recordstore.closeRecordStore();
} catch (Exception error) {
alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
D.setCurrent(alert);
}
}
}
Baik!, sampai disini dulu artikel yang penulis
berikan. Semoga dapat bermanfaat dan membantu Anda semua. Salam "Java
Mobile Perbaungan"!!
Langganan:
Posting Komentar (Atom)
1 komentar:
Hasil run file nya seperti apa ya ka?
Posting Komentar