Tampilkan postingan dengan label Pemograman Java. Tampilkan semua postingan
Tampilkan postingan dengan label Pemograman Java. Tampilkan semua postingan

1.23.2009

GameActions


Kali ini kita akan membuat Gambe actions, dengan menggunakan menginport Midletnya sekalian. IDE yang digunakan dalam contoh ini adalah Net-Beans 5.5 dengan mobility-nya...!
lansung saja buat project baru di Net_Beansnya dengan nama file GameAction, lalu ketikan kode dibawah ini :

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

public class GameActions extends MIDlet
{
private Display display; // The display
private GameActionCanvas canvas; // Canvas

public GameActions()
{
display = Display.getDisplay(this);
canvas = new GameActionCanvas(this);
}

protected void startApp()
{
display.setCurrent( canvas );
}

protected void pauseApp()
{ }

protected void destroyApp( boolean unconditional )
{ }

public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}

/*--------------------------------------------------
* GameActionCanvas.java
*
* Game action event handling
*-------------------------------------------------*/
class GameActionCanvas extends Canvas implements CommandListener
{
private Command cmExit; // Exit midlet
private String keyText = null; // Key code text
private GameActions midlet;

/*--------------------------------------------------
* Constructor
*-------------------------------------------------*/
public GameActionCanvas(GameActions midlet)
{
this.midlet = midlet;

// Create exit command & listen for events
cmExit = new Command("Exit", Command.EXIT, 1);
addCommand(cmExit);
setCommandListener(this);
}

/*--------------------------------------------------
* Paint the text representing the key code
*-------------------------------------------------*/
protected void paint(Graphics g)
{
// Clear the background (to white)
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());

// Set color and draw text
if (keyText != null)
{
// Draw with black pen
g.setColor(0, 0, 0);
// Center the text
g.drawString(keyText, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);
}
}

/*--------------------------------------------------
* Command event handling
*-------------------------------------------------*/
public void commandAction(Command c, Displayable d)
{
if (c == cmExit)
midlet.exitMIDlet();
}

/*--------------------------------------------------
* Game action event handling
* A game action will be converted into a key code
* and handed off to this method
*-------------------------------------------------*/
protected void keyPressed(int keyCode)
{
switch (getGameAction(keyCode))
{
// Place logic of each action inside the case
case FIRE:
case UP:
case DOWN:
case LEFT:
case RIGHT:
case GAME_A:
case GAME_B:
case GAME_C:
case GAME_D:
default:
// Print the text of the game action
keyText = getKeyName(keyCode);
}
repaint();
}
}

Baca Selengkapnya......

1.16.2009

Membuat Kalkulator dengan J Creator

MEmbuat Kalkulator dengan Menggunakan J Creator
Tidak terlalu sulit, yang penting di PC Mu sudah ada minimal J2SDK lalu J Creator, Lalu Copy kan Kode Dibawah ini :

import java.awt.*;
import java.awt.event.*;

