?? boardview.java
字號:
package example.jbombman;
import javax.microedition.lcdui.*;
public class BoardView extends Canvas {
private Board oBoard;
private Player oPlayer;
private Enemy oEnemy;
private final int BackgroundColor = 0xffffff;
private final int iCellSize = 20;
private final int iLeft = 10;
private final int iTop = 10;
public BoardView( Board board ) {
oBoard = board;
}
public void setPlayer( Player player ) {
oPlayer = player;
}
public void setEnemy( Enemy enemy ) {
oEnemy = enemy;
}
public void paint( Graphics g ) {
synchronized( g ) {
if( oBoard.isGameOver )
paintGameOver( g );
else if( oBoard.isWin )
paintWin( g );
else
paintBoard( g );
}
}
private void paintGameOver( Graphics g ) {
g.drawImage( Img.imgGameOver, 60, 100, Graphics.LEFT|Graphics.TOP );
}
private void paintWin( Graphics g ) {
g.drawImage( Img.imgCongratulation, 60, 100, Graphics.LEFT|Graphics.TOP );
}
private void paintFrame( Graphics g ) {
g.drawRect( 0, 0, 239, 239 );
g.drawRect( 1, 1, 237, 237 );
g.drawRect( 2, 2, 235, 235 );
g.drawRect( 3, 3, 233, 233 );
g.drawRect( 7, 7, 225, 225 );
g.drawRect( 8, 8, 223, 223 );
}
private void paintBoard( Graphics g ) {
if( g.getClipX() == 0 )
paintFrame( g );
int x = ( g.getClipX() - iLeft ) / iCellSize;
int y = ( g.getClipY() - iTop ) / iCellSize;
int w = g.getClipWidth() / iCellSize;
int h = g.getClipHeight() / iCellSize;
//System.out.println( "paint: x=" + x + ", y=" + y
// + ", w=" + w + ", h=" + h );
if( h>oBoard.iRows )
h = oBoard.iRows;
for( int i=x; i<x+w && i<oBoard.chBoard.length; i++ )
for( int j=y; j<y+h && j<oBoard.chBoard[i].length; j++ )
switch( oBoard.chBoard[i][j] ) {
case 'N': //N - None
g.setColor( BackgroundColor );
g.fillRect( iLeft + i*iCellSize, iTop + j*iCellSize,
iCellSize, iCellSize );
break;
case 'W': //W - Wall
g.drawImage( Img.imgWall, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
case 'L': //L - Pillar
g.drawImage( Img.imgPillar, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
case 'P': //P - Player
case 'U': //U - Bomb under player
g.drawImage( Img.imgPlayer, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
case 'E': //E - Enemy
g.drawImage( Img.imgEnemy, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
case 'B': //B - Bomb
g.drawImage( Img.imgBomb, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
case 'X': //X - Exploding
g.drawImage( Img.imgExploding, iLeft + i*iCellSize, iTop + j*iCellSize,
Graphics.LEFT|Graphics.TOP );
break;
}
}
public void keyPressed( int code ) {
if( oBoard.isGameOver||oBoard.isWin )
keyForInit( getGameAction(code) );
else
keyForPlay( getGameAction(code) );
}
private void keyForInit( int action ) {
if( action != FIRE )
return;
oBoard.init();
oEnemy.init();
oPlayer.init();
repaint();
}
private void keyForPlay( int action ) {
switch( action ){
case FIRE:
oPlayer.fire();
break;
case LEFT:
oPlayer.left();
break;
case RIGHT:
oPlayer.right();
break;
case UP:
oPlayer.up();
break;
case DOWN:
oPlayer.down();
break;
}
}
public void repaintCells( int x, int y, int w, int h ) {
if( x < 0 )
x = 0;
if( x >= oBoard.iCols )
x = oBoard.iCols - 1;
if( x + w > oBoard.iCols )
w = oBoard.iCols - x;
if( y < 0 )
y = 0;
if( y >= oBoard.iRows )
y = oBoard.iRows - 1;
if( y + h > oBoard.iRows )
h = oBoard.iRows - y;
repaint( iLeft + x*iCellSize, iTop + y*iCellSize,
w*iCellSize, h*iCellSize );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -