import javax.microedition.lcdui.Canvas; |
import javax.microedition.lcdui.Display; |
import javax.microedition.lcdui.Graphics; |
import javax.microedition.lcdui.Image; |
import javax.microedition.midlet.MIDlet; |
|
public class TemplateMIDlet extends MIDlet implements Runnable { |
|
int FIELD_WIDTH = 12; |
int FIELD_HEIGHT = 22; |
|
Thread thread; |
int[][] field = new int[FIELD_HEIGHT][FIELD_WIDTH]; |
|
Image offi; |
Canvas canvas; |
|
int COLOR_GRAY = 0xC0C0C0; |
int COLOR_BLACK = 0x000000; |
|
public TemplateMIDlet() { |
canvas = new Canvas() { |
public void paint(Graphics g) { |
g.drawImage(offi, 0, 0, Graphics.TOP | Graphics.LEFT); |
} |
}; |
offi = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
Display.getDisplay(this).setCurrent(canvas); |
} |
|
protected void startApp() { |
if (thread == null) { |
thread = new Thread(this); |
thread.start(); |
} |
} |
|
protected void pauseApp() { |
// このイベントは利用しないので実装する必要はない |
} |
|
protected void destroyApp(boolean unconditional) { |
// このイベントは利用しないので実装する必要はない |
} |
|
public void run() { |
Graphics g = offi.getGraphics(); |
initField(); |
while (true) { |
drawBlocks(g); |
canvas.repaint(); |
canvas.serviceRepaints(); |
sleep(100); |
} |
} |
|
void initField() { |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
for (int j = 0; j < FIELD_WIDTH; j++) { |
if (j == 0 || j == FIELD_WIDTH - 1) { |
field[i][j] = -1; |
} else if (i == FIELD_HEIGHT - 1) { |
field[i][j] = -1; |
} else { |
field[i][j] = 0; |
} |
} |
} |
for (int i = 1; i < FIELD_WIDTH - 1; i++) { |
field[FIELD_HEIGHT - 1][i] = -1; |
} |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
field[i][0] = -1; |
field[i][FIELD_WIDTH - 1] = -1; |
} |
} |
|
void drawBlocks(Graphics g) { |
int blockSize = canvas.getHeight() / (FIELD_HEIGHT + 2); |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
for (int j = 0; j < FIELD_WIDTH; j++) { |
if (field[i][j] == -1) { |
g.setColor(COLOR_GRAY); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else { |
g.setColor(COLOR_BLACK); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} |
} |
} |
} |
|
void sleep(long millis) { |
try { |
Thread.sleep(millis); |
} catch (InterruptedException e) { |
// 例外は発生しない |
} |
} |
|
} |