Kamis, 19 Mei 2011

MANUAL BOOK GAME LABIRIN BERBASIS JAVA NETBEANS 6.8

3IA11
DINA AMALIA (50408283)
NUR HANDAYANI (50408625)
RISTI MUSTIKA BRILIANTI (50408732)




Membuat Kode Program
Midlet
Labirin:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class labirin extends MIDlet implements CommandListener {
private canvas_labirin layar_kanvas;
private pilih layar_pilih;
private Command comm_keluar = new Command("Keluar", Command.EXIT, 99);
private Command comm_new = new Command("Level baru", Command.SCREEN, 1);
private Command comm_alert = new Command("Selesai", Command.EXIT, 1);
private Command comm_opsi = new Command("Pengaturan ukuran", Command.SCREEN, 1);
public labirin() {
try {
layar_kanvas = new canvas_labirin(Display.getDisplay(this));
layar_kanvas.addCommand(comm_keluar);
layar_kanvas.addCommand(comm_new);
layar_kanvas.addCommand(comm_opsi);
layar_kanvas.setCommandListener(this);
} catch(Exception e) {
Alert errorAlert = new Alert("error",
e.getMessage(), null, AlertType.ERROR);
errorAlert.setCommandListener(this);
errorAlert.setTimeout(Alert.FOREVER);
errorAlert.addCommand(comm_alert);
Display.getDisplay(this).setCurrent(errorAlert);
}
}
public void startApp() throws MIDletStateChangeException {
if(layar_kanvas != null) {
layar_kanvas.start();
}
}
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
layar_kanvas = null;
System.gc();
}
public void pauseApp() {
}
public void commandAction(Command c, Displayable s) {
if(c == comm_new) {
layar_kanvas.newMaze();
} else if(c == comm_alert) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
} else if(c == comm_opsi) {
if(layar_pilih == null) {
layar_pilih = new pilih(layar_kanvas);
}
Display.getDisplay(this).setCurrent(layar_pilih);
} else if(c == comm_keluar) {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
}
}

}

