AquariumSC

here is simple aquarium which has only a turtle in it
who is randomly moving and if you wish you can rotate it to directions



get AquariumSC
all source
jar



package paket;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* @author sertac
*/
public class AquariumSC extends MIDlet implements CommandListener {

Display d;
Form f;
Command cmdExit, cmdGoAquarium;
CanvasSC canvasSC;
Command cmdSelectAquarium;
List l;
Image[] backImages;
Image resim;
Image back;
private TextField tfThreadSpeed;
private ChoiceGroup cgPrintStats;
private ChoiceGroup cgPrintBounds;
private TextField tfBoundLimit;
private TextField tfFastMove;
private Command cmdHelp;

public AquariumSC() {
try {

d = Display.getDisplay(this);
f = new Form("AquariumSC");
f.setCommandListener(this);

cmdGoAquarium = new Command("Go", Command.OK, 1);
cmdExit = new Command("Exit", Command.EXIT, 2);
cmdSelectAquarium = new Command("Select", Command.ITEM, 2);
cmdHelp = new Command("Help", Command.SCREEN, 1);
f.addCommand(cmdHelp);

tfThreadSpeed = new TextField("Smoothness", "1000", 4, TextField.NUMERIC);
f.append(tfThreadSpeed);

cgPrintStats = new ChoiceGroup("Print Stats", ChoiceGroup.POPUP,new String[]{"Yes","No"},null);
cgPrintBounds = new ChoiceGroup("Print Bounds", ChoiceGroup.POPUP,new String[]{"Yes","No"},null);
f.append(cgPrintBounds);
f.append(cgPrintStats);

tfBoundLimit = new TextField("Aquarium Bounds", "5", 2, TextField.NUMERIC);
f.append(tfBoundLimit);

tfFastMove = new TextField("Fast Swim", "15", 2, TextField.NUMERIC);
f.append(tfFastMove);


f.addCommand(cmdGoAquarium);





backImages = new Image[2];
backImages[0] = Image.createImage("/back1.jpg");
backImages[1] = Image.createImage("/back2.jpg");

String[] backName = new String[2];
backName[0] = "Aquarium 1";
backName[1] = "Aquarium 2";

l = new List("Choose your AquariumSC", List.EXCLUSIVE, backName, backImages);
l.addCommand(cmdSelectAquarium);
l.addCommand(cmdExit);
l.setCommandListener(this);

} catch (IOException ex) {
ex.printStackTrace();
}


}

public void startApp() {
d.setCurrent(l);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == cmdGoAquarium) {

if (canvasSC == null) {

canvasSC = new CanvasSC();
}


canvasSC.setBoundLimit(Integer.parseInt(tfBoundLimit.getString()));
canvasSC.setPrintBoundLimits(cgPrintBounds.getSelectedIndex()==0?true:false);
canvasSC.setGg(Integer.parseInt(tfFastMove.getString()));
canvasSC.setThreadSpeed(Integer.parseInt(tfThreadSpeed.getString()));
canvasSC.setPrintStats(cgPrintStats.getSelectedIndex()==0?true:false);


d.setCurrent(canvasSC);

} else if (arg0 == cmdSelectAquarium) {

back = l.getImage(l.getSelectedIndex());
d.setCurrent(f);

} else if (arg0 == cmdExit) {
notifyDestroyed();
}
else if(arg0==cmdHelp){
d.setCurrent(new Alert("Tip","Press Fire for random swimming",null,AlertType.INFO), canvasSC);

}

}

