*注意:*我是新来的。如果您要投反对票,请告诉我原因。
我正在使用 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很陌生,这真的让我陷入了困境。任何人都可以帮忙吗?
谢谢!