Java class:
Canvas_labirin:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class canvas_labirin extends javax.microedition.lcdui.Canvas {
private Display layar;
private pola_gambar garis;
private boolean gameover = false;
private int area;
private int x1;
private int x2;
private int x = 0;
private int y = 0;
private int yy;
private int xx;
private int yyh;
private int xxw;
private int Ox = 1;
private int Oy = 1;
private int Px = 1;
private int Py = 1;
int setColWidth(int colWidth) {
if(colWidth < 2) {
area = 2;
} else {
area = colWidth;
}
xx = getWidth() / area;
if(xx % 2 == 0) {
xx -= 1;
}
yy = getHeight() / area;
if(yy % 2 == 0) {
yy -= 1;
}
garis = null;
return(xx);
}
int getMinColWidth() {
return(x2);
}
int getMaxColWidth() {
return(x1);
}
int getMaxNumCols() {
return(yyh);
}
int getColWidth() {
return(area);
}
int getNumCols() {
return(xx);
}
public canvas_labirin(Display d) throws Exception {
layar = d;
int width = getWidth();
int height = getHeight();
area = 5;
x2 = 3;
yyh = width / x2;
if(yyh % 2 == 0) {
yyh -= 1;
}
xx = width / area;
if(xx % 2 == 0) {
xx -= 1;
}
yy = height / area;
if(yy % 2 == 0) {
yy -= 1;
}
xxw = 15;
x1 = width / xxw;
if(x1 > height / xxw) {
x1 = height / xxw;
}
if(x1 < area) {
throw(new Exception("error"));
}
}
void start() {
layar.setCurrent(this);
repaint();
}
void newMaze() {
gameover = false;
garis = null;
Px = 1;
Py = 1;
Ox = 1;
Oy = 1;
layar.setCurrent(this);
repaint();
}
protected void paint(Graphics g) {
if(garis == null) {
int width = getWidth();
int height = getHeight();
garis = new pola_gambar(xx, yy);
for(int i = 0; i < xx; i++) {
for(int j = 0; j < yy; j++) {
if(garis.area_xy[i][j] == 0) {
g.setColor(0,100,10);
} else {
g.setColor(255,255,200);
}
g.fillRect(x + (i*area),
y + (j*area),
area, area);
}
}
g.setColor(0,100,10);
g.fillRect(x + ((xx-1) * area),
y, width, height);
g.setColor(255,255,200);
g.fillRect(x + ((xx-1) * area),
y + ((yy-2) * area), width, height);
g.setColor(0,100,10);
g.fillRect(x,
y + ((yy-1) * area), width, height);
}
g.setColor(255, 100, 150);
g.fillRoundRect(x + (area)*Px,
y + (area)*Py,
area, area,
area, area);
if((Ox != Px) || (Oy != Py)) {
g.setColor(255,255,200);
g.fillRect(x + (area)*Ox,
y + (area)*Oy,
area, area);
}
if(gameover) {
int width = getWidth();
int height = getHeight();
Font font = g.getFont();
int fontHeight = font.getHeight();
int fontWidth = font.stringWidth("Berhasil");
g.setColor(255,255,200);
g.fillRect((width - fontWidth)/2, (height - fontHeight)/2,
fontWidth + 2, fontHeight);
g.setColor(255, 0, 0);
g.setFont(font);
g.drawString("Berhasil", (width - fontWidth)/2,
(height - fontHeight)/2,
g.TOP|g.LEFT);
}
}
public void keyPressed(int keyCode) {
if(! gameover) {
int action = getGameAction(keyCode);
switch (action) {
case LEFT:
if((garis.area_xy[Px-1][Py] == 1) &&
(Px != 1)) {
Ox = Px;
Oy = Py;
Px -= 2;
repaint();
}
break;
case RIGHT:
if(garis.area_xy[Px+1][Py] == 1) {
Ox = Px;
Oy = Py;
Px += 2;
repaint();
} else if((Px == garis.area_xy.length - 2) &&
(Py == garis.area_xy[0].length - 2)) {
Ox = Px;
Oy = Py;
Px += 2;
gameover = true;
repaint();
}
break;
case UP:
if(garis.area_xy[Px][Py-1] == 1) {
Ox = Px;
Oy = Py;
Py -= 2;
repaint();
}
break;
case DOWN:
if(garis.area_xy[Px][Py+1] == 1) {
Ox = Px;
Oy = Py;
Py += 2;
repaint();
}
break;
}
}
}
}

Pilih:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class pilih extends Form
implements ItemStateListener, CommandListener {
private Command kelaur = new Command("Selesai", Command.EXIT, 1);
private Gauge indikator;
private Gauge klomindikator;
private canvas_labirin xycanvas;
public pilih(canvas_labirin canvas) {
super("Pengaturan ukuran");
addCommand(kelaur);
setCommandListener(this);
xycanvas = canvas;
setItemStateListener(this);
indikator = new Gauge("Lebar", true,
xycanvas.getMaxColWidth(),
xycanvas.getColWidth());
klomindikator = new Gauge("Jumlah kolom", false,
xycanvas.getMaxNumCols(),
xycanvas.getNumCols());
append(indikator);
append(klomindikator);
}
public void itemStateChanged(Item item) {
if(item == indikator) {
int val = indikator.getValue();
if(val < xycanvas.getMinColWidth()) {
indikator.setValue(xycanvas.getMinColWidth());
} else {
int numCols = xycanvas.setColWidth(val);
klomindikator.setValue(numCols);
}
}
}
public void commandAction(Command c, Displayable s) {
if(c == kelaur) {
xycanvas.newMaze();
}
}
}

