这是我的第一个问题
我使用java创建一个益智游戏:
public class ImageJoorichin extends JApplet implements KeyListener {
Image myImage,alaki;
boolean preViewImage = true; // Control First PreView before Shuffle
int n, // bord higth & wight
blankX, // Blank X Coordinates
blankY; // Blank Y Coordinates
int[][] board;
public void init() {
myImage = getImage( getCodeBase(), "Screen Shot 1391-12-22 at 12.54.54 PM.png" );
String temp = JOptionPane.showInputDialog(rootPane, "Enter Size of Board", "Board Size", 2);
n = Integer.parseInt( temp );
board = new int[ n ][ n ];
temp = JOptionPane.showInputDialog(rootPane, "1-Easy\n2-Normal\n3-Hard", "Level Defficulty", 2);
int alaki = Integer.parseInt( temp ),
numberOfShuffle = 0;
switch( alaki ) {
case 1:
numberOfShuffle = n * n;
break;
case 2:
numberOfShuffle = n * n * n;
break;
case 3:
numberOfShuffle = n * n * n * n;
break;
}
repaint();
set();
setSize( 400, 400 );
shuffle( numberOfShuffle );
setFocusable(true);
requestFocus();
this.addKeyListener(this);
}
public void start() {
//repaint();
}
// For New Set & Orginaze Table
public void set() {
blankX = blankY = n - 1;
for( int i = 0; i < board.length; i++ ) {
for( int j = 0; j < board[ i ].length; j++ ) {
board[ i ][ j ] = ( i * n ) + j;
}
}
}
public void right() {
if( blankY < n - 1 ) {
board[ blankX ][ blankY ] = board[ blankX ][ blankY + 1 ];
//board[ blankX ][ blankY - 1 ] = 0;
++blankY;
}
}
public void left() {
if( blankY > 0 ) {
board[ blankX ][ blankY ] = board[ blankX ][ blankY - 1 ];
//board[ blankX + 1 ][ blankY ] = 0;
--blankY;
}
}
public void down() {
if( blankX < n - 1 ) {
board[ blankX ][ blankY ] = board[ blankX + 1 ][ blankY ];
//board[ blankX ][ blankY - 1 ] = 0;
++blankX;
}
}
public void up() {
if( blankX > 0 ) {
board[ blankX ][ blankY ] = board[ blankX - 1 ][ blankY ];
//board[ blankX ][ blankY - 1 ] = 0;
--blankX;
}
}
public void shuffle( int numberOfShuffels ) {
for( int i = 0; i < numberOfShuffels; i++ ) {
int temp = ( int )( Math.random() * 4 ) + 1;
switch( temp ) {
case 1:
up();
break;
case 2:
down();
break;
case 3:
left();
break;
case 4:
right();
break;
default:
break;
}
}
}
public void paint( Graphics graphic ) {
super.paint( graphic );
myImage.getWidth( rootPane );
int distance = getWidth() / n;
if( preViewImage == true ) {
graphic.drawImage(myImage, 0, 0, 400, 400, this);
//graphics.drawLine( 0, 0, 100, 100 );
preViewImage = false;
return;
}
else {
for( int i = 0; i < board.length; i++ ) {
for( int j = 0; j < board[ i ].length; j++ ) {
int temp = board[ i ][ j ];
int I = temp / n; // SX
int J = temp % n; // SY
graphic.drawImage( myImage, j * distance, i * distance, ( j + 1 ) * distance, ( i + 1 ) * distance, J * distance, I * distance, ( J + 1 ) * distance, ( I + 1 ) * distance, this );
if( ! won() ) {
graphic.setColor(Color.YELLOW);
graphic.drawRect( j * distance, i * distance, distance, distance);
}
}
}
graphic.setColor(Color.GRAY);
graphic.fillRect( blankY * distance, blankX * distance, distance, distance);
if( !won() ) {
graphic.setColor(Color.YELLOW);
graphic.drawRect( blankY * distance, blankX * distance, distance, distance);
}
else {
graphic.setFont( new Font( "Tohama", Font.BOLD, 27) );
graphic.setColor(Color.RED);
int temp = ( getWidth() - graphic.getFontMetrics().stringWidth( "Congratulation" ) ) / 2;
graphic.drawString( "Congratulation", temp, getHeight() / 2 );
}
}
}
@Override
public void keyTyped( KeyEvent e ) {}
@Override
public void keyReleased( KeyEvent e ) {}
@Override
public void keyPressed( KeyEvent e ) {
switch( e.getKeyCode() ) {
case KeyEvent.VK_UP:
up();
break;
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_LEFT:
left();
break;
case KeyEvent.VK_RIGHT:
right();
break;
}
if( won() ) {
this.removeKeyListener( this );
}
repaint();
}
public boolean won() {
for( int i = 0; i < board.length; i++ ) {
for( int j = 0; j < board[i].length; j++ ) {
if( ( i != blankX || j != blankY ) && board[ i ][ j ] != i * n + j ) {
return false;
}
}
}
return true;
}
}
我尝试在随机播放之前显示图像预览。
但我不能!
我尝试用graphic.drawImage()
来做,但它没有显示......
但代码正在运行,因为如果我用graphic.drawLine()
替换它将会起作用