-1

*注意:*我是新来的。如果您要投反对票,请告诉我原因。

我正在使用 swing 编写一个 Java 国际象棋程序。我能够显示棋盘、初始化棋子并将它们存储在二维数组中。但是,我不知道如何在我的画布上显示这些作品。我在 Piece 类的第 65 行不断收到空指针错误。

*更新:*我已经包含了一些建议的更改。空指针错误已清除,但我仍然无法显示这些片段。我认为我没有正确地将它们指向我在国际象棋课上创建的画布。

我的程序分为三个类,如下所示:

国际象棋课

   import java.util.Scanner;

    import javax.swing.*;
    //import java.awt.*;


    public class Chess {
        public static final int WINDOW_WIDTH=600;
        public static final int WINDOW_HEIGHT=600;
        public static final int SQUARE_WIDTH = (WINDOW_WIDTH-10)/8;
        public static final int SQUARE_HEIGHT = (WINDOW_HEIGHT-40)/8;

        public static int position[][] = {};

        public BoardComponent mycanvas= new BoardComponent(this);

        public Chess()
        {

            JFrame mywindow;
            mywindow=new JFrame("ChessMaster 2012");
            mywindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mywindow.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

            //BoardComponent mycanvas= new BoardComponent(this);    
            mywindow.add(mycanvas);
            mywindow.setVisible(true);              //window appears here
        }

        public static void main(String[] args) {
            position = new int [8][8];

            new Chess();
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;

public class Piece extends JPanel{

    Piece[]  mypiece;

    public ImageIcon piece; 
    int nextID = 0;
    BoardComponent board;
    Chess chess;
    public int locx, locy;

    public void setCanvas(BoardComponent board)
    {
        this.board=board;
    }

    public Piece(char color, char Type, int posX, int posY){
        // each piece assigned a PK on creation, beginning sequentially from top left 
        // and looping back to the beginning of each row
        int pieceID = nextID;
        char pieceColor = color;
        char pieceType = Type;

        posX = locx;
        posY = locy;

        // P = pawn, K = knight, R = Rook, B = Bishop, Q = Queen, 
        //S = king (can't reuse K, so we use S instead)
        if (pieceType == 'P'){
            new Pawn(pieceColor);
        }
        else if (pieceType == 'K'){
            new Knight(pieceColor);
        }
        else if (pieceType == 'R'){
            new Rook(pieceColor);
        }
        else if (pieceType == 'B'){
            new Bishop(pieceColor);
        }
        else if (pieceType == 'Q'){
            new Queen(pieceColor);
        }
        else if (pieceType == 'S'){
            new King(pieceColor);
        }
        nextID ++;
        Chess.position[posX][posY] = pieceID;
        setCanvas(board);
        repaint();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawPiece(g);
    }

    public void drawPiece(Graphics g){
        g.drawImage(piece.getImage(),(locx*Chess.SQUARE_WIDTH),(locy*Chess.SQUARE_HEIGHT),null);

    }

    public class Pawn{

        public Pawn(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wpawn.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/bpawn.gif");
            }
        }
    }   

    public class Knight{

        public Knight(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wknight.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/bknight.gif");
            }
        }
    }   

    public class Rook{
        public Rook(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wrook.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/brook.gif");
            }
        }
    }

    public class Bishop{
        public Bishop(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wbishop.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/bbishop.gif");
            }
        }
    }

    public class Queen{
        public Queen(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wqueen.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/bqueen.gif");
            }
    }
    }

    public class King{
        public King(char color){
            if(color == 'w'){
                piece = new ImageIcon("src/gfx/wking.gif");
            }
            else{
                piece = new ImageIcon("src/gfx/bking.gif");
            }
        }
    }

    }

类板组件:

    import java.awt.*;
import javax.swing.*;

//This class draws the board and places the initial pieces
public class BoardComponent extends JComponent{
    Chess chess;

    public BoardComponent(Chess chessobject)
    {
        super();
        chess=chessobject;
    }

    @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    int rowCount = 0;
    int highCount = 0;
    int wideCount = 0;
    int squareCount = 0;
    ImageIcon piece;
    for(rowCount = 0; rowCount<8;rowCount++){

        for(int i = 0; i < 8; i++){
            if(squareCount%2==1){
                g.setColor(Color.ORANGE);
            }
            else{
                g.setColor(Color.darkGray);
            }
            g.fillRect(wideCount,highCount, Chess.SQUARE_WIDTH-5, Chess.SQUARE_HEIGHT-5);
            squareCount = squareCount + 1;
            wideCount = wideCount + Chess.SQUARE_WIDTH;
            g.setColor(Color.RED);
        }
        squareCount +=1;
        wideCount = 0;
        highCount = highCount + Chess.SQUARE_HEIGHT;
    }
    }
}

