Java rootPane是什么意思?

Java rootPane是什么意思?,java,swing,graphics,Java,Swing,Graphics,这是我的第一个问题 我用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 Coord

这是我的第一个问题

我用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; 
} 
公共类ImageJoorichin扩展JApplet实现KeyListener{
图像我的图像,阿拉基;
boolean preViewImage=true;//在洗牌之前控制第一次预览
int n,//bord high&wight
blankX,//blankX坐标
blankY;//空白Y坐标
int[][]董事会;
公共void init(){
myImage=getImage(getCodeBase(),“下午12时54分的屏幕截图1391-12-22.png”);
String temp=JOptionPane.showInputDialog(根窗格,“输入电路板大小”,“电路板大小”,2);
n=整数.parseInt(温度);
board=新的整数[n][n];
temp=JOptionPane.showInputDialog(根窗格,“1-Easy\n2 Normal\n3 Hard”,“Level defficity”,2);
int-alaki=Integer.parseInt(temp),
numberOfShuffle=0;
开关(alaki){
案例1:
numberOfShuffle=n*n;
打破
案例2:
numberOfShuffle=n*n*n;
打破
案例3:
numberOfShuffle=n*n*n*n;
打破
}
重新油漆();
set();
设置大小(400400);
洗牌(numberOfShuffle);
设置聚焦(真);
requestFocus();
this.addKeyListener(this);
}
公开作废开始(){
//重新油漆();
}
//用于新设置和组织表格
公共无效集(){
blankX=blankY=n-1;
对于(int i=0;i0){
板[blankX][blankY]=板[blankX][blankY-1];
//板[blankX+1][blankY]=0;
--空白;
}      
}
公众假期{
如果(空白x0){
板[blankX][blankY]=板[blankX-1][blankY];
//板[blankX][blankY-1]=0;
--布兰克斯;
}      
}
公共无效洗牌(int numberOfShuffels){
for(int i=0;i
}

我尝试在洗牌之前显示图像的预览

但我不能! 我试着用
graphic.drawImage()
实现它,但它没有显示。。。
但是代码是有效的,因为如果我将其替换为
graphic.drawLine()
它将是有效的

请阅读上的Swing教程。您不应该修改JApplet的paint()方法。本教程还有一个关于“如何制作小程序”的部分,您应该看一下。看起来您对调用paint方法的方式和时间做出了假设:根据随机因素,可以多次调用paint,而这些随机因素是您无法控制的。此外,考虑扩展JCube和凌驾<代码> PruttCu饰件来执行自定义绘制(如果您只需要画一个IMAG)