Java实现两人五子棋游戏(三) 画出棋子

作者:v_xchen_v 时间:2021-05-29 04:53:53 

上一篇文章讲的是Java实现两人五子棋游戏(二) 画出棋盘,已经画好棋盘,接下来要实现控制功能,主要功能:

1)选择棋子

2)画棋子

3)判断胜负

4)交换行棋方

先实现画棋子PART

-------------画棋子代码示例如下--------------

Java实现两人五子棋游戏(三) 画出棋子

首先,定义一个棋子类,这个类有两个属性,棋子颜色(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);
}
}

运行一下!

Java实现两人五子棋游戏(三) 画出棋子

来源:https://blog.csdn.net/v_xchen_v/article/details/53431670

标签:java,五子棋
0
投稿

猜你喜欢

  • c#队列Queue学习示例分享

    2022-06-30 13:39:45
  • C# 获取客户端IPv4地址的示例代码

    2022-09-12 12:39:34
  • Maven构建生命周期详细介绍

    2023-04-06 13:02:59
  • 带大家认识Java语法之泛型与通配符

    2021-06-04 06:14:46
  • Java中流的有关知识点详解

    2022-07-21 15:38:21
  • JAVA中Comparable接口和自定义比较器示例讲解

    2023-11-20 22:16:32
  • 深入多线程之:用Wait与Pulse模拟一些同步构造的应用详解

    2021-07-09 06:14:19
  • DevExpress实现GridView当无数据行时提示消息

    2023-08-23 04:13:33
  • java中如何使用HttpClient调用接口

    2021-10-13 11:19:14
  • spring中向一个单例bean中注入非单例bean的方法详解

    2022-07-19 13:14:18
  • C#有效防止同一账号多次登录(附三种方法)

    2023-05-23 10:32:45
  • Mono for Android 实现高效的导航(Effective Navigation)

    2023-09-05 21:39:56
  • Java 数据结构与算法系列精讲之KMP算法

    2023-05-06 14:55:55
  • Flutter自定义圆盘取色器

    2023-07-05 23:55:43
  • 解决springMVC 跳转js css图片等静态资源无法加载的问题

    2022-04-13 04:06:51
  • Spingboot JPA CriteriaBuilder 如何获取指定字段

    2022-07-20 15:40:22
  • Java常用的八种排序算法及代码实现+图解

    2022-04-09 13:30:06
  • Java中实现获取路径的方法汇总

    2022-12-19 08:10:54
  • Android Zxing 转换竖屏扫描且提高识别率的方法

    2022-06-19 13:18:12
  • Android RecyclerView实现水平、垂直方向分割线

    2023-07-24 15:40:35
  • asp之家 软件编程 m.aspxhome.com