日期:2014-05-20 浏览次数:20807 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FiveChessPanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11]; //定义11行11列的棋盘
boolean BlackChess ;
/**
* 构造方法
* 初始化棋盘数据
* 设置Panel面板属性
*/
public FiveChessPanel()
{
BlackChess = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this); //给鼠标注册监听器
setBackground(Color.gray);
setVisible(true);
}
/**
* 鼠标按下方法
*/
public void mousePressed(MouseEvent e)
{
//获取鼠标坐标
int x = e.getX();
int y = e.getY();
//计算是否超出棋盘边界
if(x < 30 || x > 360 ||y < 30 || y > 360)
{
JOptionPane.showMessageDialog(this, "超出棋盘边界");
return;
}
//计算此处是否有棋子,0为未下棋子
if(chess[x/30-1][y/30-1] != 0)
{
JOptionPane.showMessageDialog(this, "已有棋子");
return;
}
//////////////////////////////开始下棋,黑棋先下
if(BlackChess == true)
{
chess[x/30-1][y/30-1] = 1; //1为黑棋,2为白棋
BlackChess = false;
repaint(); //调用paint重绘方法
Justisewiner();
return;
}
if(BlackChess == false)
{
chess[x/30-1][y/30-1] = 2; //1为黑棋,2为白棋
BlackChess = true;
repaint();
Justisewiner();
return;
}
}
/**
* 画棋盘
* @param g
*/
public void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330); //画垂直线,设置每个点坐标
}
}
for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j); //画横线
}
}