代码如下:
chess类
package chess;
import java.awt.Point;
/**
* 棋子类,用来保存棋子的信息,包括棋子的位置(Point p),棋子的颜色(flag,1为白,0为黑)
* @author dzr
*
*/
public class Chess
{
Point p;
int flag = 0;
/**
* 默认构造函数
*/
public Chess()
{
p = new Point();
}
/**
* 根据棋子的坐标构造棋子
* @param p 棋子坐标
*/
public Chess(Point p)
{
this.p = p;
}
public Chess(Point p,int flag)
{
this.p = p;
this.flag = flag;
}
}
棋盘类:
package chess;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
public class ChessBoard extends JFrame
{
/**
*
*/
Chess chess;
private static final long serialVersionUID = 1L;
final int len = 30; //棋盘之间的间隔
public ChessBoard()
{
chess = new Chess();
this.setTitle("五子棋");
this.setResizable(false);
this.setSize(600, 600);
int screenX = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenY = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(screenX/2-300,screenY/2-300);
//设置背景颜色
Container container = this.getContentPane();
container.setBackground(Color.yellow);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addMouseListener(new MyMouseClick());
}
/**
* 鼠标点击事件响应类
* @author dzr
*
*/
final class MyMouseClick extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
System.out.println(e.getPoint().toString());
chess.p = new Point();
chess.p = e.getPoint();
chess.flag= 1;
repaint();
}
}