Java实现两人五子棋游戏(三) 画出棋子
作者:v_xchen_v 发布时间:2021-05-29 04:53:53
标签:java,五子棋
上一篇文章讲的是Java实现两人五子棋游戏(二) 画出棋盘,已经画好棋盘,接下来要实现控制功能,主要功能:
1)选择棋子
2)画棋子
3)判断胜负
4)交换行棋方
先实现画棋子PART
-------------画棋子代码示例如下--------------
首先,定义一个棋子类,这个类有两个属性,棋子颜色(0-表示黑色,1-表示白色),是否落子(我计划用一个二维数组才存储棋子的落子信息)
Chessman.java
package xchen.test.simpleGobang;
public class Chessman {
private int color;//1-white,0-black
private boolean placed = false;
public Chessman(int color,boolean placed){
this.color=color;
this.placed=placed;
}
public boolean getPlaced() {
return placed;
}
public void setPlaced(boolean placed) {
this.placed = placed;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
接着我们上一部分的画好棋盘的代码部分,新增画棋子的代码,我用两个棋子(一白一黑,分别位于棋盘的【8,8】,【7,7】)来检验画棋子的代码
DrawChessBoard.java
package xchen.test.simpleGobang;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Color;
import javax.swing.JPanel;
public class DrawChessBoard extends JPanel{
final static int BLACK=0;
final static int WHITE=1;
public int chessColor = BLACK;
public Image boardImg;
final private int ROWS = 19;
Chessman[][] chessStatus=new Chessman[ROWS][ROWS];
public DrawChessBoard() {
boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png");
if(boardImg == null)
System.err.println("png do not exist");
//test draw chessman part simple
Chessman chessman=new Chessman(0, true);
chessStatus[7][7]=chessman;
Chessman chessman2 = new Chessman(1, true);
chessStatus[8][8]=chessman2;
//test draw chessman part simple
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
int imgWidth = boardImg.getHeight(this);
int imgHeight = boardImg.getWidth(this);
int FWidth = getWidth();
int FHeight= getHeight();
int x=(FWidth-imgWidth)/2;
int y=(FHeight-imgHeight)/2;
g.drawImage(boardImg, x, y, null);
int margin = x;
int span_x=imgWidth/ROWS;
int span_y=imgHeight/ROWS;
//画横线
for(int i=0;i<ROWS;i++)
{
g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y);
}
//画竖线
for(int i=0;i<ROWS;i++)
{
g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y);
}
//画棋子
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<ROWS;j++)
{
if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true)
{
System.out.println("draw chessman "+i+" "+j);
int pos_x=x+i*span_x;
int pos_y=y+j*span_y;
int chessman_width=20;
float radius_b=20;
float radius_w=50;
float[] fractions = new float[]{0f,1f};
java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE};
Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK};
RadialGradientPaint paint;
if(chessStatus[i][j].getColor()==1)
{
System.out.println("draw white chess");
paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w);
}else{
System.out.println("draw black chess");
paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b);
}
((Graphics2D)g).setPaint(paint);
((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width);
}
}
}
}
}
主模块代码不变
Main.java
package xchen.test.simpleGobang;
import java.awt.Container;
import javax.swing.JFrame;
import xchen.test.simpleGobang.DrawChessBoard;
public class Main extends JFrame{
private DrawChessBoard drawChessBoard;
public Main() {
drawChessBoard = new DrawChessBoard();
//Frame标题
setTitle("单机五子棋");
Container containerPane =getContentPane();
containerPane.add(drawChessBoard);
}
public static void main(String[] args) {
Main m = new Main();
m.setSize(800, 800);
m.setVisible(true);
}
}
运行一下!
来源:https://blog.csdn.net/v_xchen_v/article/details/53431670


猜你喜欢
- 定义享元模式(FlyWeight Pattern),也叫蝇量模式,运用共享技术,有效的支持大量细粒度的对象,享元模式就是池技术的重要实现方式
- 多段颜色的进度条实现思路,供大家参考,具体内容如下这个进度条其实相对简单. 这里可以把需要绘制的简单分为两个部分1.灰色背景部分 2.多段颜
- IDEA 新手使用手册1 简介IDEA的全称是IntelliJ IDEA,这是一个java编程语言开发的集成环境。IDEA的每一个方面都是为
- PageAdapter是一个抽象类,直接继承于Object,导入包android.support.v4.view.PagerAdapter即
- 新添加个发文类型insert into mis.zyb_sf_type values('121','榆财法字'
- Java 8 Instant 时间戳用于“时间戳”的运算。它是以Unix元年(传统 的设定为UTC时区1970年1月1日午夜时分)开始 所经
- IntelliJ IDEA安装好以后,按说我们就要双击进行启动了,但在启动之前,我得给大家说一下IntelliJ IDEA安装以后的安装目录
- 1、UUID类库UUID 根据时间戳实现自动无重复字符串定义// 获取UUIDpublic static UUID randomUUID()
- namespace ConsoleApplication1{ using System; &n
- 前言在前面的2个章节中,一一哥 带大家实现了在Spring Security中添加图形验证码校验功能,其实Spring Security的功
- 本文以实例形式讲述了C#通过反射创建自定义泛型的实现方法,分享给大家供大家参考。具体如下:比如有这样一个泛型:Demo.GenericsSi
- ChatGPT的基本介绍ChatGPT是一个用来进行自然语言处理任务的预训练模型。要使用ChatGPT,需要了解以下几点:理解预训练模型:预
- 实践过程效果代码public partial class frmSend : Form{ public frmSe
- Hibernate中有HQL查询语法。但我们用得比较熟的还是数SQL语句,那么应该怎么来让Hibernate支持SQL呢?这个不用我们去考虑
- spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。原理是在发现
- Android中实现全屏、无标题栏的两种办法,另附Android系统自带样式的解释实现全屏无标题栏:1.在xml文件中进行配置 Androi
- RestTemplate未设置超时时间,导致RabbitMQ队列大量堆积,消费者假死,不进行消费,类似线程堵塞。排查:从日志排查问题,在从进
- Android webView加载数据时内存溢出今天使用webView加载数据时 如果数据太长就会崩溃,造成内存溢出,在网上查
- 摘要本文主要讲解mall通过整合SpringSecurity和JWT实现后台用户的登录和授权功能,同时改造Swagger-UI的配置使其可以
- 前言MongoDB是一款由C++编写的高性能、开源、无模式的常用非关系型数据库产品,是非关系数据库当 * 能最丰富、最像关系数据库的数据库。它