C#绘制中国象棋棋盘
程序员文章站
2022-03-23 18:44:01
在c#绘制中国象棋棋盘是c#程序设计中gdi+的一个重要组成部分。这也是非常考验编程技巧的操作。在绘制之前首先要对棋盘有一个完整的认识。下面是完成后的输出图案。1.在制作过程中用到了背景图片如下:2....
在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)); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。