3 象棋棋盘
在下面棋盘中,划线尤其是划炮位、兵卒位时,最好在纸上先写好坐标,否则容易和我一样一头大,浪费时间~~
package abstractChess;
import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class ChineseChessBoard extends Board {
public ChineseChessBoard() {// 无参构造函数
this(0, 0, 0, 0);
}
public ChineseChessBoard(int w, int h, int c, int r) {// 有宽、高、列和行的构造函数
this.w = w;
this.h = h;
this.c = c;
this.r = r;
}
public ChineseChessBoard(int w, int h) {// 只有宽和高数的构造函数
this(0, 0, 9, 11);
this.w = w;
this.h = h;
}
public void paint(Graphics g) {
align = 100;// 棋盘距离窗体的网格距离
size = (w - 2 * align) / (c - 1);// 每一列的平均宽度
for (int y = 1; y < r - 1; y++)// 划横线
{
if (y != (r / 2))// 空出楚河汉界的区域
g.drawLine(0 + align, y * size + align, align + (c - 1) * size,
y * size + align);
}
for (int x = 1; x < c - 1; x++)// 划纵线
{
g.drawLine(x * size + align, 0 + align, x * size + align, align
+ (r / 2 - 1) * size);// 划楚河汉界上面的纵线
g.drawLine(x * size + align, align + (r / 2 + 1) * size, x * size
+ align, align + (r - 1) * size);// 划楚河汉界下面的纵线
}
// 划士位
g.drawLine((c / 2 - 1) * size + align, 0 + align, (c / 2 + 1) * size
+ align, 2 * size + align);
g.drawLine((c / 2 + 1) * size + align, 0 + align, (c / 2 - 1) * size
+ align, 2 * size + align);
// 划楚河汉界
g.setFont(new Font("Serif", 2, 25));
g.drawString(" 楚 河 汉 界", align,
(r / 2) * size + align);
// 划士位
g.drawLine((c / 2 - 1) * size + align, (r - 1) * size + align,
(c / 2 + 1) * size + align, (r - 3) * size + align);
g.drawLine((c / 2 + 1) * size + align, (r - 1) * size + align,
(c / 2 - 1) * size + align, (r - 3) * size + align);
// 画炮位
//四个左上直角
g.drawLine(align + size - (size * 2 / 7), align + size * 2
- (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 2 - (size * 1 / 7));// 先划短横
g.drawLine(align + size - (size * 1 / 7), align + size * 2
- (size * 2 / 7), align + size - (size * 1 / 7), align + size
* 2 - (size * 1 / 7));// 再划短竖
g.drawLine(align + 7 * size - (size * 2 / 7), align + size * 2
- (size * 1 / 7), align + 7*size - (size * 1 / 7), align + size
* 2 - (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size - (size * 1 / 7), align + size * 2
- (size * 2 / 7), align + 7 *size - (size * 1 / 7), align + size
* 2 - (size * 1 / 7));// 再划短竖
g.drawLine(align + size - (size * 2 / 7), align + size * 8
- (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 8 - (size * 1 / 7));// 先划短横
g.drawLine(align + size - (size * 1 / 7), align + size * 8
- (size * 2 / 7), align + size - (size * 1 / 7), align + size
* 8 - (size * 1 / 7));// 再划短竖
g.drawLine(align + 7 * size - (size * 2 / 7), align + size * 8
- (size * 1 / 7), align + 7*size - (size * 1 / 7), align + size
* 8 - (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size - (size * 1 / 7), align + size * 8
- (size * 2 / 7), align + 7 *size - (size * 1 / 7), align + size
* 8 - (size * 1 / 7));// 再划短竖
//四个右上直角
g.drawLine(align + size + (size * 1 / 7), align + size * 2
- (size * 1 / 7), align + size + (size * 2 / 7), align + size
* 2 - (size * 1 / 7));// 先划短横
g.drawLine(align + size + (size * 1 / 7), align + size * 2
- (size * 1 / 7), align + size + (size * 1 / 7), align + size
* 2 - (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 2
- (size * 1 / 7), align + 7 * size + (size * 2 / 7), align + size
* 2 - (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 2
- (size * 1 / 7), align + 7 * size + (size * 1 / 7), align + size
* 2 - (size * 2 / 7));// 再划短竖
g.drawLine(align + size + (size * 1 / 7), align + size * 8
- (size * 1 / 7), align + size + (size * 2 / 7), align + size
* 8 - (size * 1 / 7));// 先划短横
g.drawLine(align + size + (size * 1 / 7), align + size * 8
- (size * 1 / 7), align + size + (size * 1 / 7), align + size
* 8 - (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 8
- (size * 1 / 7), align + 7 * size + (size * 2 / 7), align + size
* 8 - (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 8
- (size * 1 / 7), align + 7 * size + (size * 1 / 7), align + size
* 8 - (size * 2 / 7));// 再划短竖
//四个左下直角
g.drawLine(align + size - (size * 2 / 7), align + size * 2
+ (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 2 + (size * 1 / 7));// 先划短横
g.drawLine(align + size - (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 2 + (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size - (size * 2 / 7), align + size * 2
+ (size * 1 / 7), align + 7*size - (size * 1 / 7), align + size
* 2 + (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size - (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + 7 *size - (size * 1 / 7), align + size
* 2 + (size * 2 / 7));// 再划短竖
g.drawLine(align + size - (size * 2 / 7), align + size * 8
+ (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 8 + (size * 1 / 7));// 先划短横
g.drawLine(align + size - (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + size - (size * 1 / 7), align + size
* 8 + (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size - (size * 2 / 7), align + size * 8
+ (size * 1 / 7), align + 7*size - (size * 1 / 7), align + size
* 8 + (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size - (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + 7 *size - (size * 1 / 7), align + size
* 8 + (size * 2 / 7));// 再划短竖
//四个右下直角
g.drawLine(align + size + (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + size + (size * 2 / 7), align + size
* 2 + (size * 1 / 7));// 先划短横
g.drawLine(align + size + (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + size + (size * 1 / 7), align + size
* 2 + (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + 7 * size + (size * 2 / 7), align + size
* 2 + (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 2
+ (size * 1 / 7), align + 7 * size + (size * 1 / 7), align + size
* 2 + (size * 2 / 7));// 再划短竖
g.drawLine(align + size + (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + size + (size * 2 / 7), align + size
* 8 + (size * 1 / 7));// 先划短横
g.drawLine(align + size + (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + size + (size * 1 / 7), align + size
* 8 + (size * 2 / 7));// 再划短竖
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + 7 * size + (size * 2 / 7), align + size
* 8 + (size * 1 / 7));// 先划短横
g.drawLine(align + 7 * size + (size * 1 / 7), align + size * 8
+ (size * 1 / 7), align + 7 * size + (size * 1 / 7), align + size
* 8 + (size * 2 / 7));// 再划短竖
//画卒位
g.drawLine(align + size*0+(size*2/7), align + size*3-(size*1/7), align + size*0+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*0+(size*1/7), align + size*3-(size*2/7), align + size*0+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*0+(size*2/7), align + size*3+(size*1/7), align + size*0+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*0+(size*1/7), align + size*3+(size*2/7), align + size*0+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*2-(size*2/7), align + size*3-(size*1/7), align + size*2-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*2-(size*1/7), align + size*3-(size*2/7), align + size*2-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*2-(size*2/7), align + size*3+(size*1/7), align + size*2-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*2-(size*1/7), align + size*3+(size*2/7), align + size*2-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*2+(size*2/7), align + size*3-(size*1/7), align + size*2+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*2+(size*1/7), align + size*3-(size*2/7), align + size*2+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*2+(size*2/7), align + size*3+(size*1/7), align + size*2+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*2+(size*1/7), align + size*3+(size*2/7), align + size*2+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*4-(size*2/7), align + size*3-(size*1/7), align + size*4-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*4-(size*1/7), align + size*3-(size*2/7), align + size*4-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*4-(size*2/7), align + size*3+(size*1/7), align + size*4-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*4-(size*1/7), align + size*3+(size*2/7), align + size*4-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*4+(size*2/7), align + size*3-(size*1/7), align + size*4+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*4+(size*1/7), align + size*3-(size*2/7), align + size*4+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*4+(size*2/7), align + size*3+(size*1/7), align + size*4+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*4+(size*1/7), align + size*3+(size*2/7), align + size*4+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*6-(size*2/7), align + size*3-(size*1/7), align + size*6-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*6-(size*1/7), align + size*3-(size*2/7), align + size*6-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*6-(size*2/7), align + size*3+(size*1/7), align + size*6-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*6-(size*1/7), align + size*3+(size*2/7), align + size*6-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*6+(size*2/7), align + size*3-(size*1/7), align + size*6+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*6+(size*1/7), align + size*3-(size*2/7), align + size*6+(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*6+(size*2/7), align + size*3+(size*1/7), align + size*6+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*6+(size*1/7), align + size*3+(size*2/7), align + size*6+(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*8-(size*2/7), align + size*3-(size*1/7), align + size*8-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*8-(size*1/7), align + size*3-(size*2/7), align + size*8-(size*1/7), align + size*3-(size*1/7));
g.drawLine(align + size*8-(size*2/7), align + size*3+(size*1/7), align + size*8-(size*1/7), align + size*3+(size*1/7));
g.drawLine(align + size*8-(size*1/7), align + size*3+(size*2/7), align + size*8-(size*1/7), align + size*3+(size*1/7));
//划兵位
g.drawLine(align + size*0+(size*2/7), align + size*7-(size*1/7), align + size*0+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*0+(size*1/7), align + size*7-(size*2/7), align + size*0+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*0+(size*2/7), align + size*7+(size*1/7), align + size*0+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*0+(size*1/7), align + size*7+(size*2/7), align + size*0+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*2-(size*2/7), align + size*7-(size*1/7), align + size*2-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*2-(size*1/7), align + size*7-(size*2/7), align + size*2-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*2-(size*2/7), align + size*7+(size*1/7), align + size*2-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*2-(size*1/7), align + size*7+(size*2/7), align + size*2-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*2+(size*2/7), align + size*7-(size*1/7), align + size*2+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*2+(size*1/7), align + size*7-(size*2/7), align + size*2+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*2+(size*2/7), align + size*7+(size*1/7), align + size*2+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*2+(size*1/7), align + size*7+(size*2/7), align + size*2+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*4-(size*2/7), align + size*7-(size*1/7), align + size*4-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*4-(size*1/7), align + size*7-(size*2/7), align + size*4-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*4-(size*2/7), align + size*7+(size*1/7), align + size*4-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*4-(size*1/7), align + size*7+(size*2/7), align + size*4-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*4+(size*2/7), align + size*7-(size*1/7), align + size*4+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*4+(size*1/7), align + size*7-(size*2/7), align + size*4+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*4+(size*2/7), align + size*7+(size*1/7), align + size*4+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*4+(size*1/7), align + size*7+(size*2/7), align + size*4+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*6-(size*2/7), align + size*7-(size*1/7), align + size*6-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*6-(size*1/7), align + size*7-(size*2/7), align + size*6-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*6-(size*2/7), align + size*7+(size*1/7), align + size*6-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*6-(size*1/7), align + size*7+(size*2/7), align + size*6-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*6+(size*2/7), align + size*7-(size*1/7), align + size*6+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*6+(size*1/7), align + size*7-(size*2/7), align + size*6+(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*6+(size*2/7), align + size*7+(size*1/7), align + size*6+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*6+(size*1/7), align + size*7+(size*2/7), align + size*6+(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*8-(size*2/7), align + size*7-(size*1/7), align + size*8-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*8-(size*1/7), align + size*7-(size*2/7), align + size*8-(size*1/7), align + size*7-(size*1/7));
g.drawLine(align + size*8-(size*2/7), align + size*7+(size*1/7), align + size*8-(size*1/7), align + size*7+(size*1/7));
g.drawLine(align + size*8-(size*1/7), align + size*7+(size*2/7), align + size*8-(size*1/7), align + size*7+(size*1/7));
// 设置四周边线为粗线
float lineWidth = 3.0f;
((Graphics2D) g).setStroke(new BasicStroke(lineWidth));
// 划边界四周
g.drawLine(align, align, size * (c - 1) + align, align);
g.drawLine(align, size * (r - 1) + align, size * (c - 1) + align,
(r - 1) * size + align);
g.drawLine(align, align, align, (r - 1) * size + align);
g.drawLine(align + size * (c - 1), align, align + size * (c - 1),
(r - 1) * size + align);
}
}
4.设计窗体
package abstractChess;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class MainFrame extends JFrame implements ActionListener {// 自定义类文件,必须继承JFrame框架
// 定义组件
JMenuBar jmb;
JMenu[] jm;
JMenuItem[] jmi;
public static void main(String[] args) {// 添加主函数,并调用类的构造函数
new MainFrame();
}
public MainFrame() {// 添加构造函数
// 创建组件
jmb = new JMenuBar();
jm = new JMenu[2];
jm[0] = new JMenu("棋类游戏选择");
jm[1] = new JMenu("帮助");
jmi = new JMenuItem[4];
jmi[0] = new JMenuItem("五子棋");
jmi[1] = new JMenuItem("象棋");
jmi[2] = new JMenuItem("关于");
jmi[3] = new JMenuItem("退出");
for (int i = 0; i < 4; i++) {
jmi[i].addActionListener(this);
}
initGUI();//设置布局,添加组件
//设置窗口属性
this.setTitle("使用java制作的棋类游戏By毛二");//基础属性
this.setIconImage(new ImageIcon("熊猫.jpg").getImage());//设置窗口标题
this.setSize(800,800);//设置大小,按像素
this.setLocation(1000,200);//设置初始位置,默认是屏幕最左上角
this.setVisible(true);//让它显示,位置可变
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认到关闭操作,不然java虚拟机还没也关闭
}
private void initGUI() {
this.setJMenuBar(jmb);
jmb.add(jm[0]);
jmb.add(jm[1]);
jm[0].add(jmi[0]);
jm[0].add(jmi[1]);
jm[1].add(jmi[2]);
jm[1].add(jmi[3]);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jmi[0]){//五子棋
paintRenjuBoard();
}
if(e.getSource() == jmi[1]){//象棋
paintChineseChessBoard();
}
if(e.getSource() == jmi[2]){//关于
JOptionPane.showMessageDialog(null, "使用java制作的棋类游戏By毛二");
}
if(e.getSource() == jmi[3]){//退出
System.exit(0);
}
}
private void paintRenjuBoard() {
update(getGraphics());//清空上次窗体里的棋盘
RenjuBoard r = new RenjuBoard(600,400);
r.paint(getGraphics());//继承了JFrame类,方便获取图
}
private void paintChineseChessBoard() {
update(getGraphics());//清空上次窗体里的棋盘
ChineseChessBoard c = new ChineseChessBoard(520,400);
c.paint(getGraphics());
}
}