public class CalculatorH2_DIIITKJ
{

Font Bold=new Font("Helvetica", Font.BOLD, 20);
Label TampilanLayar = new Label("MULAI",Label.RIGHT);
Label TampilanLayarkiri = new Label("",Label.RIGHT);
Label TampilanLayarkanan = new Label("",Label.CENTER);
Button Panggil_Simpanan=new Button("MR");
Button Bersihkan_Simpanan=new Button("MC");


public void map_KalkulOgut()
{
event_handler e1=new event_handler ( );
Frame KalkulOgut=new Frame(" ................................... ");
KalkulOgut.setResizable(true);
KalkulOgut.setSize(520,400);
KalkulOgut.setBackground(Color.blue);
KalkulOgut.setLayout(null);


//Tampilan Display. bisa di edit menggunakan Label
TampilanLayar.setBounds(70,50,360,40);
TampilanLayar.setFont(new Font("Helvetica", Font.BOLD, 25));
TampilanLayar.setForeground(Color.white);
TampilanLayar.setBackground(Color.red);
KalkulOgut.add(TampilanLayar);

TampilanLayarkanan.setBounds(68,48,364,44);
TampilanLayarkanan.setFont(new Font("Algerian", Font.BOLD, 40));
TampilanLayarkanan.setForeground(Color.red);
TampilanLayarkanan.setBackground(Color.green);
KalkulOgut.add(TampilanLayarkanan);

//Menambah Tombol 1 sampai dengan 0
Button simpanan=new Button("M+");
simpanan.setBounds(72,96,60,34);
simpanan.setBackground(Color.white);
simpanan.setForeground(Color.red);
simpanan.setFont(Bold);
simpanan.addActionListener(e1);
KalkulOgut.add(simpanan);

Bersihkan_Simpanan.setEnabled(false);
Bersihkan_Simpanan.setBounds(136,96,60,34);
Bersihkan_Simpanan.setBackground(Color.black);
Bersihkan_Simpanan.setForeground(Color.red);
Bersihkan_Simpanan.setFont(Bold);
Bersihkan_Simpanan.addActionListener(e1);
KalkulOgut.add(Bersihkan_Simpanan);

Panggil_Simpanan.setBounds(200,96,60,34);
Panggil_Simpanan.setBackground(Color.yellow);
Panggil_Simpanan.setForeground(Color.red);
Panggil_Simpanan.setFont(Bold);
Panggil_Simpanan.addActionListener(e1);
Panggil_Simpanan.setEnabled(false);
KalkulOgut.add(Panggil_Simpanan);

Button satu=new Button("1");
satu.setBounds(72,134,60,34);
satu.setBackground(Color.orange);
satu.setFont(Bold);
satu.addActionListener(e1);
KalkulOgut.add(satu);

Button dua=new Button("2");
dua.addActionListener(e1);
dua.setBackground(Color.orange);
dua.setBounds(136,134,60,34);
dua.setFont(Bold);
KalkulOgut.add(dua);

Button tiga=new Button("3");
tiga.addActionListener(e1);
tiga.setBackground(Color.orange);
tiga.setBounds(200,134,60,34);
tiga.setFont(Bold);
KalkulOgut.add(tiga);

Button empat=new Button("4");
empat.setBackground(Color.orange);
empat.addActionListener(e1);
empat.setFont(Bold);
empat.setBounds(72,172,60,34);
KalkulOgut.add(empat);

Button lima=new Button("5");
lima.setBackground(Color.orange);
lima.addActionListener(e1);
lima.setFont(Bold);
lima.setBounds(136,172,60,34);
KalkulOgut.add(lima);

Button enam=new Button("6");
enam.setBackground(Color.orange);
enam.addActionListener(e1);
enam.setFont(Bold);
enam.setBounds(200,172,60,34);
KalkulOgut.add(enam);

Button tujuh=new Button("7");
tujuh.setBackground(Color.orange);
tujuh.addActionListener(e1);
tujuh.setFont(Bold);
tujuh.setBounds(72,210,60,34);
KalkulOgut.add(tujuh);

Button delapan=new Button("8");
delapan.setBackground(Color.orange);
delapan.addActionListener(e1);
delapan.setFont(Bold);
delapan.setBounds(136,210,60,34);
KalkulOgut.add(delapan);

Button sembilan=new Button("9");
sembilan.setBackground(Color.orange);
sembilan.addActionListener(e1);
sembilan.setFont(Bold);
sembilan.setBounds(200,210,60,34);
KalkulOgut.add(sembilan);

Button kosong=new Button("0");
kosong.setBackground(Color.orange);
kosong.addActionListener(e1);
kosong.setFont(Bold);
kosong.setBounds(136,248,60,34);
KalkulOgut.add(kosong);

Button nanin =new Button("STIMIK - IKMI CIREBON");
nanin.setForeground(Color.red);
nanin.setBackground(Color.green);
nanin.addActionListener(e1);
nanin.setFont(Bold);
nanin.setBounds(72,290,358,50);
KalkulOgut.add(nanin);

Label simbol =new Label("BRAVO MAHASISWA DIII TKJ KELAS H2 STIMIK ",Label.LEFT);
simbol.setForeground(Color.white);
simbol.setFont(Bold);
simbol.setBounds(33,350,550,50);
KalkulOgut.add(simbol);

//Tombol operasi
Button TombolBagi = new Button("BAGI");
TombolBagi.setBackground(Color.gray);
TombolBagi.addActionListener(e1);
TombolBagi.setBounds(264,134,100,34);
TombolBagi.setForeground(Color.green);
TombolBagi.setFont(Bold);
KalkulOgut.add(TombolBagi);

Button TombolKali = new Button("KALI");
TombolKali.setBackground(Color.gray);
TombolKali.addActionListener(e1);
TombolKali.setBounds(264,172,100,34);
TombolKali.setForeground(Color.green);
TombolKali.setFont(Bold);
KalkulOgut.add(TombolKali);

Button TombolPengurangan = new Button("KIRANG");
TombolPengurangan.setBackground(Color.gray);
TombolPengurangan.addActionListener(e1);
TombolPengurangan.setBounds(264,210,100,34);
TombolPengurangan.setForeground(Color.green);
TombolPengurangan.setFont(Bold);
KalkulOgut.add(TombolPengurangan);

Button TombolTambah= new Button("TAMBIH");
TombolTambah.setBackground(Color.gray);
TombolTambah.addActionListener(e1);
TombolTambah.setBounds(264,248,100,34);
TombolTambah.setForeground(Color.green);
TombolTambah.setFont(Bold);
KalkulOgut.add(TombolTambah);

Button titik= new Button(".");
titik.setBackground(Color.gray);
titik.addActionListener(e1);
titik.setBounds(200,248,60,34);
titik.setForeground(Color.green);
titik.setFont(Bold);
KalkulOgut.add(titik);

Button TombolTambahKurang = new Button("+/-");
TombolTambahKurang.setBackground(Color.gray);
TombolTambahKurang.addActionListener(e1);
TombolTambahKurang.setBounds(72,248,60,34);
TombolTambahKurang.setForeground(Color.green);
TombolTambahKurang.setFont(Bold);
KalkulOgut.add(TombolTambahKurang);

Button Pembersih = new Button("C");
Pembersih.setBackground(Color.red);
Pembersih.addActionListener(e1);
Pembersih.setBounds(370,134,60,34);
Pembersih.setForeground(Color.white);
Pembersih.setFont(Bold);
KalkulOgut.add(Pembersih);

Button persenBae = new Button("%");
persenBae.setBackground(Color.gray);
persenBae.addActionListener(e1);
persenBae.setBounds(370,172,60,34);
persenBae.setForeground(Color.green);
persenBae.setFont(Bold);
KalkulOgut.add(persenBae);

Button satu_Dibagi_X = new Button("1/x");
satu_Dibagi_X.setBackground(Color.gray);
satu_Dibagi_X.addActionListener(e1);
satu_Dibagi_X.setBounds(370,210,60,34);
satu_Dibagi_X.setForeground(Color.green);
satu_Dibagi_X.setFont(Bold);
KalkulOgut.add(satu_Dibagi_X);

Button SamiSareng = new Button("=");
SamiSareng.setBackground(Color.gray);
SamiSareng.addActionListener(e1);
SamiSareng.setBounds(370,248,60,34);
SamiSareng.setForeground(Color.green);
SamiSareng.setFont(Bold);
KalkulOgut.add(SamiSareng);
KalkulOgut.setVisible(true);
}
public static void main(String args[])
{

CalculatorH2_DIIITKJ m1=new CalculatorH2_DIIITKJ();
m1.map_KalkulOgut();
}
String temp="";
String sign="";
double result=0;

//Membuat perubahan Hasil = sebelum 0

double temp_minus=0;
boolean flag_minus_first=true;
boolean mul_flag=true;
double mul_temp=1;
double memory;
boolean memory_flag=false;
boolean divide_flag=true;
double divide_temp=1;
boolean mr_flag=false;

///Penggunaan mekanisme untuk penggunaan memori;
boolean point_flag=true;

//boolean tambah_bagi_kurang_flag=true;
private class event_handler
implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
System.out.println(cmd);

//****************************************************************************

if(cmd=="+/-")
{
//Tambah_bagi_pengurangan_flag=true
//apabila pemakai telah menekan tombol +/- maka nilai dari temp akan ditampikan di layar
//tampilan tidak menyaring hasil dari variabel

if(temp!="" )
//&& plus_slash_minus_flag==true && )
{
if(Double.parseDouble(temp)>0)
{
temp=("KIRANG"+ temp);
System.out.println("temp="+temp);
TampilanLayar.setText(temp);
}
else if(Double.parseDouble(temp)<0)
{
temp=temp.substring(1,temp.length());
System.out.println("temp="+temp);

TampilanLayar.setText(temp);
}

//tambah_slash_kurang_flag=false;
}

//enters when user press +/- and value from result variable is showing
//on screen in that case temp is always empty conditionly
if(temp==" " )

//&& plus_slash_minus_flag==false)
{
String pm="";
pm=String.valueOf(result);
if(result>0)
{
pm="KIRANG"+pm;
result=Double.parseDouble(pm);
TampilanLayar.setText(String.valueOf(result));
pm="";
}
else if(result<0)
{
pm=pm.substring(1,pm.length());
result=Double.parseDouble(pm);
TampilanLayar.setText(String.valueOf(result));
pm="";
}
System.out.println("result="+result);
}
}

//****************************************************************************
//******************

if(cmd=="%")
{
point_flag=true;
if(temp!="")
{
result=result/Double.parseDouble(temp);
result=result*100;
TampilanLayar.setText(String.valueOf(result));
}
// kontruksi ada dibawah ini
}
//****************************************************************************
// kemandirian operasi

if(cmd=="1/x")
{
point_flag=true;
if(temp!="")
{
double byx=0;
byx=Double.parseDouble(temp);
byx=1/byx;
temp=String.valueOf(byx);
TampilanLayar.setText(temp);
}
if(temp=="")
{
result=1/result;
TampilanLayar.setText(String.valueOf(result));
}

//kontruksi dibawah ini juga coy...!
}
//****************************************************************************
//if(cmd=="+/-")
//{
//poin_flag=true;
//}
//****************************************************************************

if(cmd=="MC")
{
point_flag=true;
//Harus menambahkan flag comaon dalam semua operasi
//dan harus dicatat bila ada penambahan oprasi baru setelah di bawah ini
memory_flag=false;

//mempunyai makna tidak ada nilai yang di simpan dalam M+
memory=0;
Panggil_Simpanan.setEnabled(false);
Bersihkan_Simpanan.setEnabled(false);
}

//****************************************************************************
//******************

if(cmd=="M+")
{
point_flag=true;
if(mr_flag==false)

//yang di gunakan untuk menyimpan dari suatu hasil
if(temp!="")
{
memory_flag=true;
temp=TampilanLayar.getText();
memory=Double.parseDouble(temp);
mul_flag=false;
flag_minus_first=false;
divide_flag=false;
Panggil_Simpanan.setEnabled(true);
Bersihkan_Simpanan.setEnabled(true);
}
if(mr_flag==true)
{
System.out.println("ENTERED IN M+ where mr_flad=false");

memory_flag=true;
result=Double.parseDouble(TampilanLayar.getText());
memory=result;
mul_flag=false;
flag_minus_first=false;
divide_flag=false;
mr_flag=false;
Panggil_Simpanan.setEnabled(true);
Bersihkan_Simpanan.setEnabled(true);
}
}
//****************************************************************************
if(cmd=="MR")
{
point_flag=true;
if(memory_flag==true)
//pemeriksaan nomor berapa saja yang disimpan dengan penggunaan M+
{
temp=String.valueOf(memory);
TampilanLayar.setText(temp);
}
}
//****************************************************************************
if(cmd=="BAGI")
{
point_flag=true;
if(sign=="KIRANG" && temp!="")
{
result=result-
Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
divide_flag=false;
}
if(sign=="TAMBIH" && temp!="")
{
result=Double.parseDouble(temp)+result;
TampilanLayar.setText(String.valueOf(result));
temp="";
divide_flag=false;
}
if(sign=="KALI" && temp!="")
{
result=result*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
divide_flag=false;
}
sign="BAGI";
if(temp!="")
{
if(divide_flag==true)
{
divide_temp=Double.parseDouble(temp)/divide_temp;
System.out.println("Divide_temp="+divide_temp);
TampilanLayar.setText(String.valueOf(divide_temp));
temp="";
divide_flag=false;
result=divide_temp;
System.out.println("result="+result);
}
if(divide_flag==false && temp!="")
{
result=result/Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
System.out.println("result="+result);
temp="";
}
}
}
//****************************************************************************
//******************
if(cmd=="KALI")
{
point_flag=true;
if(sign=="BAGI" && temp!="")
{
result=result/Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
System.out.println("result="+result);
temp="";
mul_flag=false;
}
if(sign=="TAMBIH" && temp!="")
{
result=Double.parseDouble(temp)+result;
TampilanLayar.setText(String.valueOf(result));
temp="";
mul_flag=false;

//pengujian statemen beta
}
if(sign=="KIRANG" && temp!="")
{
result=result-Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
mul_flag=false;

/// pengujian statemen beta ;
}
sign="KALI";
if(temp!="")
{
if(mul_flag==true)
{
System.out.println("Entered in X where temp!=empty");
mul_temp=mul_temp*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(mul_temp));
temp="";
result=mul_temp;
System.out.println("result="+result);
mul_flag=false;
}
else
{
System.out.println("Entered in cmd=X and where mul_flag=false");
result=result*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));////////////////
temp="";
}
}
}