Pola_gambar:
import java.util.Random;
import java.util.Vector;
public class pola_gambar {
private Random acak_acak = new Random();
int[][] area_xy;
public pola_gambar(int width, int height) {
area_xy = new int[width][height];
for(int i = 1; i < width - 1; i++) {
for(int j = 1; j < height - 1; j++) {
if((i % 2 == 1) || (j % 2 == 1)) {
area_xy[i][j] = 1;
}
}
}
area_xy[0][1] = 1;
createMaze();
}
private void createMaze() {
for(int i = 1; i < area_xy.length - 1; i++) {
for(int j = 1; j < area_xy[i].length - 1; j++) {
if((i + j) % 2 == 1) {
area_xy[i][j] = 0;
}
}
}
for(int i = 1; i < area_xy.length - 1; i+=2) {
for(int j = 1; j < area_xy[i].length - 1; j+=2) {
area_xy[i][j] = 3;
}
}
Vector possibleSquares = new Vector(area_xy.length
* area_xy[0].length);
int[] startSquare = new int[2];
startSquare[0] = getRandomInt(area_xy.length / 2)*2 + 1;
startSquare[1] = getRandomInt(area_xy[0].length / 2)*2 + 1;
area_xy[startSquare[0]][startSquare[1]] = 2;
possibleSquares.addElement(startSquare);
while(possibleSquares.size() > 0) {
int chosenIndex = getRandomInt(possibleSquares.size());
int[] chosenSquare = (int[])possibleSquares.elementAt(chosenIndex);
area_xy[chosenSquare[0]][chosenSquare[1]] = 1;
possibleSquares.removeElementAt(chosenIndex);
link(chosenSquare, possibleSquares);
}
possibleSquares = null;
System.gc();
}
private void link(int[] chosenSquare, Vector possibleSquares) {
int linkCount = 0;
int i = chosenSquare[0];
int j = chosenSquare[1];
int[] links = new int[8];
if(i >= 3) {
if(area_xy[i - 2][j] == 1) {
links[2*linkCount] = i - 1;
links[2*linkCount + 1] = j;
linkCount++;
} else if(area_xy[i - 2][j] == 3) {
area_xy[i - 2][j] = 2;
int[] newSquare = new int[2];
newSquare[0] = i - 2;
newSquare[1] = j;
possibleSquares.addElement(newSquare);
}
}
if(j + 3 <= area_xy[i].length) {
if(area_xy[i][j + 2] == 3) {
area_xy[i][j + 2] = 2;
int[] newSquare = new int[2];
newSquare[0] = i;
newSquare[1] = j + 2;
possibleSquares.addElement(newSquare);
} else if(area_xy[i][j + 2] == 1) {
links[2*linkCount] = i;
links[2*linkCount + 1] = j + 1;
linkCount++;
}
}
if(j >= 3) {
if(area_xy[i][j - 2] == 3) {
area_xy[i][j - 2] = 2;
int[] newSquare = new int[2];
newSquare[0] = i;
newSquare[1] = j - 2;
possibleSquares.addElement(newSquare);
} else if(area_xy[i][j - 2] == 1) {
links[2*linkCount] = i;
links[2*linkCount + 1] = j - 1;
linkCount++;
}
}
if(i + 3 <= area_xy.length) {
if(area_xy[i + 2][j] == 3) {
area_xy[i + 2][j] = 2;
int[] newSquare = new int[2];
newSquare[0] = i + 2;
newSquare[1] = j;
possibleSquares.addElement(newSquare);
} else if(area_xy[i + 2][j] == 1) {
links[2*linkCount] = i + 1;
links[2*linkCount + 1] = j;
linkCount++;
}
}
if(linkCount > 0) {
int linkChoice = getRandomInt(linkCount);
int linkX = links[2*linkChoice];
int linkY = links[2*linkChoice + 1];
area_xy[linkX][linkY] = 1;
int[] removeSquare = new int[2];
removeSquare[0] = linkX;
removeSquare[1] = linkY;
possibleSquares.removeElement(removeSquare);
}
}
public int getRandomInt(int upper) {
int retVal = acak_acak.nextInt() % upper;
if(retVal < 0) {
retVal += upper;
}
return(retVal);
}
}



sumber: 12 aplikasi java mobile (maxicom)