class CanvasSC extends Canvas implements CommandListener, Runnable {

Command cmdBackToForm,cmdHelp;
int w, h;
int gx = -999;
int x, y;
Image[] resim;
int way;
boolean randomWay = true;
int boundLimit = 15; //kenarlardan ne kadar uzakda yuzsun
int gg = 15; // haraket uzaklik carpani
private int xx, yy;
private boolean printStats = true; // degerleri yazdir
private boolean printBoundLimits = true; // akvaryum limitini goster
private int threadSpeed = 500;

public void setBoundLimit(int boundLimit) {
this.boundLimit = boundLimit;
}

public void setPrintBoundLimits(boolean printBoundLimits) {
this.printBoundLimits = printBoundLimits;
}

public void setThreadSpeed(int threadSpeed) {
this.threadSpeed = threadSpeed;
}

public void setGg(int gg) {
this.gg = gg;
}

public void setPrintStats(boolean printStats) {
this.printStats = printStats;
}



public CanvasSC() {
try {

cmdBackToForm = new Command("Back", Command.BACK, 0);

this.addCommand(cmdBackToForm);

this.setCommandListener(this);
w = this.getWidth();
h = this.getHeight();

x = w / 2;
y = h / 2;

resim = new Image[4];
resim[0] = Image.createImage("/turtle0.gif");
resim[1] = Image.createImage("/turtle1.gif");
resim[2] = Image.createImage("/turtle2.gif");
resim[3] = Image.createImage("/turtle3.gif");

new Thread(this).start();

} catch (IOException ex) {
ex.printStackTrace();
}



}

protected void paint(Graphics g) {
try {


//Temizlik
g.setGrayScale(255);
g.fillRect(0, 0, w, h);
//

Random generator = new Random();
generator.setSeed(System.currentTimeMillis());



xx = Math.abs(generator.nextInt() * 100 % gg);
yy = Math.abs(generator.nextInt() * 100 % gg);

if (randomWay) {
way = generator.nextInt() * 100 % 4;

}



switch (way) {
case 0: //SAG
yy = generator.nextInt() * 100 % gg;
if (controlRightBound(x + xx) && controlDownBound(y + yy) && controlUpBound(y + yy)) {
x = x + xx;
y = y + yy;
}
break;
case 1://SOL
yy = generator.nextInt() * 100 % gg;
if (controlLeftBound(x - xx) && controlDownBound(y + yy) && controlUpBound(y + yy)) {
x = x - xx;
y = y + yy;
}
break;
case 2://YUKARI
xx = generator.nextInt() * 100 % gg;
if (controlRightBound(x + xx) && controlUpBound(y - yy) && controlLeftBound(x + xx)) {
x = x + xx;
y = y - yy;
}
break;
case 3://ASAGI
xx = generator.nextInt() * 100 % gg;
if (controlRightBound(x + xx) && controlDownBound(y + yy) && controlLeftBound(x + xx)) {
x = x + xx;
y = y + yy;
}
break;
}

g.drawImage(back, w / 2, h / 2, Graphics.HCENTER | Graphics.VCENTER);
g.drawImage(resim[way], x, y, Graphics.HCENTER | Graphics.VCENTER);

if (printBoundLimits) {
g.drawRect(0, 0, w, boundLimit);
g.drawRect(0, h - boundLimit, w, boundLimit);
g.drawRect(w - boundLimit, 0, boundLimit, h);
g.drawRect(0, 0, boundLimit, h);
}




if (printStats) {
g.setGrayScale(0);
g.drawString("way -> " + way, 5, 15, Graphics.BASELINE | Graphics.LEFT);
g.drawString("randomway -> " + String.valueOf(randomWay), 5, 25, Graphics.BASELINE | Graphics.LEFT);
g.drawString("x -> " + String.valueOf(x), 5, 35, Graphics.BASELINE | Graphics.LEFT);
g.drawString("y -> " + String.valueOf(y), 5, 45, Graphics.BASELINE | Graphics.LEFT);
g.drawString("xx -> " + String.valueOf(xx), 5, 55, Graphics.BASELINE | Graphics.LEFT);
g.drawString("yy -> " + String.valueOf(yy), 5, 65, Graphics.BASELINE | Graphics.LEFT);
}




Thread.sleep(100);



} catch (InterruptedException ex) {
ex.printStackTrace();
}



}

public void commandAction(Command arg0, Displayable arg1) {
if(arg0==cmdBackToForm){
d.setCurrent(l);
}

}

protected void keyPressed(int arg0) {

gx = getGameAction(arg0);
randomWay = false;

}

public void keyReleased(int key) {
gx = -999;

}

private boolean controlLeftBound(int x) {
if (x < randomway =" false;" way =" 0;"> w - boundLimit) {
randomWay = false;
way = 1;
return false;
} else {

return true;
}
}

private boolean controlUpBound(int y) {

if (y < randomway =" false;" way =" 3;"> h - boundLimit) {
randomWay = false;
way = 2;
return false;
} else {

return true;
}
}

public void run() {

while (true) {
switch (gx) {
case UP:
way = 2;

break;
case DOWN:
way = 3;


break;
case LEFT:
way = 1;


break;
case RIGHT:
way = 0;


break;
case FIRE:
randomWay = true;

}


repaint();

}
}
}
}

Comments

Popular posts from this blog

Pyppeteer fix for BrowserError: Browser closed unexpectedly

overlay filesystem and containers

How to add pagination to django comments for your model