//****************************************************************************
//******************
if(cmd=="TAMBIH")
{
point_flag=true;
if(sign=="BAGI" && temp!="")
{
result=result/Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
System.out.println("result="+result);
temp="";
}
if(sign=="KALI" && temp!="")
{
result=result*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
}
if(sign=="KIRANG" && temp!="")

//e.g 2-2+4 for this like operation
{
result=result-Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
}
sign="TAMBIH";
if(temp!="")
{
mul_flag=false;
System.out.println("Entered in + temp!=empty ");
result=Double.parseDouble(temp)+result;
TampilanLayar.setText(String.valueOf(result));
temp="";
System.out.println("result="+result);
System.out.println(String.valueOf(temp));
}
else
{
}
}

//****************************************************************************
//******************
if(cmd=="KIRANG")
{
point_flag=true;
if(sign=="BAGI" && temp!="")
{
result=result/Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
System.out.println("result="+result);
temp="";
flag_minus_first=false;
}
if(sign=="KALI" && temp!="")
{
result=result*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
temp="";
flag_minus_first=false;
}
if(sign=="TAMBIH" && temp!="")

//jika pengguna memasukan 2-2+ atau nomer manapun
{
result=Double.parseDouble(temp)+result;
TampilanLayar.setText(String.valueOf(result));
temp="";
flag_minus_first=false;
}
sign="KIRANG";
if(temp!="")
{
mul_flag=false;
System.out.println(" Entered in - where temp!=empty ");
if(flag_minus_first==true)

//kapan pertama kali pemakai melaksanakan-operasi setelah mulai program
{
result=Double.parseDouble(temp)-result;
flag_minus_first=false;
System.out.println(result);
}
else
{
result=result-Double.parseDouble(temp);
System.out.println(result);
}
TampilanLayar.setText(String.valueOf(result));
temp="";
}
}

