cara membuat bola pantul pada netbean 7.0
ini scripnya.
1. Buat scrippt pada java class ..nama Bola1.klo sudah copy aja script dibawah ini :
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
public class Bola1 extends GameCanvas implements Runnable, PlayerListener {
private boolean isPlay;
private Image img1 ;
private int x = getWidth()/2, y = getHeight() / 2, arahx = 1, arahy = 1;
public static Player play;
private VolumeControl vc;
private static Player player;
public Bola1(int arahx, int arahy){
super(true);
this.arahx = arahx;
this.arahy = arahy;
try{
img1 = Image.createImage("/sipaku.png");
}catch(Exception e){}
}
public void start(){
isPlay = true;
try{
playMedia("bg.mid", "mid");
}catch(Exception e){}
Thread t = new Thread(this);
t.start();
}
public void stop() {
isPlay=false;
}
public void run() {
Graphics g = getGraphics();
while(isPlay){
DrawGame(g);
try{
Thread.sleep(50);
}catch(Exception e){}
}
}
private void DrawGame(Graphics g){
//bersihkan layar
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
//gambar bola
g.drawImage(img1, x, y, Graphics.HCENTER|Graphics.VCENTER);
//ubah arah
x += arahx;
y += arahy;
if(x >= getWidth() || x<=0) {
arahx *= - 1;
// suara memantul
try{
playMedia("bom.wav", "wav");
}catch(Exception e){}
}
if(y >= getHeight() || y<=0) {
arahy *= - 1;
// suara memantul
try{
playMedia("bom.wav", "wav");
}catch(Exception e){}
}
//tampilkan
flushGraphics();
}
private void playMedia(String file, String p) throws Exception
{
if(p.equals("wav")){
player = Manager.createPlayer(getClass().getResourceAsStream(file),"audio/x-wav");
}else if(p.equals("mid")){
player = Manager.createPlayer(getClass().getResourceAsStream(file),"audio/midi");
}
player.addPlayerListener(this);
player.setLoopCount(1);
player.prefetch();
player.realize();
vc = (VolumeControl)player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(100);
}
player.start();
}
public void playerUpdate(Player player, String event, Object eventData) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
2.buat Midlet nya ...nama Midlet ini scriptnya..
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
public class Midlet extends MIDlet implements CommandListener, PlayerListener {
private Display display;
private List list = new List("Daftar File", List.IMPLICIT);
private Command exit = new Command("Exit", Command.EXIT, 0),
play = new Command("Play", Command.OK, 0);
private VolumeControl vc;
private static Player player;
public void startApp() {
display = Display.getDisplay(this);
list.append("bg.mid", null);
list.append("explode.wav", null);
list.append("gambang.mid", null);
list.addCommand(exit);
list.addCommand(play);
list.setCommandListener(this);
display.setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
if(c == exit){
destroyApp(true);
notifyDestroyed();
}
else if(c == play) {
int pilih = list.getSelectedIndex();
switch(pilih) {
case 0 : {
try{
playMedia("bg.mid", "mid");
}catch(Exception e){}
break;
}
case 1 : {
No comments:
Post a Comment
Udah di baca kan.... kritik dan sarannya saya persilahkan ^_^..jangan lupa isi Buku tamunya juga ya...