---
title: "MIDP图形设计5"
author: "fanjava"
date: "2005-07-08T21:47:00Z"
tags: []
slug: "midp-5"
draft: false
lastmod: "2005-07-08T21:47:00Z"
id: 601703
url: "https://fanjava.iteye.com/blog/601703"
---

**5、GameScreen.java**

　　GameScreen使用了一个低级应用编程接口Canvas屏幕，和Image、Graphics类来绘制游戏面板、棋子，以及游戏的最终结果状态。要获取更详细的信息，请参阅各种绘画方法和drawCircle、drawCross、drawPiece、drawPlayerCursor、drawBoard等方法。这个屏幕使用MIDlet的quit回调方法来指示游戏结束。  
此屏幕可适应各种可用显示性能(高、宽、色彩等)。此外还要注意到可以使用四向导航键，也可以使用双向导航键来移动光标。

　　它使用了封装了主游戏程序逻辑的Game类。

package example.tictactoe;  
import java.util.Random;  
import javax.microedition.midlet.\*;  
import javax.microedition.lcdui.\*;  
class GameScreen extends Canvas implements CommandListener {  
private static final int BLACK = 0x00000000;  
private static final int WHITE = 0x00FFFFFF;  
private static final int RED = 0x00FF0000;  
private static final int BLUE = 0x000000FF;  
private static final int NO\_MOVE = -1;  
private final TicTacToeMIDlet midlet;  
private final Game game;  
private final Command exitCommand;  
private final Command newGameCommand;  
private final Random random = new Random();  
private int screenWidth, screenHeight;  
private int boardCellSize, boardSize, boardTop, boardLeft;  
private boolean playerIsCircle;  
private boolean computerIsCircle;  
private int preCursorPosition, cursorPosition;  
private int computerMove = NO\_MOVE;  
private int playerMove = NO\_MOVE;  
private int computerGamesWonTally = 0;  
private int playerGamesWonTally = 0;  
private boolean isRestart;  
public GameScreen(TicTacToeMIDlet midlet, boolean playerIsCircle) {  
this.midlet = midlet;  
this.playerIsCircle = playerIsCircle;  
computerIsCircle = !playerIsCircle;  
game = new Game(random);  
initializeBoard();  
// configure Screen commands  
exitCommand = new Command("Exit", Command.EXIT, 1);  
newGameCommand = new Command("New", Command.SCREEN, 2);  
addCommand(exitCommand);  
addCommand(newGameCommand);  
setCommandListener(this);  
// begin the game play initialize();  
}  
// Initialize the Game and Game screen. Also used for game restarts.  
private void initialize() {  
game.initialize();  
preCursorPosition = cursorPosition = 0;  
playerMove = NO\_MOVE;  
boolean computerFirst = ((random.nextInt() & 1) == 0);  
if (computerFirst) {  
computerMove = game.makeComputerMove();  
}  
else  
{  
computerMove = NO\_MOVE;  
}  
isRestart = true;  
repaint();  
}  
public void paint(Graphics g) {  
if (game.isGameOver()) {  
paintGameOver(g);  
}  
else {  
paintGame(g);  
}  
}  
private void paintGame(Graphics g) {  
if (isRestart) {  
// clean the canvas  
g.setColor(WHITE);  
g.fillRect(0, 0, screenWidth, screenHeight);  
drawBoard(g);  
isRestart = false;  
}  
drawCursor(g);  
if (playerMove != NO\_MOVE) {  
drawPiece(g, playerIsCircle, playerMove);  
}  
if (computerMove != NO\_MOVE) {  
drawPiece(g, computerIsCircle, computerMove);  
}  
}  
private void paintGameOver(Graphics g)

{  
String statusMsg = null;  
if(game.isComputerWinner()) {  
statusMsg = "I win !";  
computerGamesWonTally++;  
}  
else if (game.isPlayerWinner()) {  
statusMsg = "You win";  
playerGamesWonTally++;  
}  
else {  
statusMsg = "Stalemate";  
}  
String tallyMsg = "You:" + playerGamesWonTally + " Me:" + computerGamesWonTally;  
Font font = Font.getFont(Font.FACE\_SYSTEM, Font.STYLE\_PLAIN, Font.SIZE\_MEDIUM);  
int strHeight = font.getHeight();  
int statusMsgWidth = font.stringWidth(statusMsg);  
int tallyMsgWidth = font.stringWidth(tallyMsg);  
int strWidth = tallyMsgWidth;  
if (statusMsgWidth &gt; tallyMsgWidth)  
{  
strWidth = statusMsgWidth;  
}  
// Get the  
{  
x, y  
}  
position for painting the strings. int x = (screenWidth - strWidth) / 2;  
x = x &lt; 0 ? 0 : x;  
int y = (screenHeight - 2 * strHeight) / 2;  
y = y &lt; 0 ? 0 : y;  
// clean the canvas  
g.setColor(WHITE);  
g.fillRect(0, 0, screenWidth, screenHeight);  
// paint the strings' text  
g.setColor(BLACK);  
g.drawString(statusMsg, x, y, (Graphics.TOP | Graphics.LEFT));  
g.drawString(tallyMsg, x, (y + 1 + strHeight), (Graphics.TOP | Graphics.LEFT));  
}

public void commandAction(Command c, Displayable d) {  
if (c == exitCommand) {  
midlet.quit();  
}  
else if (c == newGameCommand) {  
initialize();  
}  
}  
private void initializeBoard() {  
screenWidth = getWidth();  
screenHeight = getHeight();  
if (screenWidth &gt; screenHeight) {  
boardCellSize = (screenHeight - 2) / 3;  
boardLeft = (screenWidth - (boardCellSize * 3)) / 2;  
boardTop = 1;  
}  
else {  
boardCellSize = (screenWidth - 2) / 3;  
boardLeft = 1;  
boardTop = (screenHeight - boardCellSize * 3) / 2;  
}  
}  
protected void keyPressed(int keyCode) {  
// can't continue playing until the player restarts  
if (game.isGameOver()) {  
return;  
}  
int gameAction = getGameAction(keyCode);  
switch (gameAction) {  
case FIRE: doPlayerMove();

break;  
case RIGHT: doMoveCursor(1, 0);  
break;  
case DOWN: doMoveCursor(0, 1);  
break;  
case LEFT: doMoveCursor(-1, 0);  
break;  
case UP: doMoveCursor(0, -1);  
break;  
default: break;  
}  
}  
private void doPlayerMove() {  
if (game.isFree(cursorPosition)) {  
// player move game.  
makePlayerMove(cursorPosition);  
playerMove = cursorPosition;  
// computer move  
if (!game.isGameOver()) {  
computerMove = game.makeComputerMove();  
}  
repaint();  
}  
}  
private void doMoveCursor(int dx, int dy) {  
int newCursorPosition = cursorPosition + dx + 3 * dy;  
if ((newCursorPosition &gt;= 0) && (newCursorPosition &lt; 9))

{  
preCursorPosition = cursorPosition;  
cursorPosition = newCursorPosition;  
repaint();  
}  
}  
// Draw a CIRCLE or CROSS piece on the board  
private void drawPiece(Graphics g, boolean isCircle, int pos) {  
int x = ((pos % 3) * boardCellSize) + 3;  
int y = ((pos / 3) * boardCellSize) + 3;  
if (isCircle) {  
drawCircle(g, x, y);  
}  
else {  
drawCross(g, x, y);  
}  
}  
// Draw blue CIRCLE onto the board image  
private void drawCircle(Graphics g, int x, int y) {  
g.setColor(BLUE);  
g.fillArc(x + boardLeft, y + boardTop, boardCellSize - 4, boardCellSize - 4, 0, 360);  
g.setColor(WHITE);  
g.fillArc(x + 4 + boardLeft, y + 4 + boardTop, boardCellSize - 4 - 8, boardCellSize - 4 - 8, 0, 360);  
}  
// Draw red CROSS onto the board image  
private void drawCross(Graphics g, int x, int y) {  
g.setColor(RED);  
for (int i = 0;  
i &lt; 4;  
i++) {  
g.drawLine(x + 1 + i + boardLeft, y + boardTop, x + boardCellSize - 4 - 4 + i + boardLeft, y + boardCellSize - 5 + boardTop);

g.drawLine(x + 1 + i + boardLeft, y + boardCellSize - 5 + boardTop, x + boardCellSize - 4 - 4 + i + boardLeft, y + boardTop);  
}  
}  
// Visually indicates a Player selected square on the board image  
private void drawCursor(Graphics g) {  
// draw cursor at selected Player square.  
g.setColor(WHITE);  
g.drawRect(((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop, boardCellSize - 3, boardCellSize - 3);  
// draw cursor at selected Player square.  
g.setColor(BLACK);  
g.drawRect(((cursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((cursorPosition/3) * boardCellSize) + 2 + boardTop, boardCellSize - 3, boardCellSize - 3);  
}  
private void drawBoard(Graphics g) {  
// clean the board  
g.setColor(WHITE);  
g.fillRect(0, 0, screenWidth, screenHeight);  
// draw the board  
g.setColor(BLACK);  
for (int i = 0;  
i &lt; 4;  
i++) {  
g.fillRect(boardLeft, boardCellSize * i + boardTop, (boardCellSize * 3) + 2, 2);  
g.fillRect(boardCellSize * i + boardLeft, boardTop, 2, boardCellSize * 3);  
}  
}  
}

&lt;!-- google\_ad\_client = "pub-3051157228350391"; google\_ad\_width = 728; google\_ad\_height = 90; google\_ad\_format = "728x90\_as"; google\_ad\_type = "text\_image"; google\_ad\_channel ="0045736275"; //--&gt; &lt;iframe name="google\_ads\_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3051157228350391&amp;dt=1120830351250&amp;format=728x90\_as&amp;output=html&amp;channel=0045736275&amp;ad\_type=text\_image&amp;cc=551&amp;u\_h=768&amp;u\_w=1024&amp;u\_ah=740&amp;u\_aw=1024&amp;u\_cd=32&amp;u\_tz=480&amp;u\_his=17&amp;u\_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="65535"&gt;&lt;/iframe&gt;