Java 棋盘游戏代码优化:绘制棋盘线条问题排查
以下代码展示了 Java 棋盘游戏代码中可能导致线条显示不出来的几个常见问题,并提供了相应的排查建议:
public void drawChess(Graphics g) {
// g.setColor(Color.blue); // Graphics2D g2 = (Graphics2D)g; // g2.setStroke(new BasicStroke(3));//线条加粗
g.drawRect(point.x, point.y, Constant.shiftingX, Constant.shiftingX);
}
Chess[][] allChess = new Chess[10][9];
Chess selectedChess = null;//当前选中的棋子,默认为null
boolean isSide = true;//默认红方开始
@Override
public void paint(Graphics g) {//设置图片
g.drawImage(Images.getImage(Constant.Bg), 0, 0, null);
// g.drawImage(Images.getImage('images/chessBg.jpg'), 6, 10, null);
// g.setColor(Color.black);
// g.setFont(new Font("宋体",Font.BOLD,20));;
// g.drawString("車", 20, 42);
//绘制棋子
for(int i=0;i<allChess.length;i++) {
for(int j=0;j<allChess[i].length;j++) {
if(allChess[i][j]!=null) {
allChess[i][j].draw(g);
}
}
}
//选中时的背景
if(selectedChess!=null) {
selectedChess.drawChess(g);
}
}
public ChessPanel() {
initChess();
}
//初始化32个棋子
private void initChess() {
//上方黑棋
Chess blackCar1 = new Car(new Point(Constant.X,Constant.Y), Camp.黑方, Type.車);
Chess blackHorse1 = new Horse(new Point(Constant.X+Constant.shiftingX,Constant.Y), Camp.黑方, Type.馬);
Chess blackElephant1 = new Elephant(new Point(Constant.X+Constant.shiftingX*2,Constant.Y), Camp.黑方, Type.象);
Chess blackScholar1 = new Scholar(new Point(Constant.X+Constant.shiftingX*3,Constant.Y), Camp.黑方, Type.士);
Chess blackKing = new King(new Point(Constant.X+Constant.shiftingX*4,Constant.Y), Camp.黑方, Type.将);
Chess blackScholar2 = new Scholar(new Point(Constant.X+Constant.shiftingX*5,Constant.Y), Camp.黑方, Type.士);
Chess blackElephant2 = new Elephant(new Point(Constant.X+Constant.shiftingX*6,Constant.Y), Camp.黑方, Type.象);
Chess blackHorse2 = new Horse(new Point(Constant.X+Constant.shiftingX*7,Constant.Y), Camp.黑方, Type.馬);
Chess blackCar2 = new Car(new Point(Constant.X+Constant.shiftingX*8,Constant.Y), Camp.黑方, Type.車);
Chess blackCannon1 = new Cannon(new Point(Constant.X+Constant.shiftingX,Constant.Y+Constant.shiftingX*2), Camp.黑方, Type.砲);
Chess blackCannon2 = new Cannon(new Point(Constant.X+Constant.shiftingX*7,Constant.Y+Constant.shiftingX*2), Camp.黑方, Type.砲);
Chess blackSoldier1 = new Soldier(new Point(Constant.X,Constant.Y+Constant.shiftingX*3), Camp.黑方, Type.卒);
Chess blackSoldier2 = new Soldier(new Point(Constant.X+Constant.shiftingX*2,Constant.Y+Constant.shiftingX*3), Camp.黑方, Type.卒);
Chess blackSoldier3 = new Soldier(new Point(Constant.X+Constant.shiftingX*4,Constant.Y+Constant.shiftingX*3), Camp.黑方, Type.卒);
Chess blackSoldier4 = new Soldier(new Point(Constant.X+Constant.shiftingX*6,Constant.Y+Constant.shiftingX*3), Camp.黑方, Type.卒);
Chess blackSoldier5 = new Soldier(new Point(Constant.X+Constant.shiftingX*8,Constant.Y+Constant.shiftingX*3), Camp.黑方, Type.卒);
//添加黑棋
allChess[0][0] = blackCar1;
allChess[0][1] = blackHorse1;
allChess[0][2] = blackElephant1;
allChess[0][3] = blackScholar1;
allChess[0][4] = blackKing;
allChess[0][5] = blackScholar2;
allChess[0][6] = blackElephant2;
allChess[0][7] = blackHorse2;
allChess[0][8] = blackCar2;
allChess[2][1] = blackCannon1;
allChess[2][7] = blackCannon2;
allChess[3][0] = blackSoldier1;
allChess[3][2] = blackSoldier2;
allChess[3][4] = blackSoldier3;
allChess[3][6] = blackSoldier4;
allChess[3][8] = blackSoldier5;
//下方红棋
Chess redCar1 = new Car(new Point(Constant.X,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.車);
Chess redHorse1 = new Horse(new Point(Constant.X+Constant.shiftingX,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.馬);
Chess redElephant1 = new Elephant(new Point(Constant.X+Constant.shiftingX*2,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.象);
Chess redScholar1 = new Scholar(new Point(Constant.X+Constant.shiftingX*3,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.士);
Chess redKing = new King(new Point(Constant.X+Constant.shiftingX*4,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.将);
Chess redScholar2 = new Scholar(new Point(Constant.X+Constant.shiftingX*5,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.士);
Chess redElephant2 = new Elephant(new Point(Constant.X+Constant.shiftingX*6,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.象);
Chess redHorse2 = new Horse(new Point(Constant.X+Constant.shiftingX*7,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.馬);
Chess redCar2 = new Car(new Point(Constant.X+Constant.shiftingX*8,Constant.Y+Constant.shiftingX*9), Camp.红方, Type.車);
Chess redCannon1 = new Cannon(new Point(Constant.X+Constant.shiftingX,Constant.Y+Constant.shiftingX*7), Camp.红方, Type.砲);
Chess redCannon2 = new Cannon(new Point(Constant.X+Constant.shiftingX*7,Constant.Y+Constant.shiftingX*7), Camp.红方, Type.砲);
Chess redSoldier1 = new Soldier(new Point(Constant.X,Constant.Y+Constant.shiftingX*6), Camp.红方, Type.卒);
Chess redSoldier2 = new Soldier(new Point(Constant.X+Constant.shiftingX*2,Constant.Y+Constant.shiftingX*6), Camp.红方, Type.卒);
Chess redSoldier3 = new Soldier(new Point(Constant.X+Constant.shiftingX*4,Constant.Y+Constant.shiftingX*6), Camp.红方, Type.卒);
Chess redSoldier4 = new Soldier(new Point(Constant.X+Constant.shiftingX*6,Constant.Y+Constant.shiftingX*6), Camp.红方, Type.卒);
Chess redSoldier5 = new Soldier(new Point(Constant.X+Constant.shiftingX*8,Constant.Y+Constant.shiftingX*6), Camp.红方, Type.卒);
//添加红棋
allChess[9][0] = redCar1;
allChess[9][1] = redHorse1;
allChess[9][2] = redElephant1;
allChess[9][3] = redScholar1;
allChess[9][4] = redKing;
allChess[9][5] = redScholar2;
allChess[9][6] = redElephant2;
allChess[9][7] = redHorse2;
allChess[9][8] = redCar2;
allChess[7][1] = redCannon1;
allChess[7][7] = redCannon2;
allChess[6][0] = redSoldier1;
allChess[6][2] = redSoldier2;
allChess[6][4] = redSoldier3;
allChess[6][6] = redSoldier4;
allChess[6][8] = redSoldier5;
}
//初始化完就加载
public void start() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//获取坐标位置
int x = e.getX();
int y = e.getY();
//获取下标位置
int x1 = (y-Constant.Y)/Constant.shiftingX;
int y1 = (x-Constant.X)/Constant.shiftingX;
System.out.println("("+x+","+y+"),["+x1+","+y1+"]");
//创建数组,来判断点的是哪个棋子
Chess clickChess = allChess[x1][y1];
System.out.println(clickChess);
//下棋的逻辑
if(clickChess!=null) {//判断是选子、吃子
if(selectedChess==null) {
if(isSide==true&&clickChess.getCamp()==Camp.红方||isSide==false&&clickChess.getCamp()==Camp.黑方) {
selectedChess = clickChess;
}
}
}else {
}
}
});
}
可能存在以下几个原因导致线条显示不出来:
- 绘制的线条颜色和背景颜色相同,导致线条无法被看到。 可以尝试修改线条颜色为与背景颜色不同的颜色。例如:
// 设置线条颜色为红色
g.setColor(Color.red);
-
绘制线条的坐标计算错误。 请确保
point.x和point.y的值在正确的范围内,并且线条的起始点和终点坐标是正确的。可以打印出这些坐标的值,检查它们是否符合预期。 -
绘制线条的宽度设置错误。 请确保线条的宽度大于0,可以尝试设置更大的宽度值。例如:
// 设置线条宽度为3像素
g.setStroke(new BasicStroke(3));
- 绘制线条的方法被注释掉或者没有调用。 请确保
drawChess方法中的g.drawRect方法被正确调用。可以删除drawChess方法的注释,或者在调用该方法的地方取消注释。
如果问题仍然存在,可以提供更多的代码和详细的错误描述,以便更好地帮助您解决问题。
原文地址: https://www.cveoy.top/t/topic/p9vb 著作权归作者所有。请勿转载和采集!