Java五子棋(人机版),昨天买的棋子今天就用不上了_五子棋人机


运行一下  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210719175418887.png)


### 绘制棋盘


**重写paint方法**



@Override
public void paint(java.awt.Graphics g) {
super.paint(g);
}


**绘制横竖相交接的线**  
 定义15行、15列



public static final int ROWS=15;
public static final int COLS=15;


**绘制网格**



//绘制网格
private void drawGrid(Graphics g) {
Graphics2D g_2d=(Graphics2D)g;
int start=26;
int x1=start;
int y1=20;
int x2=586;
int y2=20;
for (int i = 0; i < ROWS; i++) {
y1 = start + 40*i;
y2 = y1;
g_2d.drawLine(x1, y1, x2, y2);
}

y1=start;
y2=586;
for (int i = 0; i < COLS; i++) {
	x1 = start + 40\*i;
	x2 = x1;
	g_2d.drawLine(x1, y1, x2, y2);		
}

}


**绘制5个圆点**



//绘制5个黑点
private void draw5Point(Graphics g) {
//第1个点
g.fillArc(142, 142, 8, 8, 0, 360);
//第2个点
g.fillArc(462, 142, 8, 8, 0, 360);

//第3个点
g.fillArc(142, 462, 8, 8, 0, 360);
//第4个点
g.fillArc(462, 462, 8, 8, 0, 360);

//中心点
g.fillArc(302, 302, 8, 8, 0, 360);

}


**在paint方法里面调用以上2个方法**



@Override
public void paint(java.awt.Graphics g) {
super.paint(g);
//绘制网格
drawGrid(g);
//绘制5个黑点
draw5Point(g);
}


**棋盘已经绘制完成**  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210719180312404.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RrbTEyMzQ1Ng==,size_16,color_FFFFFF,t_70)


### 实现落子指示器


1. 创建指示器类



package main;

import java.awt.Color;
import java.awt.Graphics;

//指示器类
public class Pointer {
private int i=0;//二维下标i
private int j=0;//二维下标j
private int x=0;//坐标X
private int y=0;//坐标Y
private GamePanel panel=null;
private Color color=null;
private int h=40;//指示的大小
private boolean isShow=false;//是否展示
private int qizi = 0 ;//棋子类型 0:无 1:白棋 2:黑棋

public Pointer(int x,int y,int i,int j,Color color,GamePanel panel){
	this.x=x;
	this.y=y;
	this.i=i;
	this.j=j;
	this.panel=panel;
	this.color=color;
}
//绘制
void draw(Graphics g){
	Color oColor = g.getColor();
	if(color!=null){
		g.setColor(color);
	}
	if(isShow){
		//绘制指示器
		g.drawRect(x-h/2, y-h/2, h, h);
	}
	if(color!=null){//用完设置回去颜色
		g.setColor(oColor);
	}
}
//判断鼠标是否在指针范围内
boolean isPoint(int x,int y){
	//大于左上角,小于右下角的坐标则肯定在范围内
	if(x>this.x-h/2 && y >this.y-h/2
		&& x<this.x+h/2 && y <this.y+h/2){
		return  true;
	}
	return false;
}
public boolean isShow() {
	return isShow;
}
public void setShow(boolean isShow) {
	this.isShow = isShow;
}
public int getQizi() {
	return qizi;
}
public void setQizi(int qizi) {
	this.qizi = qizi;
}
public int getX() {
	return x;
}
public void setX(int x) {
	this.x = x;
}
public int getY() {
	return y;
}
public void setY(int y) {
	this.y = y;
}
public int getI() {
	return i;
}
public void setI(int i) {
	this.i = i;
}
public int getJ() {
	return j;
}
public void setJ(int j) {
	this.j &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值