上手练习一下五子棋,倡导在做之前先做出一个计划,才不会稀里糊涂。
具体操作步骤如下:
-
创建一个界面类,在里面创建界面和添加按钮对象,监听器等等。
-
建立一个监听类,监听器类用来声明接口,接口包括动作监听器,鼠标动作监听器,还会声明一个用来防止数值的接口(之后会创建)。
-
建立一个接口,用来获取数值(因为JAVA只能继承一个父类,所以接口会比较自由一些,接口是框架的重要组成部分)。
-
再创建一个类,用来重绘和绘制棋盘和棋子(分开写会比较清晰)
界面类:
public class GoBang extends JFrame implements GoBangconfig {
GBListener gL=new GBListener();//创建监听器对象
public void initUI() {
//创建界面的方法
//JFrame jf=new JFrame();
Shape jf=new Shape();//由于Shape类继承了界面,所以可以用Shape类来创建对象。
jf.setTitle("五子棋游戏");
jf.setSize(1000,1000);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体中
jf.setLocationRelativeTo(null);
jf.setLayout(null);
jf.getContentPane().setBackground(Color.darkGray);//设置窗体颜色
JButton jButton1=new JButton("开始游戏");
jButton1.setBounds(800,200,90,30);
jf.add(jButton1);
jButton1.addActionListener(gL);
JButton jButton2=new JButton("认输");
jButton2.setBounds(800,280,90,30);
jf.add(jButton2);
jButton2.addActionListener(gL);
JButton jButton3=new JButton("悔棋");
jButton3.setBounds(800,360,90,30);
jf.add(jButton3);
jButton3.addActionListener(gL);
jf.setVisible(true);
}
界面类也可以添加弹窗来进行提示,但注意了这步操作是需要刷新操作的.
构建弹窗操作:
JOptionPane.showMessageDialog(null,"");
监听器类:
public class GBListener implements MouseListener,GoBangconfig, ActionListener {
Graphics g;
JFrame jf;
int x1,y1,x2,y2;
int count=1;//颜色
public int [][] isAvail=new int [16][16];//在这里定义一个二维数组为棋子的落子情况
int begin;//开始游戏
int flag=1;
int xx,yy;//行和列
int Setx[]=new int[row*row];
int Sety[]=new int[column*column];
int index;
public void actionPerformed(ActionEvent e){
String gbStr=e.getActionCommand();
//开始游戏,source
switch (gbStr){
case"开始游戏":
begin=2;//开始游戏
jf.repaint();//记住一定要刷新
break;
case"悔棋":
System.out.println("index"+index);
jf.repaint();
break;
case"认输":
jf.repaint();
break;
System.out.println(gbStr+"被点击了");
}
public void mouseClicked(MouseEvent e){
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
if (begin == 2) {
//计算棋子的交值点
if ((x1 - x) % size < size / 2) {
xx = (x1 - x) / size;
} else if ((x1 - x) % size > size / 2) {
xx = (x1 - x) / size + 1;
}
if ((y1 - y) % size < size / 2) {
yy = (y1 - y) / size;
} else if ((y1 - y) % size > size / 2) {
yy = (y1 - y) / size + 1;
}
if (xx>=0&&xx<=(row-1)&&yy>=0&&yy<=(column-1)){
//当该位置没有棋子的时候,可以下棋
if (isAvail[xx][yy] == 0) {
if (count == 1){
//count=1为黑棋
isAvail[xx][yy] = 1;
g.setColor(Color.BLACK);
count++;
} else if (count == 2) {
//count=1时为白棋
g.setColor(Color.WHITE);
isAvail[xx][yy] = 2;
count--;
}
g.fillOval(xx * size + x - size / 2, yy * size + y - size / 2, size, size);
Setx[index] = xx;
Sety[index] = yy;
index++;
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
注意:重写接口,记住要重写接口内的方法。
接口类:
public interface GoBangconfig {
int x=50,y=75,size=40,row=15,column=15;
}
shape类:用来重绘和绘制棋盘
public class Shape extends JFrame implements GoBangconfig {
Shape shape;
int count;
// Shape jf;
int countx,county,x1,y1;
public int [][] isAvail;
public void paint(Graphics g){
super.paint(g);
//首先重绘棋盘
g.setColor(Color.pink);//设置画笔颜色
for (int i=0;i<row;i++){
g.drawLine(x,y+size*i,x+size*(column-1),y+size*i);
}
for (int j=0;j<column;j++){
g.drawLine(x+size*j,y,x+size*j,y+size*(row-1));
}
//在这里实现重绘棋子的功能,遍历
for (int i=0;i<isAvail.length;i++){
for (int j=0;j< isAvail.length;j++){
int arr=isAvail[i][j];
if (arr!=0) {
if (arr == 1) {
g.setColor(Color.BLACK);
g.fillOval(i * size + x - size / 2, j * size + y - size / 2, size, size);
} else if (arr == 2) {
g.setColor(Color.WHITE);
g.fillOval(i * size + x - size / 2, j * size + y - size / 2, size, size);
}
}
}
}
}
}
在界面类里,是要传递数值的,不然无法实现,包括重绘(重绘需要将监听器里的数组传递给界面类的数组),传递监听器。
Graphics g=jf.getGraphics();
//调用画笔
gL.g=g;
gL.jf=jf;
jf.addMouseListener(gL);
jf.isAvail=gL.isAvail;//将监听器的数组传递给界面
}
在这篇文章需要实现的有绘制棋盘,在监听类重写接口的方法,鼠标按下获取坐标,矫正坐标的位置,棋子的形状为实心圆,重绘了棋子。
对了千万不要忘了创建一个主类,不然实现不了!!!