//****************************************************************************
if(cmd=="=")
{
point_flag=true;
if(sign=="TAMBIH")
if(temp!="")
{
mul_flag=false;
divide_flag=false;
flag_minus_first=false;

//pengujian kemungkinan pemindahan
result=Double.parseDouble(temp)+result;
TampilanLayar.setText(String.valueOf(result));
sign="";
temp="";
System.out.println("Hasil="+result);
mr_flag=true;

///penggunaan memori untuk pemanggilan mekanisme
}
if(sign=="KIRANG")
if(temp!="")
{
mul_flag=false;
divide_flag=false;
flag_minus_first=false;

//pengujian kemungkinan pemindahan
result=result-Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
sign="";
temp="";
System.out.println("result="+result);
mr_flag=true;

///penggunaan memori untuk pemanggilan mekanisme
}
if(sign=="KALI")
if(temp!="")
{
mul_flag=false;
divide_flag=false;
flag_minus_first=false;

//pengujian kemungkinan pemindahan
result=result*Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
sign="";
temp="";
mr_flag=true;

///penggunaan memori untuk pemanggilan mekanisme
//mul_temp=1;
}
if(sign=="BAGI")
if(temp!="")
{
mul_flag=false;
divide_flag=false;
flag_minus_first=false;

//pengujian kemungkinan pemindahan
result=result/Double.parseDouble(temp);
TampilanLayar.setText(String.valueOf(result));
sign="";
temp="";
mr_flag=true;

///penggunaan memori untuk pemanggilan mekanisme
}
if(sign=="")
if(temp!="")
//pengujian kemungkinan pemindahan
{
result=Double.parseDouble(TampilanLayar.getText());
System.out.println("result="+TampilanLayar.getText());
mul_flag=false;
mr_flag=true;
///penggunaan memori untuk pemanggilan mekanisme (tkj tea pikirkeunuen.just kidul..he.he.)
}
temp="";
}
//****************************************************************************
//******************

if(cmd=="."||cmd=="0" || cmd=="1" ||
cmd=="2" || cmd=="3" || cmd=="4" || cmd=="5" || cmd=="6" || cmd=="7" ||
cmd=="8" || cmd=="9")
{
if(cmd=="." && temp=="" && point_flag==true)
{
temp="0";
temp=temp+cmd;
point_flag=false;
}
else if(cmd=="." && temp!="" && point_flag==true)
{
temp=temp+cmd;
point_flag=false;
}
//temp=temp+cmd;
else if(cmd!=".")
temp=temp+cmd;
TampilanLayar.setText(temp);
System.out.println("temp"+temp);
mr_flag=false;
}

//****************************************************************************
//******************
if(cmd=="C")
{
//tambah_slash_minus_flag=true;
point_flag=true;
mr_flag=false;
divide_temp=1;
divide_flag=true;
mul_flag=true;
temp="";
TampilanLayar.setText("SALAM HANGAT MHS TKJ H2");
result=0;
sign="";
flag_minus_first=true;
mul_temp=1;
System.out.println("result="+result);
System.out.println("temp="+temp);
}

//****************************************************************************


}
}

}

Baca Selengkapnya......
Template by : kendhin x-template.blogspot.com