课件:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;

public class Piece extends JPanel{

Piece[]  mypiece;

public ImageIcon piece; 
int nextID = 0;
BoardComponent board;
Chess chess;
public int locx, locy;

public void setCanvas(BoardComponent board)
{
    this.board=board;
}

public Piece(char color, char Type, int posX, int posY){
    // each piece assigned a PK on creation, beginning sequentially from top left 
    // and looping back to the beginning of each row
    int pieceID = nextID;
    char pieceColor = color;
    char pieceType = Type;

    posX = locx;
    posY = locy;

    // P = pawn, K = knight, R = Rook, B = Bishop, Q = Queen, 
    //S = king (can't reuse K, so we use S instead)
    if (pieceType == 'P'){
        new Pawn(pieceColor);
    }
    else if (pieceType == 'K'){
        new Knight(pieceColor);
    }
    else if (pieceType == 'R'){
        new Rook(pieceColor);
    }
    else if (pieceType == 'B'){
        new Bishop(pieceColor);
    }
    else if (pieceType == 'Q'){
        new Queen(pieceColor);
    }
    else if (pieceType == 'S'){
        new King(pieceColor);
    }
    nextID ++;
    Chess.position[posX][posY] = pieceID;
    setCanvas(board);
    repaint();
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    drawPiece(g);
}

public void drawPiece(Graphics g){
    g.drawImage(piece.getImage(),(locx*Chess.SQUARE_WIDTH),(locy*Chess.SQUARE_HEIGHT),null);

}

public class Pawn{

    public Pawn(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wpawn.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/bpawn.gif");
        }
    }
}   

public class Knight{

    public Knight(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wknight.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/bknight.gif");
        }
    }
}   

public class Rook{
    public Rook(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wrook.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/brook.gif");
        }
    }
}

public class Bishop{
    public Bishop(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wbishop.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/bbishop.gif");
        }
    }
}

public class Queen{
    public Queen(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wqueen.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/bqueen.gif");
        }
}
}

public class King{
    public King(char color){
        if(color == 'w'){
            piece = new ImageIcon("src/gfx/wking.gif");
        }
        else{
            piece = new ImageIcon("src/gfx/bking.gif");
        }
    }
}

}

我对java很陌生,这真的让我陷入了困境。任何人都可以帮忙吗?

谢谢!

4

2 回答 2

3
  • drawPiece()出于什么原因不要从您的构造函数调用?我想repaint()可能是你需要的。

  • 不要使用getGraphics()它,因为它不会被初始化,直到添加面板并且如果我没记错的话第一次重绘完成。

  • 也永远不要忘记尊重油漆链,并在面板super.paintComponent(..)的覆盖方法中作为您的第一个调用。paintComponent(..)

  • 而是扩展JPanel而不是JComponent

我认为您的意思是在哪个地方调用drawPiece()paintComponent(..)应该将Graphics 对象传递给drawPiece()from ,paintComponent(..)如下所示:

public class Piece extends JPanel{



    public Piece(char color, char Type, int posX, int posY){

        // each piece assigned a PK on creation, beginning sequentially from top left 
        // and looping back to the beginning of each row
        ....

        // P = pawn, K = knight, R = Rook, B = Bishop, Q = Queen, 
        //S = king (can't reuse K, so we use S instead (from Shah, the historical name)) 
        ....
            nextID ++;
            Chess.position[posX][posY] = pieceID;
            repaint();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawPiece(g);
    }

    public void drawPiece(Graphics g){
        g.drawImage(piece.getImage(),(locx*Chess.SQUARE_WIDTH),(locy*Chess.SQUARE_HEIGHT),null);
    }
}

其他建议:

  • 通过将 UI 创建代码包装在EDT上创建JFrame和其他 Swing 组件

    SwingUtilities.invokeLater(new Runnable(){
        @Override 
        public void run() {
           //create ui here
        }
    });
    
  • 不要setSize(...)打电话JFrame

而是覆盖JPanel getPreferredSize()并返回适合组件等的适当大小,而不是在设置可见之前删除调用setSizeJFrame调用实例pack()JFrame

于 2012-12-03T08:50:14.363 回答
0

您的变量似乎board尚未初始化。你需要先调用setCanvas()来初始化它,然后你可以调用drawPiece().

于 2012-12-03T08:51:59.503 回答