Selasa, 18 Januari 2011

Kamus HP berbasis Java (J2ME/JME) sederhana

Dari mailing list: netbeans-indonesia 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

/**
 * @author Sapto Sutardi Fakultas Kedokteran Univ. Mataram
 */

public class KamusSasakIndo extends MIDlet implements CommandListener {
    public Form frSasak, frIndo;
    public Display display;
    public Command cmTransSasak, cmTransIndo, cmExit, cmBackSasak, cmBackIndo, cmKeUtama;
    public TextField tfSasak, tfIndo;
    public TextBox tbSasak, tbIndo;
    public List lsUtama;
    public Alert alNotFound;
    public String[] sasak = {"uwah", "mangan", "side", "tidoq", "ende ne", "araq", "kepeng"};
    public String[] indo = {"sudah", "makan", "anda", "tidur", "tidak", "ada", "uang"};
    public KamusSasakIndo(){
        display = Display.getDisplay(this);
        alNotFound = new Alert("Maaf", "Kata yang Anda cari ndeq ne araq.", null, AlertType.INFO);
        alNotFound.setTimeout(3000);
    }
    public void startApp() {
        get_lsUtama();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void exitMIDlet() {
        display.setCurrent(null);
        destroyApp(false);
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmTransSasak){
            String sSasak = tfSasak.getString().toLowerCase();
            for(int i =0; i
                if(sSasak.equals(sasak[i])){
                    System.out.println("Posisi :"+i);
                get_vSasak();
                tbSasak.setString(indo[i]);
                break;
                } else {
                    display.setCurrent(alNotFound);
                }
            }
        }

        if(c == cmTransIndo){
            String sSasak = tfIndo.getString().toLowerCase();
            for(int i =0; i
                if(sSasak.equals(indo[i])){
                    System.out.println("Posisi :"+i);
                get_vIndo();
                tbIndo.setString(sasak[i]);
                break;
                } else {
                    display.setCurrent(alNotFound);
                }
            }
        }

        else if(c == cmExit){
            destroyApp(false);
            notifyDestroyed();
        }

        else if (c == List.SELECT_COMMAND) {
            if (d == lsUtama){
                switch (lsUtama.getSelectedIndex()) {
                    case 0: {
                        get_Sasak();
                        break;
                    }
                    case 1: {
                        get_Indo();
                        break;
                    }
                }
            }
        }

        else if(c == cmBackSasak){
            display.setCurrent(frSasak);
        }

        else if(c == cmBackIndo){
            display.setCurrent(frIndo);
        }

        else if(c == cmKeUtama){
            get_Sasak();
            display.setCurrent(lsUtama);
        }
    }

    public List get_lsUtama(){
        lsUtama = new List("Kamus Sasak Indo", List.IMPLICIT);
        lsUtama.append("Sasak -> Indo", null);
        lsUtama.append("Indo -> Sasak", null);
        cmExit = new Command("Keluar", Command.EXIT, 1);
        lsUtama.addCommand(cmExit);
        lsUtama.setCommandListener(this);
        display.setCurrent(lsUtama);
        return lsUtama;
    }

    public Form get_Sasak(){
        frSasak = new Form("Sasak -> Indo");
        tfSasak = new TextField("Kata Sasak", null, 20, TextField.ANY);
        cmTransSasak = new Command("Terjemah", Command.SCREEN, 1);
        cmKeUtama = new Command("Kembali", Command.SCREEN, 2);
        frSasak.append(tfSasak);
        frSasak.addCommand(cmTransSasak);
        frSasak.addCommand(cmKeUtama);
        frSasak.setCommandListener(this);
        display.setCurrent(frSasak);
        return frSasak;
    }

    public Form get_Indo(){
        frIndo = new Form("Indo -> Sasak");
        tfIndo = new TextField("Kata Indo", null, 20, TextField.ANY);
        cmTransIndo = new Command("Terjemah", Command.SCREEN, 1);
        cmKeUtama = new Command("Kembali", Command.SCREEN, 2);
        frIndo.append(tfIndo);
        frIndo.addCommand(cmTransIndo);
        frIndo.addCommand(cmKeUtama);
        frIndo.setCommandListener(this);
        display.setCurrent(frIndo);
        return frIndo;
    }

    public TextBox get_vSasak(){
        tbSasak = new TextBox(tfSasak.getString(), "", 100, 1);
        cmBackSasak = new Command("Kembali", Command.SCREEN, 1);
        tbSasak.addCommand(cmBackSasak);
        tbSasak.setCommandListener(this);
        display.setCurrent(tbSasak);
        return tbSasak;
    }

    public TextBox get_vIndo(){
        tbIndo= new TextBox(tfIndo.getString(), "", 100, 1);
        cmBackIndo = new Command("Kembali", Command.SCREEN, 1);
        tbIndo.addCommand(cmBackIndo);
        tbIndo.setCommandListener(this);
        display.setCurrent(tbIndo);
        return tbIndo;
    }
}

Bisa dikembangkan:
  1. Bahasa lain (misalnya Jawa –> Inggris), karena saya orang Sasak-Lombok jadi saya buat Sasak-Indonesia saja.
  2. Data kosa kata bisa ditaruh pada file terpisah dengan "getResourceAsStream"
  3. Menambah kosa kata dengan RMS.
  4. Pencarian dengan similiar text
  5. dll
Semoga bisa membantu yang membutuhkan.
Sapto Sutardi, Mataram NTB ============ ========= ===== Programmer untuk keperluan medis (+sesuai pesanan: Web & J2ME) 085 639 899 94, sapto_sutardi@yahoo.com

1 komentar:

  1. trimakasi postingannya bermanfaat Gan.

    Gan Nitip link y :)
    bagi siapa aja yg mau shering ttg desain 3D bisa klik: http://www.bihum.blogspot.com

    BalasHapus