Monday, January 14, 2013

Contoh Program Java


Java ini dibuat menggunakan Midlet degan output Pertambahan bola
Berikut adalah link yang dapat diambil untuk penjelasan coding dan cara pembuatannya untuk pemula
silahkan Download :)

http://www.4shared.com/rar/i0Fk9_Uo/Program_Java_MIDlet_Penambahan.html?


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

public class qwerty extends MIDlet
{
layar myLayar;

public qwerty ()
{
myLayar = new layar();
    }

public void startApp()
{
Display.getDisplay(this).setCurrent(myLayar);
}
public void destroyApp(boolean b) {notifyDestroyed();}
public void pauseApp(){}

}

class layar extends Canvas implements Runnable
{
int[] x, y;
int[] arahX, arahY;
int now;

public layar()
{
now = 0;
x = new int[5];
y = new int[5];
arahX = new int[5];
arahY = new int[5];

for (int i=0; i < 5; i++) {
x[i] = 0;
y[i] = 0;
arahY[i] = 0;
arahX[i] = 0;
}

Thread th = new Thread(this);
th.start();
}

public void paint(Graphics g)
{
g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);      
       
        for (int i=0; i < 5; i++) {
       if (now == i) g.setColor(0x00ff00);
       else g.setColor(0x0000ff);
       g.fillRoundRect(x[i], y[i], 10, 10, 10, 10);
        }
    }
       
    public void run()
    {
   while (true)
   {
   for (int i=0; i < 5; i++) {
   x[i]+=arahX[i];  
   y[i]+=arahY[i];      
 
   if ((x[i] > getWidth()) || (x[i] < 0)) arahX[i] = -arahX[i];
   if ((y[i] > getHeight()) || (y[i] < 0)) arahY[i] = -arahY[i];
   }      
     
   try {
   Thread.sleep(20);
   } catch (Exception e){};
   repaint();
    }
    }
   
    public void keyPressed(int key) {
   switch(key) {
   case KEY_NUM5:
       now++;
       if (now > 4) now = 0;
    arahX[now] = 1;
    arahY[now] = 1;
    break;
   case KEY_NUM2:
       now++;
       if (now > 4) now = 0;
    arahX[now] = 0;
    arahY[now] = -1;
    break;
   case KEY_NUM4:
       now++;
       if (now > 4) now = 0;
    arahX[now] = -1;
    arahY[now] = 0;
    break;
   case KEY_NUM6:
       now++;
       if (now > 4) now = 0;
    arahX[now] = 1;
    arahY[now] = 0;
    break;
   case KEY_NUM8:
       now++;
       if (now > 4) now = 0;
    arahX[now] = 0;
    arahY[now] = 1;
    break;
   }
    }
}

3 comments:

  1. scriptnya mutlak harus begini ga?

    ReplyDelete
  2. rizal : iya gan sayajuga baru belajar dan ini tugas tututan hheheh setau saya outputnya berhasil scritnya seperti ini :D

    ReplyDelete