一、先绘制中国象棋的背景图,选择白色的底然后背景图我们可以随意,一定要将该图片的bg.jpg放置到应用程序同一个文件夹中。在图上坐标位(10,10)的位置上画一个高度为480,宽度为430的棋盘底部。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Windows2App1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
Image img = Image.FromFile("bg.jpg");
g.DrawImage(img, 10, 10, 430, 480);
二、绘制棋盘的框图,坐标原点为(20,20)高度为460,宽度为410
//Pen pen= new Pen(Color.Black,3);
//Rectangle rec = new Rectangle(new Point(20,20),new Size(410,460));
//g.DrawRectangle(pen,rec);
这三句可以简写成以下语句:
g.DrawRectangle(new Pen(Color.Black, 3), new Rectangle(new Point(20, 20), new Size(410, 460)));
三、画出内部的框线每个格子对应50像素,左上角的坐标应该是(25,25),右上角的坐标为(425,25),然后能够推出规律左边对应(25, 25 + (i * 50)),右边就是(25, 25 + (i * 50)); i从0到9取值,通过for循环来绘制水平线。垂直线的也是类似要用到公式(25 + (j * 50), 25), (25 + (j * 50), 225)); (25 + (j * 50), 275), (25 + (j * 50), 475));,通过for循环完成。在绘制一下将士位置斜线,根据坐标,代码就如下:
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(25, 25 + (i * 50));
}
for (int j = 0; j < 9; j++)
{
g.DrawLine(pen, new Point(25 + (j * 50), 25), new Point(25 + (j * 50), 225));
}
for (int j = 0; j < 9; j++)
{
g.DrawLine(pen, new Point(25 + (j * 50), 25), new Point(25 + (j * 50), 225));
g.DrawLine(pen, new Point(25 + (j * 50), 275), new Point(25 + (j * 50), 475));
}
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));
四、兵位(炮位)线,我们就用10像素线条绘制,距离原竖线、横线5像素。
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));
五、绘制 楚河 汉界,通过坐标平移和旋转得到倒置的汉界,最后释放对象。
Font font = new Font("黑体",20);
g.DrawString("楚 河",font,Brushes.Black,new Point(50,235));
g.TranslateTransform(300, 235);
g.RotateTransform(180);
g.DrawString("汉 界", font, Brushes.Black, new Point(-100,-35));
g.ResetTransform();
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));
}
}
}
}
七、最后的成果图就如下: