C#绘制中国象棋棋盘
作者:寒茗清雾 时间:2021-05-27 15:08:44
在C#绘制中国象棋棋盘是C#程序设计中GDI+的一个重要组成部分。这也是非常考验编程技巧的操作。在绘制之前首先要对棋盘有一个完整的认识。下面是完成后的输出图案。
1.在制作过程中用到了背景图片如下:
2.棋盘的横竖交叉线的坐标如下图(棋盘横向、竖向都以50象素间距绘制)
3.更细的坐标分布如下图
完整的代码如下图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chinese_chessboard
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Paint(object sender, PaintEventArgs e)
{
if (!File.Exists(Application.StartupPath + @"\" + "bg.jpg"))
{
MessageBox.Show("请把素材中的bg.jpg图片文件放到可执行程序相同文件夹中,并重新启动程序!", "提示"); Application.Exit();
}
Console.WriteLine("C#编程交流群:485532597");
Graphics g = e.Graphics; // 获取一个Graphics对象
g.Clear(Color.Coral); //用珊瑚色清除窗体
Font font1 = new Font("黑体", 25, FontStyle.Regular);
int s = (int)(font1.Size / 0.6);
int s2 = (int)font1.Height;
Image img = Image.FromFile("bg.jpg");
g.DrawImage(Image.FromFile(Application.StartupPath + @"\" + "bg.jpg"), 10, 10, 430, 480); //绘制背景图
g.DrawRectangle(new Pen(Color.Black, 3), new Rectangle(new Point(20, 20), new Size(410, 460))); //绘制框图
Pen pen = new Pen(Color.Black, 1);
//绘制水平线
for (int i = 0; i < 10; i++)
{
g.DrawLine(pen, new Point(25, 25 + (i * 50)), new Point(425, 25 + (i * 50)));
}
//绘制垂直线
for (int i = 0; i < 9; i++)
{
g.DrawLine(pen, new Point(25 + (i * 50), 25), new Point(25 + (i * 50), 225));
g.DrawLine(pen, new Point(25 + (i * 50), 275), new Point(25 + (i * 50), 475));
}
//return;
//绘制将士位置斜线
g.DrawLine(pen, new Point(175, 25), new Point(275, 125));
g.DrawLine(pen, new Point(275, 25), new Point(175, 125));
g.DrawLine(pen, new Point(175, 375), new Point(275, 475));
g.DrawLine(pen, new Point(175, 475), new Point(275, 375));
//兵位(炮位)线,只画一个
g.DrawLine(pen, new Point(30, 160), new Point(30, 170));
g.DrawLine(pen, new Point(30, 170), new Point(40, 170));
g.DrawLine(pen, new Point(30, 180), new Point(30, 190));
g.DrawLine(pen, new Point(30, 180), new Point(40, 180));
g.DrawString("楚 河", new Font("黑体", 25), Brushes.Black, new Point(50, 235));
g.TranslateTransform(300, 235); //平移坐标轴
g.RotateTransform(180); //进行180度旋转
g.DrawString("漢 界", new Font("黑体", 25), Brushes.Black, new Point(-100, -35));
g.ResetTransform(); //坐标轴复位
this.DrawAngle(g, pen, new Point(125, 175), "all");
this.DrawAngle(g, pen, new Point(425, 175), "left");
this.DrawAngle(g, pen, new Point(225, 175), "all");
this.DrawAngle(g, pen, new Point(425, 175), "left");
this.DrawAngle(g, pen, new Point(325, 175), "all");
this.DrawAngle(g, pen, new Point(425, 175), "left");
this.DrawAngle(g, pen, new Point(75, 125), "all");
this.DrawAngle(g, pen, new Point(375, 125), "all");
this.DrawAngle(g, pen, new Point(25, 325), "right");
this.DrawAngle(g, pen, new Point(125, 325), "all");
this.DrawAngle(g, pen, new Point(225, 325), "all");
this.DrawAngle(g, pen, new Point(325, 325), "all");
this.DrawAngle(g, pen, new Point(425, 325), "left");
this.DrawAngle(g, pen, new Point(75, 375), "all");
this.DrawAngle(g, pen, new Point(375, 375), "all");
g.Dispose(); //释放对象
}
private void DrawAngle(Graphics g,Pen pen, Point point,string str)
{
Point p1 = new Point(point.X + 5, point.Y + 5);
Point p2= new Point(point.X - 5, point.Y + 5);
Point p3= new Point(point.X -5, point.Y - 5);
Point p4 = new Point(point.X +5, point.Y -5);
if(str=="right"||str=="all")
{
//p1 p4
g.DrawLine(pen, p1, new Point(p1.X + 10, p1.Y));
g.DrawLine(pen, p1, new Point(p1.X, p1.Y+10));
g.DrawLine(pen, p4, new Point(p4.X + 10, p4.Y));
g.DrawLine(pen, p4, new Point(p4.X, p4.Y-10));
}
if(str=="left" || str == "all")
{
//p2 p3
g.DrawLine(pen, p2, new Point(p2.X - 10, p2.Y));
g.DrawLine(pen, p2, new Point(p2.X, p2.Y+10));
g.DrawLine(pen, p3, new Point(p3.X - 10, p3.Y));
g.DrawLine(pen, p3, new Point(p3.X, p3.Y - 10));
}
}
}
}
来源:https://blog.csdn.net/stwzx/article/details/112757048
标签:C#,象棋,棋盘
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis-plus分页查询三种方法小结
2023-11-13 08:44:26
![](https://img.aspxhome.com/file/2023/6/63446_0s.png)
java map转Multipart/form-data类型body实例
2023-04-19 13:16:18
Spring @Conditional注解原理解析
2022-10-04 16:09:51
Spring cloud alibaba之Gateway网关功能特征详解
2022-10-26 18:22:28
![](https://img.aspxhome.com/file/2023/1/84611_0s.jpg)
Unity3D基于OnGUI实时显示FPS
2021-06-25 09:10:52
关于@CacheEvict无法解决分页缓存清除的解决思路
2023-07-06 20:22:44
java 中设计模式(值对象)的实例详解
2021-12-27 14:15:08
apollo更改配置刷新@ConfigurationProperties配置类
2022-01-09 22:28:22
![](https://img.aspxhome.com/file/2023/6/62666_0s.png)
java基于JDBC连接Oracle 11g Release2实例分析
2022-06-06 02:36:36
MyBatis-Plus实现分页的方法使用详解
2023-02-14 18:22:16
![](https://img.aspxhome.com/file/2023/6/60026_0s.png)
c#爬虫爬取京东的商品信息
2022-12-03 14:38:11
Java下载文件时文件名乱码问题解决办法
2023-08-23 17:37:03
SpringCloud断路器Hystrix原理及用法解析
2022-03-20 16:47:41
elasticsearch节点的transport请求发送处理分析
2022-04-05 22:38:08
Java中BorderLayout布局管理器的两种排列方式
2022-03-14 08:04:35
![](https://img.aspxhome.com/file/2023/9/80889_0s.png)
C#实现无损压缩图片的示例详解
2023-04-29 21:07:26
![](https://img.aspxhome.com/file/2023/2/84442_0s.png)
java解析多层嵌套json字符串问题
2023-08-26 11:08:33
![](https://img.aspxhome.com/file/2023/5/84315_0s.png)
springboot实现多模块项目添加一新模块
2021-09-22 16:43:09
![](https://img.aspxhome.com/file/2023/7/77377_0s.jpg)
IDEA编译报错:Error:java:无效的源发行版:17的解决办法
2023-08-25 10:38:06
![](https://img.aspxhome.com/file/2023/3/62453_0s.png)
c#使用Dataset读取XML文件动态生成菜单的方法
2022-07-14 22:52:20