五子棋棋盘java实现

2024-07-18 21:37:45作者:饭克斯

其实我也有用JAVA做五子棋呢~,棋盘都是用画的,我把代码发下,你自己试下,也不定合你一意.事件代码我都去啦,因为是简单的麻烦事.~!

importjava.awt.*;

importjavax.swing.*;

@SuppressWarnings(serial)

publicclassChessBoardextendsJPanel{

/*

*制作棋盘的宽高;

*/

publicstaticfinalintBOARD_WIDTH=515;

/*

*计算棋盘表格坐标(单元格宽高相等)

*/

publicstaticint[]location=newint[22];

static{

for(inti=0,WIDTH=30;i<location.length;i++,WIDTH+=22){

location[i]=WIDTH;

}

}

publicChessBoard(intx,inty){

super(null);

this.setBounds(x,y,BOARD_WIDTH,BOARD_WIDTH);

this.setBackground(newColor(255,164,85));

}

/**

*重写方法,绘制棋盘表格图;

*/

publicvoidpaintComponent(Graphicsg){

super.paintComponent(g);

charch='A';

g.setFont(newFont(宋体,Font.BOLD,12));

//画横线

for(inti=0,width=30+22*21;i<location.length;i++,ch++){

g.setColor(Color.black);

g.drawLine(30,location[i],width,location[i]);

g.setColor(Color.blue);

g.drawString(+ch,5,location[i]+3);

}

//画竖线

for(inti=0,width=30+22*21;i<location.length;i++){

g.setColor(Color.black);

g.drawLine(location[i],30,location[i],width);

g.setColor(Color.blue);

g.drawString(+(i+1),location[i]-3,13);

}

}

}

展开全文

热门推荐

相关攻略

猜你喜欢