import com.nttdocomo.ui.Canvas; |
import com.nttdocomo.ui.Display; |
import com.nttdocomo.ui.Graphics; |
import com.nttdocomo.ui.IApplication; |
|
public class TemplateAppli extends IApplication implements Runnable { |
|
int FIELD_WIDTH = 12; |
int FIELD_HEIGHT = 22; |
|
Thread thread; |
int[][] field = new int[FIELD_HEIGHT][FIELD_WIDTH]; |
|
Canvas canvas; |
|
public void start() { |
canvas = new Canvas() { |
public void paint(Graphics g) { |
// このメソッドは利用しないので実装する必要はない |
} |
}; |
Display.setCurrent(canvas); |
thread = new Thread(this); |
thread.start(); |
} |
|
public void resume() { |
// このイベントは利用しないので実装する必要はない |
} |
|
public void run() { |
initField(); |
Graphics g = canvas.getGraphics(); |
while (true) { |
g.lock(); |
drawBlocks(g); |
g.unlock(true); |
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(Graphics.getColorOfName(Graphics.GRAY)); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else { |
g.setColor(Graphics.getColorOfName(Graphics.BLACK)); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} |
} |
} |
} |
|
void sleep(long millis) { |
try { |
Thread.sleep(millis); |
} catch (InterruptedException e) { |
// 例外は発生しない |
} |
} |
|
} |