棋 五子棋 九宫棋 棋代码
通过五子棋代码改编,主要用画图类实现的
public void paint(Graphics g)
{
super.paint(g);
// Graphics g2 = this.getGraphics();
// g2.setColor(Color.red);
//
// for (int i = 0; i < 4; i++) {
// g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
// g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
// }
//调用父类的JApplet的paint方法在JApplet容器中把g对象画上。少了这一句就显示不出来其他的控件
Graphics g1 = this.getGraphics();
Graphics g2 = this.getGraphics();
// BufferedImage bi = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
// Graphics g1 =bi.createGraphics();
g1.drawImage(image_back, 0, 0, 500, 500, null);
g1.setColor(Color.black);
g1.drawLine(0,475,500,475);
g2.setFont(new Font("黑体",Font.BOLD,20));
g2.setColor(Color.red);
g2.drawString(message,398,420);
//绘制棋盘
for(int i=0;i<4;i++)
{
g1.drawLine(15,95+120*i,375,95+120*i);
g1.drawLine(15+120*i,95,15+120*i,455);
}
// g1.drawImage(image_black, x,y, 120, 120, null);
for(int i=0;i<4;i++)//两个for循环是因为是二维数组来输出绘制棋子两个循环分别代表XY轴
{
for(int j=0;j<4;j++)
{
if(allChess[i][j]==1)//画出一个黑棋
{
int tempX = i*120+15;
int tempY = j*120+95;
g1.drawImage(image_black, tempX,tempY , 120, 120, null);
}
if(allChess[i][j]==2)//画出一个黑棋
{
int tempX = i*120+15;
int tempY = j*120+95;
g1.drawImage(image_whit, tempX,tempY , 120, 120, null);
}
}
}
}