`
fanjava
  • 浏览: 235871 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

MIDP图形设计5

阅读更多
  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 > tallyMsgWidth)
{
strWidth = statusMsgWidth;
}
// Get the
{
x, y
}
position for painting the strings. int x = (screenWidth - strWidth) / 2;
x = x < 0 ? 0 : x;
int y = (screenHeight - 2 * strHeight) / 2;
y = y < 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 > 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 >= 0) && (newCursorPosition < 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 < 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 < 4;
i++) {
g.fillRect(boardLeft, boardCellSize * i + boardTop, (boardCellSize * 3) + 2, 2);
g.fillRect(boardCellSize * i + boardLeft, boardTop, 2, boardCellSize * 3);
}
}
}
<!-- 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"; //--> <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"></iframe>
分享到:
评论

相关推荐

    Java手机游戏编程之MIDP图形设计篇

    ### Java手机游戏编程之MIDP图形设计篇 在探讨Java手机游戏编程中关于MIDP(Mobile Information Device Profile)图形设计的知识点时,我们首先需要理解MIDP及其在移动设备上的应用背景。MIDP是Java ME(Micro ...

    MIDP图形

    **MIDP图形技术在手机游戏中的应用** MIDP(Mobile Information Device Profile)是Java ME(Micro Edition)的一个子集,主要用于开发移动设备上的应用程序,尤其是早期的智能手机和平板电脑。在MIDP框架下,图形...

    MIDP图形编程简介.pdf

    **MIDP图形编程简介** MIDP(Mobile Information Device Profile)是Java ME(Micro Edition)的一部分,用于开发在移动设备和嵌入式系统上的应用程序,特别是早期的智能手机和平板电脑。这个平台提供了一套丰富的...

    Midp2Exe Midp2Exe

    Java ME(Midp)是Java平台的一个子集,设计用于资源有限的移动设备,如早期的智能手机和平板电脑。它包含了Java应用程序接口(API)和一个运行环境,使开发者能够创建跨平台的移动应用。然而,由于并非所有计算机...

    《MIDP手机游戏设计》

    《MIDP手机游戏设计》是一本专注于利用MIDP(Mobile Information Device Profile)平台进行手机游戏开发的专业书籍。MIDP是Java ME(Micro Edition)的一个子集,专为移动设备,特别是早期的智能手机和平板电脑设计...

    高效MIDP编程中文版

    2. **用户界面设计**:MIDP提供了一套有限的用户界面组件,如表单、按钮和文本字段。书籍会详细讲解如何使用这些组件构建简洁且功能丰富的用户界面,并讨论适配不同屏幕尺寸和方向的策略。 3. **事件处理**:MIDP中...

    深入MIDP2.0(2) - CLDC与MIDP工具类别

    5. **安全和隐私**:MIDP2.0包含了安全模型,如数字签名、权限管理,以保护用户的隐私和设备的安全。 6. **模拟器和调试工具**:文档可能会讨论如何使用MIDP的模拟器进行应用测试,以及使用J2ME Polish等工具进行...

    一个MIDP程序设计,王森主编,分享了!!

    4. **用户界面设计**:讲解如何使用MIDP的图形组件创建用户友好的界面,包括布局管理、事件处理和用户交互。 5. **数据管理**:可能包括本地数据存储,如RecordStore API,用于在设备上保存应用程序数据。 6. **...

    midp2.0教程

    MIDP2.0(Mobile Information Device Profile 2.0),作为Java ME(Java Micro Edition)的一部分,专为资源受限的移动设备设计,如手机、PDA等,提供了标准的Java API集,使得开发者能够构建丰富的应用和服务。...

    MIDP和游戏用户界面

    5. 超越MIDP APIs 5.1 我们需要什么? 随着技术的进步,游戏开发者和玩家对更高质量图形、更丰富的音效和更复杂的交互有了更高的期望。MIDP的API集在某些方面已经显得过时。 5.2 提供了哪些代表性功能? 为了满足更...

    MIDP 2.0 TheGame API

    5. **多媒体支持**:增强了音频和图像处理能力,能够更好地处理游戏中的声音和动画效果。 **TheGame API** 是MIDP 2.0中专为游戏开发设计的一系列类和接口。它可能包括以下组件: - **GameCanvas**:一个特殊的...

    MIDP1.0开发者指导

    5. **受限的多媒体支持**:虽然相比后来的版本,MIDP1.0对音频和图像的支持较为有限,但仍能实现基本的媒体播放功能。 6. **安全模型**:MIDP1.0有一套安全机制,限制了应用程序的权限,防止恶意软件。 **学习MIDP...

    MIDP 图形编程简介(中文)

    以一个简单的小游戏为例子,比较全面的介绍了MIDP图形编程方法。 包括以Screen为基础的高层界面和以Canvas为基础的底层界面开发都有涉及 目 录 1 引言...................................................... 5 ...

    Java移动通信程序设计-J2ME MIDP

    Java移动通信程序设计-J2ME MIDP是Java技术在移动设备上进行应用程序开发的一个关键领域。J2ME,全称为Java 2 Micro Edition,是Java平台的一个子集,专为资源有限的嵌入式设备,如手机、智能手表以及家用电器等设计...

    midp2.0 GameAPI实现

    在Java ME(J2ME)平台上,MIDP(Mobile Information Device Profile)是为移动设备设计的一个精简版Java API。MIDP 2.0是这个平台的一个重要升级,它引入了对游戏开发的强大支持,主要体现在Game API上。这个API...

    j2me的midp2.0所有源代码

    1. 用户界面:MIDP 2.0提供了一个轻量级的用户界面框架,包括Canvas类,它是绘制图形的基础;Form类,用于显示文本和用户输入;Item类,如ChoiceGroup和TextBox,支持用户交互。 2. 网络支持:MIDP 2.0引入了对HTTP...

    高效MIDP编程 学习MIDP

    - **MIDP**: Mobile Information Device Profile 的缩写,是Java ME的一部分,专为移动设备设计的标准平台,支持创建轻量级的应用程序和服务。 - **目标**: 本指南旨在提升MIDP应用程序的效率,涵盖执行速度、JAR...

    midp_samsung.zip

    - **Java MIDP** 是Java平台的一个子集,专为资源有限的移动设备设计。它包括用户界面组件、网络功能和数据存储接口,使得开发者可以创建交互式的移动应用。 - MIDP 2.0是其一个重要的版本,提供了更丰富的图形...

Global site tag (gtag.js) - Google Analytics