本文不讲算法 残缺棋盘可视化的思路如下:
由其算法可知 :除了残缺的棋子,其余的棋子都是有规律的3个连在一起,于是考虑用数组将其三个棋子用相同颜色绑定(棋子使用按钮控件表示)代码如下:
//给连续三个同色的棋子上色 颜色随机
public static JButton[] initButton() {
JButton[] jb =new JButton[3];
Color cr = Utils.getColor();
for(int i=0;i<=2;i++){
jb[i]=new JButton();
jb[i].setBackground(cr);
}
return jb;
}
根据坐标给残缺的棋子加上黑色
//给残缺的棋子涂上黑色
public static void colorButtorn(int x,int y) {
JButton jb = new JButton();
jb.setBounds((x)*width, (y)*height, width, height);
jb.setBackground(Color.BLACK);
jp.add(jb);
}
再根据算法将棋子加在面板上
if(dr<tr+s&&dc<tc+s)
{
// 获取三个一样颜色的按钮数组
// 给其设置边界
JButton[] jb = Myframe.initButton();
jb[0].setBounds((x-1)*width, (y)*height, width, height);
jb[1].setBounds((x)*width, (y-1)*height, width, height);
jb[2].setBounds((x)*width, (y)*height, width, height);
// 将按钮组加到面板中
Myframe.addToPanel(jb);
Cover(tr,tc,dr,dc,s);
Cover(tr,tc+s,tr+s-1,tc+s,s);
Cover(tr+s,tc,tr+s,tc+s-1,s);
Cover(tr+s,tc+s,tr+s,tc+s,s);
}
运行结果如图
没怎么学javaswing也没有swing插件所以界面丑陋。
这个程序比较简陋,有许多待完善的地方:
1.对输入的值进行检查
2.代码命名不规范
3.没有很好的封装(新手写的代码)
新人的写的博客,请见谅
附上github链接:
残缺棋盘可视化github