上一篇文章讲的是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); } }
运行一下!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Springboot项目因为kackson版本问题启动报错解决方案
Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解
网友评论